samedi 7 mars 2015

trying to display data from my table into listview in android whats wrong?

as I mentioned in the title


I am trying to display data from my table into listview but when I open the emulator it stops immediately and I think i have an error nullpointer or something like that .. or I do not really now ..


here is my code:


JAVA


MainActivity.java :



public class MainActivity extends Activity {

ListView PersonsList;
DataBaseAdapter helper;
private SimpleCursorAdapter dataAdapter;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


helper = new DataBaseAdapter(this);

displayListView();

}


@TargetApi(Build.VERSION_CODES.HONEYCOMB)


private void displayListView() {


Cursor cursor = helper.fetchAllPersons();

// The desired columns to be bound
String[] columns = new String[]{

DataBaseAdapter.Helper.PERSON_NAME,
DataBaseAdapter.Helper.GENDER,
};

// the XML defined views which the data will be bound to
int[] to = new int[]{
R.id.personName,
R.id.personGender,

};

// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(
this, R.layout.single_person_item,
cursor,
columns,
to,
0);

PersonsList = (ListView) findViewById(R.id.personsListView);
PersonsList.setAdapter(dataAdapter);
}
}


fetcAllPpersons() in DataBaseAdapter.java :


public Cursor fetchAllPersons() {



SQLiteDatabase db = helper.getWritableDatabase();

Cursor mCursor = db.query(Helper.PERSONS_TABLE, new String[] {Helper.PERSON_ID,Helper.PERSON_NAME,Helper.DATE_OF_BIRTH,Helper.GENDER,Helper.ADDRESS},
null, null, null, null, null);

if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}




xml:


activity_main.xml:



<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
>


<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="+"
android:id="@+id/addnewperson"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete All"
android:id="@+id/deleteall"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/addnewperson"
android:layout_alignEnd="@+id/addnewperson" />

<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/scrollView2"
android:layout_below="@+id/addnewperson"
android:layout_alignLeft="@+id/addnewperson"
android:layout_alignStart="@+id/addnewperson">


</ScrollView>

<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/personsListView"
android:layout_below="@+id/scrollView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />


</RelativeLayout>


where PERSONS_TABLE is the table i want to show some infromation from


where is the problem ??


thx in advance


Aucun commentaire:

Enregistrer un commentaire