如何使我的上下文菜单根据单击的项目显示不同的图像

时间:2013-09-30 03:16:45

标签: android imageview contextmenu

我是Android编程的新手,我正在制作一个使用上下文菜单的简单应用程序。主屏幕显示可供选择的菜单,当您长按项目时,您会看到上下文菜单,其中包含您可以采取的操作列表:图片,成分,订单,价格。 -

问题在于:无论点击哪个项目,都可以轻松设置上下文菜单以显示相同的图片或显示相同的成分,但是当我长按巧克力时,我希望我的应用程序显示巧克力图像选择图片 - 然后当我长按香草并选择图片时显示香草的图像。我想不出怎么做才能让setImage根据选择的项目获取可互换的图像路径。我会非常感谢任何指针或解决方案!

转到 applyMenuChoice()中的切换语句,查看我遇到的位置

MenuDemo.Java

public class MenuDemo extends ListActivity {
    TextView selection;
    TextView displayText;
    String[] flavorItems;
    String[] colors;
    ImageView myImage;
    int currentMenu;

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

        listPosition = position;
    }

    @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:
        //myImage.setImageResource(ItemImageSource[0]);
            //setImageDrawable(getResources().getDrawable(R.drawable.+listPosition ));
            return(true);

        case R.id.ingredients:
            //ingredients action will go here
            return(true);

        case R.id.order:
            getListView().setDividerHeight(8);
            //order action will go here

        case R.id.price:
            //price action will go here
            return(true);

        }

        return(false);

    }

    private boolean applyOptionMenuChoice(MenuItem item) {
        switch (item.getItemId()) 
        {
        case R.id.color_menu:
            Log.i(TAG,"!!!!!!!!!!!!!!");
            setListAdapter(new ArrayAdapter<String>(this, R.layout.color_menu_style, colors));
            currentMenu = 1;
            return(true);

        case R.id.flavor_menu:

            Log.i(TAG,"!!!!!!!!!!!!!!");
            setListAdapter(new ArrayAdapter<String>(this, R.layout.flavor_menu_style, flavorItems));
            currentMenu = 2;
            return(true);

        }

        return(false);

    }


}

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:background="@color/backgroundColor"

        >

        <TextView
                android:id="@+id/selection"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#ffffffff"
                android:minHeight="40dp"
                android:paddingLeft="6dip"
                android:gravity="center_vertical"
                android:textStyle="bold"
        />

        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:drawSelectorOnTop="false" />

        <ImageView
            android:id="@+id/myImage"
            android:layout_width="242dp"
            android:layout_height="188dp"
            android:adjustViewBounds="true"
            android:src="@drawable/icecream_1"
            android:layout_gravity="center"
         />

         <TextView
                android:id="@+id/displayText"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#ffffffff"
                android:padding="20dp" 
                android:fontFamily="sans-serif-light"
                android:textStyle="normal|bold|italic"
                android:drawableLeft="@drawable/ice2"
        />


</LinearLayout>

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以根据与用户选择相关的变量(或共享偏好)的值更改菜单的图片(例如,布尔选择_巧克力,choice_vanilla ......):

/*this integer will capture what flavor the user clicked on*/
int listPosition=-1;//global scope, assign a default value

public void onListItemClick(ListView parent, View v,int position, long id) 
{   

    if (currentMenu == 1){selection.setText(colors[position]);}
    else {selection.setText(flavorItems[position]);}

    listPosition = position;
}



private boolean applyMenuChoice(MenuItem item) {
 switch (item.getItemId()) 
 {
  case R.id.picture:
   if(listPosition!=-1){
    myImage.setImageResource(ItemImageSource[listPosition]);
   }
   else{
    myImage.setImageResource(error_drawable_image);
   }
        //setImageDrawable(getResources().getDrawable(R.drawable.+listPosition ));
        return(true);
 }
}