私はアイスクリームのフレーバーのリストを持つプロジェクトに取り組んでいます。フレーバーを長押しすると、写真、材料、価格などのオプションを含むコンテキスト メニューが表示されます。チョコレートを長押しして画像を選択すると、チョコレート アイス クリームの画像が表示されます。バニラでも同じことを行うと、バニラ アイス クリームが表示されます。
これが問題です。コンテキスト メニュー内で行う選択は、長押しした元のフレーバーに関連しています。フレーバー配列内のフレーバーの位置にアクセスできる必要があります。-- OnListItemClicked() メソッドでは、フレーバー配列の位置にアクセスできますが... OnContextItemSelect() メソッドは、独自の選択項目 (picture,ingredient, price ) のみを認識します。
listPosition 変数を作成したことがわかりますが、これは、OnContextItemSelect() を使用して長押しするのではなく、OnListItemClicked() を使用してすばやくフレーバーをクリックした場合にのみ機能することに気付きました。
ApplyMenuChoice() は、問題のコードが見つかる場所です。
私は助けてくれてとても感謝しています、ありがとう:)
MenuDemo.java
public class MenuDemo extends ListActivity {
TextView selection;
TextView displayText;
String[] flavorItems;
String[] colors;
ImageView myImage;
int currentMenu;
int listPosition = 0;
int[] ItemImageSource = {R.drawable.vanilla,R.drawable.chocolate,R.drawable.strawberry};
private final String TAG = "Main Activity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
selection=(TextView)findViewById(R.id.selection);
displayText=(TextView)findViewById(R.id.displayText);
myImage=(ImageView)findViewById(R.id.myImage);
flavorItems = getResources().getStringArray(R.array.flavors);
colors = getResources().getStringArray(R.array.colors);
setListAdapter(new ArrayAdapter<String>(this, R.layout.flavor_menu_style, flavorItems));
registerForContextMenu(getListView());
displayText.setText("Welcome! Click and hold flavor to learn more about it.");
}
public void onListItemClick(ListView parent, View v,int position, long id)
{
if (currentMenu == 1){selection.setText(colors[position]);}
else {selection.setText(flavorItems[position]);}
String strI = Integer.toString(position);
displayText.setText("teeesst" + strI);
listPosition = position;
Log.i(TAG,"!!!!!!!!!!!!!!");
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_menu_demo, menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_new, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) { //item is the menu item chosen
boolean ret = applyOptionMenuChoice(item);
if ( ret == false)
ret = super.onOptionsItemSelected(item);
return ret;
}
@Override
public boolean onContextItemSelected(MenuItem item)
{
boolean ret = applyMenuChoice(item);
if ( ret == false)
ret = super.onContextItemSelected(item);
return ret;
}
private boolean applyMenuChoice(MenuItem item) {
switch (item.getItemId())
{
case R.id.picture:
//this would work if listPosition was linked to the flavors array position
myImage.setImageResource(ItemImageSource[listPosition]);
return(true);
case R.id.ingredients:
String [] ingredients;
ingredients = new String [3];
ingredients[0] = "VANILLA -- these are the ingredients VANILLA";
ingredients[1] = "CHOCOLATE -- these are the ingredients";
ingredients[2] = "STRAWBERRY -- these are the ingredients";
displayText.setText(ingredients[listPosition]);
return(true);
case R.id.order:
//I will add stuff here later
return(true);
case R.id.price:
//I will add stuff here later
return(true);
}
return(false);
}
private boolean applyOptionMenuChoice(MenuItem item) {
switch (item.getItemId())
{
case R.id.color_menu:
setListAdapter(new ArrayAdapter<String>(this, R.layout.color_menu_style, colors));
currentMenu = 1;
return(true);
case R.id.flavor_menu:
setListAdapter(new ArrayAdapter<String>(this, R.layout.flavor_menu_style, flavorItems));
currentMenu = 2;
return(true);
}
return(false);
}
}