2011-11-08 3 views
1

G'day、
さまざまなテキスト値が設定されたListViewがあります。長い間コンテキストメニューを押して開いたときに、あなたが長押ししたListItem。これまでのところ私は、「コピー」オプションをポップアップ表示するコンテキストメニューを持っている:アイテムのIDがクリックされました - Android ContextMenu

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) 
{ 
    //this was following another question but I don't know what to do with it 
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; 
    long selectedId = info.id; 
    super.onCreateContextMenu(menu, v, menuInfo); 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.context, menu); 
} 
public boolean onContextItemSelected(MenuItem item) 
{ 
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); 
    switch (item.getItemId()) 
    { 
    case R.id.copy: 
     //used to be in a function but wasn't sure about views 
     //yes I know it's depreciated but it works ;) 
     ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
     TextView clicked = (TextView)this.findViewById(???); 
     clipboard.setText(clicked.getText()); 
     Context context = getApplicationContext(); 
     Toast copied = Toast.makeText(context, "Story copied to clipboard.", Toast.LENGTH_LONG); 
     copied.show(); 
     return true; 
    default: 
     return super.onContextItemSelected(item); 
    } 
} 

おかげ

+1

問題がありますか? –

+0

@Chirag私はどこに置くべきかわからない???です。長押ししたアイテムのIDを渡すことができたら、ちょうど... findViewById(id); – ProfSmiles

答えて

0

がクリックされますビューを保持する変数を設定します。

View clicked; 

コンテキストメニューがその上に作成されたときに、それに値を割り当てる:

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) 
{ 
    clicked = v; 

    //this was following another question but I don't know what to do with it 
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; 
    long selectedId = info.id; 
    super.onCreateContextMenu(menu, v, menuInfo); 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.context, menu); 
} 

をそして今、あなたはあなたの最終的に会っそれを使用することができますhod:

public boolean onContextItemSelected(MenuItem item) 
{ 
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); 
    switch (item.getItemId()) 
    { 
    case R.id.copy: 
     //used to be in a function but wasn't sure about views 
     //yes I know it's depreciated but it works ;) 
     ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 

     clipboard.setText(clicked.getText()); 
     // this should work now properly. 

     Context context = getApplicationContext(); 
     Toast copied = Toast.makeText(context, "Story copied to clipboard.", Toast.LENGTH_LONG); 
     copied.show(); 
     return true; 
    default: 
     return super.onContextItemSelected(item); 
    } 
} 
+0

これを置くと、Eclipseはエラーだと言ってitem.getTitle()に変更しますが、クリップボードに「コピー」(私がタッチしたコンテキストメニューのタイトル)を置くだけですが、私が元々長押ししたListItemにあったもの – ProfSmiles

+0

申し訳ありませんが、私の悪い。 'onCreateContextMenu'が呼び出されると、適切なビューを変数に格納することができます。私は私の答えを編集します。 –

+0

上記のコードを使用すると、 'clicked.getText()'は明らかにエラーです。「メソッドgetText()は型Viewに対して未定義です」 – ProfSmiles

0

私はあなた自身の質問に答えたと思います。 ID:

long selectedId = info.id; 
関連する問題