2012-04-03 44 views
3

我创建了一个自定义的CursorAdapter并希望选择一个列表项,以便在onOptionsItemSelected中启动一个动作。用于CursorAdapter的onClick监听器

创建列表视图:

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

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

回答

2

onOptionItemSelected是菜单。你需要设置onItemClickListener为您的ListView这样的:

getListView().setOnItemClickListener(this); 

和工具:

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

使用下面的命令行点击listerner:;

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

因为

public boolean onOptionsItemSelected(MenuItem item) { 
} 

是菜单项目选择

1

这个工作对我来说:

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

      } 
     }); 
0

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