2017-01-09 25 views
0

私はListViewカスタムビューを表示しています(アイテムあたり3 TextViews)。私はちょうどそのようなのようなコンテキストメニューを実装している:アイテムの位置を取得ListViewにLongClicked

// in onCreateView 
ListView list = (ListView) view.findViewById(R.id.list); 

registerForContextMenu(list); 

@Override 
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { 
    super.onCreateContextMenu(menu, v, menuInfo); 

    if (v.getId() == R.id.list) { 
     MenuInflater inflater = getActivity().getMenuInflater(); 
     inflater.inflate(R.menu.todo_context_menu, menu); 
    } 
} 

@Override 
public boolean onContextItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.edit: 
      // your first action code 
      return true; 
     case R.id.delete: 
      // your second action code 
      return true; 
     default: 
      return super.onContextItemSelected(item); 
    } 
} 

私は長い編集するために(ListViewでその位置が理想的である)がクリックされた項目知りたいですか正しいものを削除してください。

どのように達成できますか?

+0

簡単な検索は長い道のり行く:http://stackoverflow.com/a/18632538/1269953 – Pztar

+0

私はこのスレッドを見ましたが、私はあなたに感謝し、それは私の頭の上に –

答えて

0
@Override 
public boolean onContextItemSelected(MenuItem item) { 

    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); 
    int index = info.position; 

    switch (item.getItemId()) { 
    case R.id.edit: 
     // your first action code 
     return true; 
    case R.id.delete: 
     // your second action code 
     return true; 
    default: 
     return super.onContextItemSelected(item); 
    } 
} 
+1

ワンダフルを行ったと思い、私は受け入れますあなたの答えはできるだけ早く。 –