2

I have a listview that gets the data from an Arraylist and this Arraylist gets data from a database. Now I want to delete an item from the listview and also I want to delete this record from database. In addition, the delete option is in a contextmenu. I just want to know how to send the record's Id to the listview items and also the delete method in the dataModel. I can have the ID by reslist.getId()

Here is getView and contextmenu:

       public View getView(int i, View view, ViewGroup viewGroup) {

        View v = view;

        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService
                    (Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.list_layout, null);

        }

        TextView tv1  = (TextView)  v.findViewById(R.id.resName);
        TextView tv2  = (TextView)  v.findViewById(R.id.resAddress);
        ImageView iv  = (ImageView) v.findViewById(R.id.resType);

        tv1.setText(resList.get(i).getName());
        tv2.setText(resList.get(i).getAddress());
        iv.setImageResource(R.drawable.tpng);

        if(resList.get(i).getType().equals("takeaway")){
            iv.setImageResource(R.drawable.tpng);
        }else if(resList.get(i).getType().equals("delivery")){
            iv.setImageResource(R.drawable.dpng);
        }else if(resList.get(i).getType().equals("sitdown")){
            iv.setImageResource(R.drawable.spng);
        }

        registerForContextMenu(v);

        return v;
    }
 public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {

    getMenuInflater().inflate(R.menu.list_menu, menu);      
    super.onCreateContextMenu(menu, v, menuInfo);
   }

   public boolean onContextItemSelected(MenuItem item) {

    switch(item.getItemId()){
    case R.id.remove:
        // I should use delete method here and I just want Item Id
        break;
    case R.id.item2:

        break;


    }

    return super.onContextItemSelected(item);
   }

and this is the delete method in dataModel

      public void deleteRestaurant(int id){
    SQLiteDatabase db = getWritableDatabase();
    db.execSQL("DELETE FROM " + TABLE_RESTAURANT + "WHERE" + KEY_ID + " = ?", new String[] {String.valueOf(id)});   
}
Michael
  • 3,093
  • 7
  • 39
  • 83
Pardis M
  • 75
  • 6

3 Answers3

1

To delete a record from the sqlite database , you can use the following code :

public void deleteRestaurant(int id) {
 System.out.println("the deleted restaurant has the id: " + id);
 SQLiteDatabase db = getWritableDatabase();
 db.delete(TABLE_RESTAURANT, KEY_ID + " = " + id, null);
 }

Check this full tutorial about how to use SQLite Database with Multiple Tables in Android

Houcine
  • 24,001
  • 13
  • 56
  • 83
0

If you use a CursorLoader with your Adapter, all you have to do is delete the row in the database and the rest will happen automatically.

Here is a link: Loaders

David C Adams
  • 1,953
  • 12
  • 12
0

you need to get the position of the selected item. then remove item from your Array list. Once you have removed the item, call the notifyDataSetChanged method then it will automatically remove the view.

  switch(item.getItemId()){
case R.id.remove:
    // I should use delete method here and I just want Item Id
    deleteRestaurant(id);
    arrayList.remove(position);
    notifyDataSetChanged();
    break;
case R.id.item2:

    break;
Genevieve
  • 450
  • 3
  • 9