Search code examples
androiddatabaselistviewandroid-cursoradapter

How to delete a particular row from my cursor based listview and data base using context menu


I want to delete a particular row from my listview generated by a cursoradapter and also from my database. In my database the _id column is primary key and also autoincrement so if i delete a particular row its id is deleted so will getposition() work in this case.I want only that particular row to be deleted.

public class DataViewer extends Fragment{
    static ListView listdata;
     DataAdapter data;
    Cursor cursor;
    DataCursorAdapter datacursor;

    public DataViewer() {
        // TODO Auto-generated constructor stub
    }

    public void update(){
          cursor=data.fetchdata();
          datacursor.swapCursor(cursor);
    }

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View rootview =inflater.inflate(R.layout.data_fragment, container,             false);
        listdata=(ListView) rootview.findViewById(R.id.listdata);
        data=new DataAdapter(getActivity());
        cursor=data.fetchdata();
        datacursor=new DataCursorAdapter(getActivity(), cursor);
        listdata.setAdapter(datacursor);
        registerForContextMenu(listdata);
        return rootview;
    }

class DataCursorAdapter extends CursorAdapter{

    public DataCursorAdapter(Context context, Cursor c) {
        super(context, c,0);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void bindView(View arg0, Context arg1, Cursor arg2) {
        // TODO Auto-generated method stub
        TextView time=(TextView) arg0.findViewById(R.id.texttime);
        TextView descriptiontext=(TextView)  arg0.findViewById(R.id.textdescription);
            String timer=arg2.getString(arg2.getColumnIndexOrThrow("date"));
        time.setText(timer);
        String description=
    arg2.getString(arg2.getColumnIndexOrThrow("description"));
    descriptiontext.setText(description);
    String mood=arg2.getString(arg2.getColumnIndexOrThrow("mood"));
    int i=arg2.getInt(0);
    Log.d("The value of id is", ""+i);
    ImageView image=(ImageView) arg0.findViewById(R.id.dataimage);
    switch (mood) {
    case "Excited":
        image.setImageResource(R.drawable.excited);
        break;
    case "Happy":
        image.setImageResource(R.drawable.happy);
        break;
    case "Lonely":
        image.setImageResource(R.drawable.lonely);
        break;
    default:
        image.setImageResource(R.drawable.others);
        break;
        }
    }

    @Override
    public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
    // TODO Auto-generated method stub
        return
     LayoutInflater.from(arg0).inflate(R.layout.data_row, arg2,false);
    }}

   @Override
   public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    // TODO Auto-generated method stub
       super.onCreateContextMenu(menu, v, menuInfo);
       menu.add(Menu.NONE, R.id.menudelete, 0, "Delete");
     }

   @Override
   public boolean onContextItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
       switch (item.getItemId()) {
    case R.id.menudelete:

        break;

    default:
        break;
    }
       return super.onContextItemSelected(item);
    }

}

Solution

  • I think something like the following should work but I haven't tested it...

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        switch (item.getItemId()) {
            case R.id.menudelete:
                deleteItem(info.position);
                return true;
            default:
                return super.onContextItemSelected(item);
        }
    }
    
    private void deleteItem(int position) {
        Cursor c = dataCursor.getCursor();
        c.moveToPosition(position);
        int databaseId = c.getInt(c.getColumnIndex("_id"));
    
        // Use databaseId to delete the row from the database...
        // ...then re-query the database to update the Cursor...
        // ...and call notifyDatasetChanged() on the Adapter.
    }