0

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

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

转到applyMenuChoice()中的 switch 语句,看看我卡在哪里

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

    }


}

主.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>

在此处输入图像描述

4

1 回答 1

0

您可以根据与用户选择相关的变量(或共享首选项)的值(例如 boolean choice_chocolate、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);
 }
}
于 2013-09-30T03:34:43.383 回答