获取listView行onContextItemSelected

时间:2013-01-21 23:23:48

标签: android listview contextmenu

我正在做一个proyect,我有一个在运行时创建多个listview的视图,每个listview加载特定的数据库行。我想实现一个contextmenu。问题是如何获取listview的行来解除数据库的id?如何在上下文菜单中获取列表适配器?或其他一些解决方案。谢谢! 这是代码的一部分...

private void makeView(){

   yearsArray = db.getUniqueYears(TABLE_NAME);

   for (int i = 0; i <  yearsArray.size()  ; i++){

        list = db.getDocByYear(TABLE_NAME,  yearsArray.get(i));
        custom_adapter = new Document_adapter(this, list);
        ListView lv = new ListView(this);
        lv.setAdapter(custom_adapter);
        lv.setBackgroundResource(R.drawable.title_container_bg);
        registerForContextMenu(lv);

        }
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);

    MenuInflater mi = getMenuInflater();
    mi.inflate(R.menu.doc_options, menu);


}

  @Override
public boolean onContextItemSelected(MenuItem item) {
     AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

    /* 

            */


    return super.onContextItemSelected(item);
}

1 个答案:

答案 0 :(得分:4)

如果您正确设置了数据库,则可以在info.id;中执行此onContextItemSelected,并提供数据库ID