0

I can't get this to work, I have looked through many posts and I really am desperate since I have to finish this until the day after tomorrow. The problem is the following:

I have a listView with entries from a database. It is possible to do a long click on them to call a contextMenu. In the context menu I can either delete or edit the entry, and to do that I need the id of the selected item.

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
                                ContextMenuInfo menuInfo) {
  super.onCreateContextMenu(menu, v, menuInfo);
  MenuInflater inflater2 = getMenuInflater();
  inflater2.inflate(R.menu.edit_grade_menu, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
  AdapterContextMenuInfo info =  (AdapterContextMenuInfo) item.getMenuInfo();
  int id = (int) info.id;
  switch (item.getItemId()) {
  case R.id.edit_grade:
    Intent i = new Intent(this, AddGradeActivity.class);
    i.putExtra(GradesDbAdapter.KEY_ROWID, linkSubject);

    // putExtra edit, so addGradeActivity knows it has to fill views with values to edit grade
    i.putExtra("edit", true);
    i.putExtra(GradesDbAdapter.KEY_GRADE, id);
    this.startActivity(i);
    finish();

    return true;
  case R.id.del_grade:
      myDbHelper.deleteGradeEntry(id, semester);
      // filldata to refresh listview
      fillData();
    return true;
  default:
    return super.onContextItemSelected(item);
  }
}

Now my problem is that this id that I get from info always is 0. It's really weird since it has worked before I changed the layout, I have this same activity running in 2 tabs on the same screen. Could this be the reason?

zenhaeus
  • 3
  • 1
  • does the item.getItemId() return you the wrong value? or the item.id? Check this link mate http://stackoverflow.com/questions/2453620/android-how-to-find-the-position-clicked-from-the-context-menu – Sergey Benner Jan 17 '12 at 21:03
  • the info.id() returns 0, no matter which entry I choose. – zenhaeus Jan 17 '12 at 21:05
  • could you please show your R.menu.edit_grade_menu layout – Sergey Benner Jan 17 '12 at 21:12
  • try to add in your onCreateContextMenu() two lines menu.add(0, 0, 0, "Edit Grade"); menu.add(0, 1, 1, "Edit Grade"); and see if it works OR menu.add(0, v.getId(), 0, "Edit Grade"); menu.add(0, v.getId(), 0, "Edit Grade"); – Sergey Benner Jan 17 '12 at 21:29
  • `//inflater2.inflate(R.menu.edit_grade_menu, menu); menu.add(0, 0, 0, "Edit Grade"); menu.add(0, 1, 1, "Delete Grade");` I tried this but I still get 0 when I log info.id same with `menu.add(0, v.getId(), 0, "Edit Grade"); menu.add(0, v.getId(), 0, "Edit Grade");` – zenhaeus Jan 17 '12 at 21:34
  • Here is a good example about ContextMenu creation http://www.stealthcopter.com/blog/2010/04/android-context-menu-example-on-long-press-gridview/ – Sergey Benner Jan 17 '12 at 21:34
  • here is your answer man - http://stackoverflow.com/questions/6208738/adaptercontextmenuinfo-id – Sergey Benner Jan 17 '12 at 21:41
  • That doesn't really help since my problem is that I can't read the id from the element that has been "long-clicked" and I really need this id because I have to fetch the data from the database on the next screen. – zenhaeus Jan 17 '12 at 21:42
  • well the context menu is the long click and you rely on position and not on the ID and as it is written 'It is the value of the _id column that is inside your Cursor, when you are using a CursorAdapter'. So basically you will need to use the CursorAdapter when you populate your clickable ListView. Perhaps you're using something else. – Sergey Benner Jan 17 '12 at 21:43
  • Thanks a lot, I found the error, the problem was in my database, somehow the primary key field was not autoincrement any more and all the entries had id 0... I should have guessed it earlier but it was confusing, because yesterday it was working fine... thanks anyway :) – zenhaeus Jan 17 '12 at 22:48

1 Answers1

2

I had the same problem. It did not happen in context menu/class that implemented ListView, but during creation of database table, _id column. When using this syntax:

db.execSQL("CREATE TABLE TABLE_NAME(_id INTEGER AUTO INCREMENT PRIMARY KEY,  TITLE TEXT, VALUE REAL);");

AdapterView.AdapterContextMenuInfo.id always returns 0

however, if this syntax is used:

db.execSQL("CREATE TABLE TABLE_NAME(_id INTEGER PRIMARY KEY AUTOINCREMENT,  TITLE TEXT, VALUE REAL);");

AdapterView.AdapterContextMenuInfo.id always returns the correct id.

So there is difference between:

_id INTEGER AUTO INCREMENT PRIMARY KEY 

and

_id INTEGER PRIMARY KEY AUTOINCREMENT

second works, first does not. What makes it difficult to debug is that both are syntactically correct.

Another important thing is that one needs to delete database object from the application via adb terminal before rerunning the app again, since onCreate method of the class that extends SQLiteOpenHelper does not trigger if database already exists - it triggers only if it doesn't, so it is important to delete database after making a correction, then rerun the application.

I hope that helps.

Damian Kozlak
  • 7,065
  • 10
  • 45
  • 51
Tomas Antos
  • 284
  • 2
  • 6