2011-11-08 50 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;