onClick Listerator for CursorAdapter

时间:2012-04-03 08:57:51

标签: android

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

创建列表视图:

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);
    }
}

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

4 个答案:

答案 0 :(得分:2)

将以下行用于项目点击监听器:;

public void onListItemClick(ListView parent, View v, int position, long id) {
}

因为

public boolean onOptionsItemSelected(MenuItem item) {
}

用于菜单项选择

答案 1 :(得分:2)

onOptionItemSelected用于菜单。你需要为你的ListView设置onItemClickListener,如下所示:

getListView().setOnItemClickListener(this);

并实施:

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub
}

答案 2 :(得分:1)

这对我有用:

       list.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

            }
        });

答案 3 :(得分:0)

使用OnLongClickListener()填充菜单,然后使用您的代码开始对所选项目执行操作。