Search code examples
androidsqlitelistviewsimplecursoradapter

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);

        }
    });
}

Solution

  • I'd suggest sending the data, extracted from the cursor as intent extras. Here's an example (a little more complicated that what you want, as on Itemclick I display an intermediate dialog to select Edit or Stock (place products into a aisle in a shop) options. :-

    productlist_csr = shopperdb.getProductsAsCursor();
            currentpca = new ProductsCursorAdapter(this, productlist_csr, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
            productlistview.setAdapter(currentpca);
    
            productlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    AlertDialog.Builder okdialog = new AlertDialog.Builder(productlistview_context);
                    okdialog.setTitle(R.string.productlistentryclicktitle);
                    okdialog.setMessage(R.string.productlistentryclickmessage001);
                    okdialog.setCancelable(true);
                    final int pos = position;
                    okdialog.setNegativeButton(R.string.standardstockproductlist, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent(productlistview_context, AddProductToShopActivity.class);
                            productlist_csr.moveToPosition(pos);
                            intent.putExtra("Caller", THIS_ACTIVITY);
                            intent.putExtra("PRODUCTID", productlist_csr.getLong(ShopperDBHelper.PRODUCTS_COLUMN_ID_INDEX));
                            intent.putExtra("ProductName", productlist_csr.getString(ShopperDBHelper.PRODUCTS_COLUMN_NAME_INDEX));
                            intent.putExtra("ProductNotes", productlist_csr.getString(ShopperDBHelper.PRODUCTS_COLUMN_NOTES_INDEX));
                            startActivity(intent);
                            dialog.cancel();
                        }
                    });
                    okdialog.setPositiveButton(R.string.standardedittext, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent(productlistview_context, ProductAddActivity.class);
                            intent.putExtra("Caller", THIS_ACTIVITY + "Update");
                            productlist_csr.moveToPosition(pos);
                            intent.putExtra("ProductName", productlist_csr.getString(ShopperDBHelper.PRODUCTS_COLUMN_NAME_INDEX));
                            intent.putExtra("ProductNotes", productlist_csr.getString(ShopperDBHelper.PRODUCTS_COLUMN_NOTES_INDEX));
                            intent.putExtra("PRODUCTID", productlist_csr.getLong(ShopperDBHelper.PRODUCTS_COLUMN_ID_INDEX));
                            startActivity(intent);
                            dialog.cancel();
                        }
                    });
                    okdialog.setNeutralButton(R.string.standardbacktext, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });
                    okdialog.show();
                }
            });
    

    Note! ShopperDBHelper.PRODUCTS_COLUMN_?????_INDEX equates to the respective column's offset into the cursor. I use the intent Extra Caller to notify the started activity of the calling activity (eg Stock can be called from a product, a shop or an aisle) so that it can act accordingly.

    In the above position is used to to move to the respective cursor row (However, I believe that this isn't actually needed as the cursor will already be positioned, but do this just in case).

    I also use a custom cursor but that shouldn't, I believe, be an issue (I've never used simpleCursor).