dimanche 8 mai 2016

Populating a list view with from a Database

So my question is I created a listview and populated it using a simple cursor adapter. So now when I click an item on my listview it takes me to another activity which suppose to show me the details of the item I clicked. What is the best approach on going about this? Here is my function of populating the list view. What exactly should I be sending to the next activity? I was thinking about sending the position but then that wouldnt work because in the next activity I would have to access the database using the position but that wouldnt be accurate because the database could have rows deleted which can return me a different row of data. Any ideas would be truly appreciated.

private void populateListView(){

    Cursor cursor = myDatabase.getAllData();
    String[] fromfieldNames = new String[]{StudentDBOpenHelper.ITEM_NAME_COLUMN};
    int[] toViewIDs = new int[] {R.id.textView_itemName};
    SimpleCursorAdapter myCursorAdapter;
    myCursorAdapter = new SimpleCursorAdapter(getActivity(),
            R.layout.indvidualview_layout,cursor,fromfieldNames,toViewIDs,0);
    ListView myList = (ListView) getActivity().findViewById(R.id.courseListXML);
    myList.setAdapter(myCursorAdapter);


    myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


            Intent nextActivity = new Intent(getActivity(), CourseWorkItemActivity.class);

            nextActivity.putExtra("Item", position);

            startActivity(nextActivity);

        }
    });
}

Aucun commentaire:

Enregistrer un commentaire