我创建了一个自定义的 CursorAdapter 并想要选择一个列表项,以便在 onOptionsItemSelected 中启动一个操作。

创建 ListView :

public void onCreate(Bundle savedInstanceState) {
    Log.d(TAG, "onCreate called");
    super.onCreate(savedInstanceState);

    Log.d(TAG, "create DatabaseOpenHelper");
    DatabaseOpenHandler helper = new DatabaseOpenHandler(this);

    Log.d(TAG, "get writeable database access");
    database = helper.getWritableDatabase();

    Log.d(TAG, "create Cursor for database access");
    Cursor data = database.query(DatabaseConstants.TABLE_NOTES, fields,
            null, null, null, null, null);

    Log.d(TAG, "set NoteCursorAdapeter");
    setListAdapter(new NoteCursorAdapter(this, data));
}

onOptionItemSelected:
public boolean onOptionsItemSelected(MenuItem item) {
    Log.d(TAG, "onOptionItemSelected called");
    switch (item.getItemId()) {
    case R.id.conference_note_menu_new:
        Toast.makeText(this, "Es sind keine Einstellungen verfügbar",
                Toast.LENGTH_LONG).show();
        return true;

    case R.id.conference_note_menu_edit:
        Toast.makeText(this, "Es sind keine Einstellungen verfügbar",
                Toast.LENGTH_LONG).show();
        return true;

    case R.id.conference_note_menu_delete:
        Toast.makeText(this, "Es sind keine Einstellungen verfügbar",
                Toast.LENGTH_LONG).show();
        return true;

    default:
        return super.onOptionsItemSelected(item);
    }
}

在互联网上找不到任何有用的信息。

最佳答案

onOptionsItemSelected 用于菜单。你需要像这样为你的 ListView 设置 onItemClickListener:

getListView().setOnItemClickListener(this);
并实现:
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub
}

关于android - CursorAdapter 的 onClick 监听器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9990076/

10-10 17:12