zenhaeus
zenhaeus

Reputation: 3

id of selected item from contextMenu is always 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?

Upvotes: 0

Views: 357

Answers (1)

Tomas Antos
Tomas Antos

Reputation: 284

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.

Upvotes: 2

Related Questions