public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);
        
        mDbHelper = new DiaryDbAdapter(this);
        mDbHelper.open();
        renderListView();
        ListView mylistView = getListView();
        mylistView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
            @Override
            public void onCreateContextMenu(ContextMenu menu, View v,
                    ContextMenuInfo menuInfo) {
                menu.setHeaderTitle("选择操作");
                menu.add(0, 0, 0, "更新该条");
                menu.add(0, 1, 0, "删除该条");
            }
        });    
    }
    

listview实现长按弹出上下文菜单。要注意

<ListView android:id="@+id/android:list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

中的id是@+id/android:list,这个并不在R文件中产生对应的id。故findViewById方法不可取。一个Activity中就只有一个ListView,所以setListAdapter(notes)方法并不需要指定对哪一个listview使用。

ListView mylistView = getListView();/*Get the activity's list view widget*/

相关文章:

  • 2022-01-12
  • 2022-01-19
  • 2021-11-17
  • 2022-12-23
  • 2022-12-23
  • 2021-12-22
  • 2022-12-23
猜你喜欢
  • 2021-10-07
  • 2021-06-23
  • 2021-06-08
  • 2021-05-11
  • 2022-12-23
相关资源
相似解决方案