一个活动中的多个列表视图,每个列表视图都有上下文菜单

时间:2014-07-16 10:23:14

标签: android listview contextmenu

我在每个ListView中使用ContextMenu在一个活动中多个ListView

实际上它应该适用于所有7个ListView,但它仅适用于Seventh List

这是我的代码。

ArrayAdapter<Course> adapter;
for(int i=0;i<7;i++){
        courses = db.findFiltered(String.format("day == %d ", i), "startTime ASC");
        ListView lv1 = (ListView) findViewById (arrayList[i]);
        registerForContextMenu(lv1);
        lv1.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
            }
        });
        adapter = new CourseListAdapter(this, courses);
        lv1.setAdapter(adapter);
    }

@Override
public void onCreateContextMenu(android.view.ContextMenu menu, View v, ContextMenuInfo        menuInfo)    
{
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.contextmenu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
 case R.id.editItem:
//do something 
return true;
 case R.id.showItem:
//do something
 return true;
 case R.id.deleteItem:
     db.removeCourse(adapter.getItem(info.position);
     adapter.remove(adapter.getItem(info.position));

 return true;
 default:
     return super.onContextItemSelected(item);
     }
}

请帮助

1 个答案:

答案 0 :(得分:1)

Adapter实例设置为最后一次迭代ListView对象。这是快速的代码更改。试试这个:

private ArrayAdapter<Course> selectedListViewAdapter;

...

for(int i=0;i<7;i++){
        courses = db.findFiltered(String.format("day == %d ", i), "startTime ASC");
        ListView lv1 = (ListView) findViewById (arrayList[i]);
        registerForContextMenu(lv1);

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

        ArrayAdapter<Course> adapter = new CourseListAdapter(this, courses);
        lv1.setAdapter(adapter);
    }

@Override
public void onCreateContextMenu(android.view.ContextMenu menu, View v, ContextMenuInfo        menuInfo)    
{
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.contextmenu, menu);

    try {
        ListView selectedListView = (ListView)v;
        selectedListViewAdapter = (ArrayAdapter<Course>)selectedListView.getAdapter();
    } catch(ClassCastException e) {
    }
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    switch (item.getItemId()) {
     case R.id.editItem:
        //do something 
        return true;
     case R.id.showItem:
        //do something
        return true;
     case R.id.deleteItem:
        if(null != selectedListViewAdapter) {
            db.removeCourse(selectedListViewAdapter.getItem(info.position);
                selectedListViewAdapter.remove(selectedListViewAdapter.getItem(info.position));
        }

     return true;
     default:
         return super.onContextItemSelected(item);
         }
}