我有列表視圖中可用的聯繫人列表現在當用戶點擊列表我想顯示編輯選項,因爲我是新來的android我不知道我應該在哪個事件應該編寫代碼。哪個事件應該使用TextView或ListView事件
我試着用listview事件作爲setOnItemClickListener,但它給了我錯誤,當我嘗試使用建議的選項,但它仍然顯示錯誤,請幫助我。
我有列表視圖中可用的聯繫人列表現在當用戶點擊列表我想顯示編輯選項,因爲我是新來的android我不知道我應該在哪個事件應該編寫代碼。哪個事件應該使用TextView或ListView事件
我試着用listview事件作爲setOnItemClickListener,但它給了我錯誤,當我嘗試使用建議的選項,但它仍然顯示錯誤,請幫助我。
使用這樣的事情:
// In OnCreate() call this
registerForContextMenu(yourlistview);
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo)
{
menu.setHeaderTitle("Choose an option");
menu.add(0, 0, 0, "Edit");
menu.add(0, 1, 0, "Something Else");
super.onCreateContextMenu(menu, v, menuInfo);
}
public boolean onContextItemSelected(MenuItem item)
{
AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId())
{
case 0:
ListView listView = (ListView)findViewById(R.id.yourlist);
// In my example i've used a string. Do not use copy pasta. yeah ?
String S = (String) listView.getAdapter().getItem(menuInfo.position);
這是一個context menu。閱讀它。你會明白的。
謝謝,但現在我面臨與RegisterForContextMenu(Textview)的問題..我顯示的聯繫人列表,當用戶長按下它,我想顯示上下文菜單。但在調試時,我觀察到我的應用程序崩潰,因爲它涉及到上面的行請幫助我.. – Neha 2011-03-08 09:27:32
Oi Neha [閱讀文檔](http://developer.android.com/guide/topics/ui/menus.html)。 registerForContextMenu(lv)將listview作爲輸入。不要盲目使用代碼。 – Reno 2011-03-08 09:30:25
ohh謝謝,但仍然無法解決問題,因爲所選聯繫人的想要的id該怎麼做,請引導我。 – Neha 2011-03-08 09:53:07
我不明白你的問題你想要什麼請詳細描述然後我希望我會給你一個完美的答案。 – DynamicMind 2011-03-07 10:52:14