我在理解Android上下文菜單的工作方式方面遇到了很多麻煩。我正在編寫一個簡單的程序,它將在列表視圖中顯示不同類型冰淇淋的列表。當用戶「點擊並按住每種類型時,上下文菜單應該出現4個選項,詢問他們想要顯示有關該冰淇淋的信息(圖片,配料,訂單等)。如何使用Android上下文菜單(點擊並按住)
我不瞭解如何使每個列表項的上下文菜單提供相應的信息(與用戶點擊並保持的列表項相關的信息)
我正在從我的老師給出的一個例子中工作,它有一些我看到在給定的代碼中,我的教授甚至在onCreateContextMenu()的參數中給出了一條提示:
public void onCreateContextMenu(//called each time the context menu is requested
ContextMenu menu, //the ContextMenu itself
View v, //The view for which the context menu is being built
ContextMenu.ContextMenuInfo menuInfo) { //tells you which item in the list the user did the tap-and-hold over
但是我仍然不確定自己能夠用這個參數做什麼,而且我現在輪了很長一段時間。在stackoverflow上至少有一個線程解決了這個問題,但它並沒有幫助我更接近理解這裏發生的事情。
我真的很感謝這個幫助,因爲我是Android的新手(一般是OOP),並且由於害怕公開技術人員嘲笑或被視爲利用社區而提出問題有點猶豫。我只希望在某個時候,我可以把錢付給其他人。
謝謝!
這裏是我的代碼:
package CS285.Assignment2;
import android.app.Activity;
import android.os.Bundle;
import android.app.ListActivity;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class IceCreamMenu extends ListActivity {
TextView selection;
ImageView imgView;
String[] items={"Butter Pecan", "Chocolate",
"Coffee", "Mango",
"Rocky Road","Strawberry",
"Vanilla"};
int[] images={R.drawable.butterpecan,
R.drawable.choclate,
R.drawable.coffee,
R.drawable.mango,
R.drawable.rockyroad,
R.drawable.strawberry,
R.drawable.vanilla};
public static final int IMG_ID = 0;
public static final int INGR_ID = 1;
public static final int ORDER_ID = 2;
public static final int OTHER_ID = 3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
selection=(TextView)findViewById(R.id.selection);
imgView = (ImageView)findViewById(R.id.Image);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
items));
registerForContextMenu(getListView()); //indicate that the ListView has a context menu.
}
//event handling for ListItemClick events
public void onListItemClick(ListView parent, View v,
int position, long id) {
selection.setText(items[position]);
}
@Override
public void onCreateContextMenu(//called each time the context menu is requested
ContextMenu menu, //the ContextMenu itself
View v, //The view for which the context menu is being built
ContextMenu.ContextMenuInfo menuInfo) { //tells you which item in the list the user did the tap-and-hold over
populateMenu(menu);
}
private void populateMenu(Menu menu) {
menu.add(Menu.NONE, IMG_ID, Menu.NONE, "Picture");
menu.add(Menu.NONE, INGR_ID, Menu.NONE, "Ingredients");
menu.add(Menu.NONE, ORDER_ID, Menu.NONE, "Order");
menu.add(Menu.NONE, OTHER_ID, Menu.NONE, "Other");
}
private boolean applyMenuChoice(MenuItem item) {
switch (item.getItemId()) {
case IMG_ID:
imgView.setImageResource(images[0]);
return(true);
case INGR_ID:
return(true);
case ORDER_ID:
return(true);
case OTHER_ID:
return(true);
}
return(false);
}
//called whenever an item in a ContextMenu is selected.
@Override
public boolean onContextItemSelected(MenuItem item) { //item is the menu item chosen
return(applyMenuChoice(item) ||
super.onContextItemSelected(item));
}
}