-4

I don't know what to compare in my if statements for my order.setOnClickerListener to get my code working properly. I'm trying to setText based on which image is selected from the array when the button is pressed.

Right now, when the button is pressed, I am trying to look at each image in turn to decide whether or not the user selected it. However, I'm not sure how I can tell whether the user selected a particular image or not. What should I replace the **** with to determine that?

public class Breakfast extends AppCompatActivity {

    Integer[]Foods = {R.drawable.pancakes, R.drawable.frenchtoast, R.drawable.waffles,
            R.drawable.omelet};
    ImageView pic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setLogo(R.mipmap.ic_launcher);
        getSupportActionBar().setDisplayUseLogoEnabled(true);

        setContentView(R.layout.activity_breakfast); GridView grid = (GridView) findViewById(R.id.gridView);
        final ImageView pic = (ImageView) findViewById(R.id.imgLarge);
        final TextView food = (TextView) findViewById(R.id.txtFood);
        Button back = (Button) findViewById(R.id.btnBack);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Breakfast.this, MainActivity.class));
            }
        });

        final Button order = (Button) findViewById(R.id.btnOrder);
        order.setEnabled(false);
        order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Foods[0] == ****){
                    Toast.makeText(getBaseContext(), "Pancakes added to your order", Toast.LENGTH_SHORT).show();
                    food.setText("You've selected Pankcakes");}
                if(Foods[1] == ****){
                    Toast.makeText(getBaseContext(), "French Toast added to your order", Toast.LENGTH_SHORT).show();
                    food.setText("You've selected French Toast");}
                if(Foods[2] == ****){
                    Toast.makeText(getBaseContext(), "Waffles added to your order", Toast.LENGTH_SHORT).show();
                    food.setText("You've selected Waffles");}
                if(Foods[3] == ****){
                    Toast.makeText(getBaseContext(), "Omelet added to your order", Toast.LENGTH_SHORT).show();
                    food.setText("You've selected Omelet");}

            }
        });

        grid.setAdapter(new ImageAdapter(this));
        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                pic.setImageResource(Foods[position]);
                order.setEnabled(true);
            }
        });
    }
}

There's a little more code but I don't think it's relevant.

2 Answers2

1

Use multiple (4 in your case), ImageView in your layout XML:

<ImageView
    android:id="@+id/pancakes"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/pancakes"/>

<ImageView
    android:id="@+id/frenchtoast"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/frenchtoast"/>

<ImageView
    android:id="@+id/waffles"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/waffles"/>

<ImageView
    android:id="@+id/omelet"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/omelet"/>

Then, add a listener to all of them in your activity and keep track of the image clicked by updating the imageSelected variable:

public class Breakfast extends AppCompatActivity implements View.OnClickListener {
    private int imageSelected;
    ImageView pancakes;
    ImageView frenchtoast;
    ImageView waffles;
    ImageView omelet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // ... your code

        pancakes.setOnClickListener(this);
        frenchtoast.setOnClickListener(this);
        waffles.setOnClickListener(this);
        omelet.setOnClickListener(this);

        // ... more code
    }



    @Override
    public void onClick(View v) {
        imageSelected = v.getId();
    }

}

And this is the OnClickListener of your order Button:

order.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (imageSelected == pancakes.getId()) {
                Toast.makeText(getBaseContext(), "Pancakes added to your order", Toast.LENGTH_SHORT).show();
                food.setText("You've selected Pankcakes");
            }

            if (imageSelected == frenchtoast.getId()) {
                Toast.makeText(getBaseContext(), "French Toast added to your order", Toast.LENGTH_SHORT).show();
                food.setText("You've selected French Toast");
            }

            if (imageSelected == waffles.getId()) {
                Toast.makeText(getBaseContext(), "Waffles added to your order", Toast.LENGTH_SHORT).show();
                food.setText("You've selected Waffles");            
            }

            if (imageSelected == omelet.getID()) {
                Toast.makeText(getBaseContext(), "Omelet added to your order", Toast.LENGTH_SHORT).show();
                food.setText("You've selected Omelet");             
            }
        }
    });
Jorge E. Hernández
  • 2,800
  • 1
  • 26
  • 50
0

I don't think that you want to do this the way you're trying to do it (i.e. you don't want to just replace the **** in your code sample with some condition and call it a day).

You have a few options here (and there are lots of code samples available online). One is to use either a HorizontalScrollView or a ListView. This has the advantage of making it easier to change what images you're displaying later (as opposed to hardcoding it, which would require a code update and redeploy every time you changed what image was being displayed).

There's also a Gallery class, but it's deprecated.

If you definitely want to hardcode the values and are OK with the potential maintenance and deployment problems that this could cause later, you can try to use an ImageButton for each. From the documentation, an ImageButton

Displays a button with an image (instead of text) that can be pressed or clicked by the user. By default, an ImageButton looks like a regular Button, with the standard button background that changes color during different button states. The image on the surface of the button is defined either by the android:src attribute in the XML element or by the setImageResource(int) method.

@lalongooo shows a code sample for how to do something similar to this in his answer.

There's also a tutorial here on creating a ListView with check boxes. I haven't gone through the tutorial that carefully yet, but the image of what they're trying to implement looks pretty nice ad it may suit your purposes. You may want to try implementing that or something similar. (I searched the phrase "check box list view android" in Bing to find that if you're curious or want to find more tutorials/examples on the same topic).