更新列表项onContextItemSelected

时间:2013-12-16 08:42:00

标签: android android-listview simplecursoradapter android-cursoradapter

我正在使用SimpleCursorAdapter创建列表视图,其中列表已为上下文菜单注册...

 registerForContextMenu(listView);

下面是onContextItemSelection

@Override
public boolean onContextItemSelected(MenuItem item) {

switch (menuItemIndex) {
    case 0:{
        //perform some action 
                //then update the adapter   
                Cursor newCursor = fetchNewCursor();
         if(newCursor .moveToFirst()){
          adapter.swapCursor(newCursor ); 
          adapter.notifyDataSetChanged();
                 }//if ends             
        }// case 0 ends             
        break;
 }//switch ends
}//onContextItemSelected() ends

这会更新我的整个列表视图,我想添加另一个context menu条目,我只想更新(长)单击的特定列表项。我怎么能这样做......

这就是我Cursor Adapter的样子

@Override
    public View newView (Context context, Cursor cursor, ViewGroup parent) {
            return viewInflater.inflate(layout, null);
    }

bindView方法

@Override
    public void bindView(View view, Context context, Cursor cursor) {
        super.bindView(view, context, cursor);

        TextView t1=(TextView)view.findViewById(R.id.txt_one);
        TextView t2=(TextView)view.findViewById(R.id.txt_two);

        t1.setText(cursor.getString(...));
        t2.getString(cursor.getString(...));
}

问候

1 个答案:

答案 0 :(得分:1)

您可以通过调用

从列表中获取所选项目索引
lv.getSelectedItemPosition();

然后更新该位置的列表项中的条目并通知适配器:

@Override
public boolean onContextItemSelected(MenuItem item) {

switch (menuItemIndex) {
    case 0:{
        //perform some action 
                //then update the adapter   
                Cursor newCursor = fetchNewCursor();
         if(newCursor .moveToFirst()){
          adapter.swapCursor(newCursor ); 
          adapter.notifyDataSetChanged();
                 }//if ends             
        }// case 0 ends             
        break;
    case 1:
        <ItemType> item=lv.getSelectedItem();
        <UPdateItem>
        adapter.notifyDataSetChanged();
        break; 
 }//switch ends
}//onContextItemSelected() ends