如何从contextMenu访问ListItem位置

时间:2013-10-03 21:48:19

标签: android contextmenu listactivity

我正在开展一个有冰淇淋口味清单的项目。当您长按一个风味时,您会看到一个上下文菜单,其中包含一些选项:图片,成分,价格等。如果长时间点击巧克力,你选择图片,你应该看到巧克力冰淇淋的图片...如果你对香草做同样的事情,你会看到香草冰淇淋。

这是问题所在。您在上下文菜单中所做的选择与您长按的原始风格相关。我需要能够访问flavor数组中的flavor的位置。 - 在OnListItemClicked()方法中,我的flavors数组的位置是可访问的,但是...... 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);

    }


}

1 个答案:

答案 0 :(得分:3)

您可以通过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
    }