1

ExpandableListActivityにはContextMenuが登録されています。私がしようとしているのは、ContextMenuが押されたグループの子リスト項目のデータを保存することです。 によると:ExpandableListActivityでのContextMenuのトラブル

onCreateContextMenu(ContextMenu menu, View v, 
      ContextMenuInfo menuInfo) 

vは、コンテキストメニューが構築されている図です。だから、このビューは、私がクリックするリスト項目のものでなければなりませんが、それは子リストの最初のリスト項目を参照しています。私はそれがコンテキストメニューが構築されているリスト項目のビューを返す必要があると信じていますが、ここではそうではありません。ここに私のコードです:

public void onCreateContextMenu(ContextMenu menu, View v, 
      ContextMenuInfo menuInfo) { 
     menu.setHeaderTitle("My Crumbs"); 

     TextView rowid = (TextView) v 
       .findViewById(R.id.trackdetails_item_row_id); 
     rowId = rowid.getText().toString(); 

     ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) menuInfo; 
     int type = ExpandableListView 
       .getPackedPositionType(info.packedPosition); 

     // Only create a context menu for the child 
     if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) { 

      TextView trackstats = (TextView) v 
        .findViewById(R.id.trackdetails_item_stats); 
    menu.add(0, MENU_SHARE, 0, "Share on Facebook"); 
     } 

    } 

誰かがこれにいくつかの光を当てることができますか?

編集:

コードExpandableListAdapter用:

public class MyExpandableListAdapter extends SimpleCursorTreeAdapter { 

     public MyExpandableListAdapter(Cursor cursor, Context context, 
       int groupLayout, int childLayout, String[] groupFrom, 
       int[] groupTo, String[] childrenFrom, int[] childrenTo) { 
      super(context, cursor, groupLayout, groupFrom, groupTo, 
        childLayout, childrenFrom, childrenTo); 
      setViewBinder(viewBinder); 
     } 

     @Override 
     protected Cursor getChildrenCursor(Cursor groupCursor) { 
      // TODO Auto-generated method stub 
      String crumbName = groupCursor.getString(mCrumbNameColumnIndex); 
      return crumpareDBAdapter.getTrackList(mTracksProjection, crumbName); 
     } 

     @Override 
     public SimpleCursorTreeAdapter.ViewBinder getViewBinder() { 
      return viewBinder; 
     } 

    } 

ViewBinderのためのコード:

SimpleCursorTreeAdapter.ViewBinder viewBinder = new ViewBinder() { 

     @Override 
     public boolean setViewValue(View view, Cursor cursor, int columnIndex) { 
      // TODO Auto-generated method stub 
     TextView textView = (TextView) view; 
     textView.setText(cursor.getString(columnIndex)); 
      return true; 
     } 
    }; 
+0

アダプタでリスト項目をリサイクルするときに問題が発生することがあります。あなたもそのコードを共有していただけますか?ありがとう! – rekaszeru

+0

ここに私が使用しているアダプタのコードがあります。助けてくれてありがとうございます – rogerstone

+0

あなたの 'viewBinder'には何が定義されていますか?あなたは 'BaseExpandableListAdapter'実装を使うことができました。それはかなり明確になります – rekaszeru

答えて

1

あなたにもContextMenuInfoから子供のIDを取得することができその視点に頼るよりむしろ。それがあなたが望むものを持っている必要があるので、documentationを参照してください。

+0

私は何をしたいのですか?私はこのメソッドで何が間違っているのか知りたかったのです – rogerstone

+0

私はgetExpandableListView.getChildAt(index)[index = childpos]を使って対応するビューを取得していましたが、私はそれが私が望んだものを与えていたと思ったが、私はいつもそうではないと思う:) IDはまさに私が使用していたはずだったはずだった。さらに、contextMenuinfoからのtargetviewは、これらのどれもが私にとって便利なものであることが証明されています。たくさんありがとう。 – rogerstone

+1

拡張可能なリスト内のビューを_inflating_するための標準的なメカニズムを使用している場合、それはおそらく問題です。彼らはリスト内のビューを最初に膨らませ、その後のアイテムのビューを再利用します。したがって、取得するビューには、ほとんどの場合、必要なデータは含まれません。 –