Ali_Waris
Ali_Waris

Reputation: 2382

Android : Deleting an item from listview as well as database

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

Upvotes: 2

Views: 272

Answers (1)

Manish
Manish

Reputation: 1215

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.

Upvotes: 2

Related Questions