0

我創建使用SimpleCursorAdapter列表視圖,在列表中登記的上下文菜單...更新列表項onContextItemSelected

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

你可以從選定項目索引該清單通過致電

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 
+0

什麼'lv.getSelectedItem();'將返回? – dakait

+0

在您的適配器中,您設置了ArrayAdapter ,因此getSeletedItem將返回選定索引處的項目。 –

+0

即時通訊不使用ArrayAdapter我的適配器看起來像'public class MyListAdapter extends SimpleCursorAdapter {'獲取項目後,我如何獲取文本視圖? – dakait