Note that there are some explanatory texts on larger screens.

plurals
  1. PODeleting items from a database in android
    text
    copied!<p>I have a little problem. I'm trying to delete an item from a list which is store in a database. When I chose "Delete List", all the others items are deleted,but the item I choosed is not deleted. I don't understand where is my mistake. Please help.</p> <p>Here is my code :</p> <pre><code>public class IncercListe extends Activity { TextView txt; String value; Button btn; private ListView lv1; private AlertDialog alertDialog; private ArrayAdapter&lt;String&gt; m_adapter; Bundle bundle ; String item; private Cursor cursor; private ListDbAdapter db; private static final int MENU_CREATE = Menu.FIRST; private static final int MENU_RENAME=Menu.FIRST+1; private static final int MENU_EDIT = Menu.FIRST + 2; private static final int MENU_SEND=Menu.FIRST+3; private static final int MENU_DELETE=Menu.FIRST+4; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.list1); lv1=(ListView)findViewById(R.id.list); db=new ListDbAdapter(this); db.open(); fillData(); registerForContextMenu(lv1); lv1.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView&lt;?&gt; parent,View v,int position, long id) { Intent j= new Intent(IncercListe.this, List.class); startActivity(j); } }); public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { menu.setHeaderTitle("Choose "); menu.add(0, MENU_RENAME, 0, "Rename List"); menu.add(0, MENU_EDIT, 0, "Edit List"); menu.add(0, MENU_SEND, 0, "Send List"); menu.add(0, MENU_DELETE, 0, "Delete List"); } @Override public boolean onContextItemSelected(MenuItem mitem) { switch (mitem.getItemId()) { case MENU_RENAME: rename(); return true; case MENU_EDIT: Intent j= new Intent(IncercListe.this, List.class); startActivity(j); return true; case MENU_SEND: return true; case MENU_DELETE: AdapterContextMenuInfo info=(AdapterContextMenuInfo)mitem.getMenuInfo(); db.delete(info.id); fillData(); return true; default: return super.onContextItemSelected(mitem); } } public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, MENU_CREATE, 0, "New List");//.setIcon(R.drawable.quit); return true; } /* Handles item selections */ public boolean onOptionsItemSelected (MenuItem item) { switch (item.getItemId()) { case MENU_CREATE: createlist(); return true; } return false; } public void createlist() { alertDialog = new AlertDialog.Builder(this).create(); alertDialog.setTitle("New List"); alertDialog.setMessage("Enter name of new shopping list :"); final EditText edt=new EditText(this); alertDialog.setView(edt); alertDialog.setButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { item =edt.getText().toString(); if (!item.equals("")) db.create(item); fillData(); return; } }); alertDialog.setButton2("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.cancel(); return; } }); alertDialog.show(); } public void rename() { } public void sendlist(){ } protected void onActivityResult(int requestCode, int resultCode, Intent intent){ super.onActivityResult(requestCode, resultCode, intent); fillData(); } private void fillData(){ cursor=db.fetchAll(); startManagingCursor(cursor); ListAdapter adapter=new SimpleCursorAdapter(this,R.layout.textview,cursor, new String[] {ListDbAdapter.KEY_TITLE},new int[] {R.id.txt1}); lv1.setAdapter(adapter); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload