使用上下文菜单在列表视图中选择项目

时间:2014-05-09 05:16:54

标签: android android-listview contextmenu

无法弄清楚我的代码有什么问题:

registerForContextMenu(lv_myhitshotlists);
    AdapterContextMenuInfo menuinfo = null;

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        /*menu.setHeaderTitle("Item Operations");
        menu.add(0, v.getId(), 0, "Edit Film");
        menu.add(0, v.getId(), 0, "Delete Film");*/

        MenuInflater inflater = getMenuInflater();  
        inflater.inflate(R.menu.context_myfilm_list,menu);
        menuinfo = (AdapterContextMenuInfo) menuInfo;
        menu.setHeaderTitle("Options");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
    try{
        menuinfo = (AdapterContextMenuInfo) item.getMenuInfo();
        AdapterView.AdapterContextMenuInfo info= (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        Long ids = madapter.getItemId(info.position);//what item was selected is ListView
         i = ids.intValue();

        switch (item.getItemId()) { 

        case R.id.menu_edit_myfilm:

            intHitshotEdit = new Intent(MyHitshotListActivity.this,MyHitshotInfoEditActivity.class);
            intHitshotEdit.putExtra("video_id", myList.get(i).getid());
            intHitshotEdit.putExtra("video_title", myList.get(i).getTitle());
            Toast.makeText(getApplicationContext(),myList.get(i).getTitle(), Toast.LENGTH_SHORT).show();
            //intVideodetails.putExtra("video_desc", myList.get(i).get);
            startActivity(intHitshotEdit);
            overridePendingTransition(R.anim.slideinfromright,R.anim.slideouttoleft);
            //intVideoUpload.putExtra("title",""+info.);

            return true;

        case R.id.menu_delete_myfilm:

            return true;

        default:

        }
    }catch(Exception e)
    {
        e.printStackTrace();
    }
        return super.onContextItemSelected(item);
        }

每次Long ids都返回0.我在asynctask的onpostexecute()部分将数据绑定到listview,如下所示:

 public void setAdapterToListview(ArrayList<MyHitshotListData> myList) {
              madapter=new MyHitshotListAdapter(context, myList);
              lv_myhitshotlists.setAdapter(madapter);
          }

MyHitshotListAdapter是一个扩展基本适配器类的适配器。有什么问题?为什么ids的值总是为零?position的值是正确的。

3 个答案:

答案 0 :(得分:5)

您可以通过MenuItem

获取您长按的列表项的位置
@Override
    public boolean onContextItemSelected(MenuItem item) {
         AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
         int listPosition = info.position;
        myList.get(listPosition).getTitle();//list item title
    }

答案 1 :(得分:4)

您将在onContextItemSelected(MenuItem item)方法中直接获得索引,例如

  

int index = info.position;

您将从ArrayList中获取特定索引的数据

  

int video_id = myList.get(index).getid();

答案 2 :(得分:1)

@Override
public boolean onContextItemSelected(MenuItem item) {
     AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
     int listPosition = info.position;
    adapter.getItem(listPosition);//list item title
}