2

I have a listview in which i am adding the items whose values are retrieved from the table in my database. Now, in the current scenario, if i am deleting an item, i am passing the complete string and breaking it in parts to provide information to my delete query.

Instead of all these dumb work, i can simply pass the id of that particular item, but the id is not in the string array which is binded to the listview. How can i delete items from the database by simply using the item's id...?

Note: id is the column in table, so i have each item's id from database. But i am not displaying it in the listview.

   String[] records = new String[report.size()];
        for(int i = 0; i<report.size(); i++)
        {
            if(report.get(i).getDate()!="")
            {
                String eachRecord = (i+1) +". "+report.get(i).getDate() + " -- " + report.get(i).getType();
                records[i] = eachRecord;
            }

        }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.singlerow, records);
        listView.setAdapter(adapter);
Ali_Waris
  • 1,944
  • 2
  • 28
  • 46

1 Answers1

2

You should map the data you are getting from the Database to a java object.

Read the data from the db store in list or array whatever you like.

Create your custom adapter by extending the BaseAdaper REF and pass your list of object to it.

In Adapter's getView build your view.

Now when you delete your list item you can easily find that object and ask for the id of it. You can use this id to delete the item from db. Now you can refresh the list with the remaining data.

Manish
  • 1,215
  • 10
  • 29
  • For now, I have created a Map and put the key as the item and value as its id. While deleting, I am using that Map to determine which id I need to delete from the database. Can you please throw some more light on custom adapters? Edit : Thanks for your help. – Ali_Waris May 13 '15 at 04:46
  • Below are few tutorials on how to use Custom Adapter http://www.pcsalt.com/android/listview-using-baseadapter-android/ http://androidadapternotifiydatasetchanged.blogspot.in/ – Manish May 13 '15 at 04:47
  • This link also helped me a lot in understanding Custom Adapters. http://www.codelearn.org/android-tutorial/android-listview – Ali_Waris May 13 '15 at 09:21