2013-10-03 23 views
1

我正在研究一個具有冰淇淋風味列表的項目。當你長時間點擊一種風味時,你會看到一個帶有一些選項的上下文菜單:圖片,配料,價格等。如果長時間點擊巧克力並選擇圖片,您應該看到巧克力冰淇淋的照片......如果您對香草做了同樣的事情,您會看到香草冰淇淋。如何從contextMenu訪問ListItem位置

這是問題所在。您在上下文菜單中進行的選擇與您長按的原始味道有關。我需要能夠訪問flavor數組中的flavor的位置。 - 在OnListItemClicked()方法中,我的flavor數組的位置是可訪問的,但是... OnContextItemSelect()方法只知道它自己的選擇項(圖片,成分,價格)。

你可以看到我創造了一個listPosition變量,但我意識到,如果被點擊風味迅速使用OnListItemClicked(),而不是長期使用點擊OnContextItemSelect()這隻會工作。

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); 

    } 


} 

回答

2

你可以通過MenuItem

@Override 
    public boolean onContextItemSelected(MenuItem item) { 
     AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); 
     int listPosition = info.position; 
     //pass this to your applyMenuChoice method. ice cream nom nom nom 
    } 
你長按列表項的位置
+0

Awsome!這只是一個小修改。我真的很感謝你的幫助,我很清楚android編程,而這本來很難發現。 只是爲了防止其他人看到這個例子,唯一的變化是我們正在查看的方法是onContextItemSelected(MenuItem項)而不是onOptionsItemSelected(MenuItem項)。 – ThisBetterWork

+0

@ThisBetterWork - 哎呀。修正了:) – dymmeh