Search code examples
javaandroidarrayshashmapandroid-drawable

How do i compare the ID of my HashMap to my spinner value


So if you see the code below, I have 3 arrays: one stores images, one stores values of my spinner and another stores the hashMap with the values of my images and the values of my spinner.

What I want to do is when I create an image by pressing my button say a husky image shows and when I select husky from my spinner it compares the two id's and if they are the same it will launch any code in the if statement.


Solution

  • Please use the below code snippet to check image view id with hashmap key and spinner text with hashmap value.

    public class MainActivity extends Activity {
            private TextView tex_view;
    
            private Spinner mDogs;
            private ImageView mImageView;
            private Button mButton;
            private Random r;
            private TextView mTextView;
            private TextView mTextView2;
    
            //image arrays
            final Integer[] images = {R.drawable.home, R.drawable.mobile, R.drawable.sports,
                    R.drawable.toys};
    
            final String[] dogs = {"Select Item", "husky", "kuvasz", "papillon", "vizsla"};
    
            Map<Integer, String> answers = new HashMap<Integer, String>() {{
                put(R.drawable.home, "husky");
                put(R.drawable.mobile, "kuvasz");
                put(R.drawable.sports, "papillon");
                put(R.drawable.toys, "vizsla");
            }};
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                tex_view = findViewById(R.id.tex_view);
                mDogs = (Spinner) findViewById(R.id.spinner);
                mImageView = (ImageView) findViewById(R.id.pic1);
                mButton = (Button) findViewById(R.id.button);
    
                ArrayAdapter<String> ad1 = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, dogs);
    
                mDogs.setAdapter(ad1);
    
                mDogs.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                        if (!mDogs.getSelectedItem().toString().equalsIgnoreCase("Select Item")) {
                            String imageID = String.valueOf(mImageView.getTag()); // imageView id
                            String selectedSpinnerText = mDogs.getSelectedItem().toString(); //selected Spinner text
                            for (Object name : answers.keySet()) {
                                String key = name.toString();
                                String value = answers.get(name);
                                if (selectedSpinnerText.equalsIgnoreCase(value) && imageID.equalsIgnoreCase(key)) {
                                    tex_view.setText("Both image and Spinner text matches");
                                    return;
                                } else {
                                    tex_view.setText("Both image and Spinner text not matches");
                                }
                            }
                        }
                    }
    
                    @Override
                    public void onNothingSelected(AdapterView<?> adapterView) {
    
                    }
                });
    
                r = new Random();
                mButton.setOnClickListener(new View.OnClickListener() {
    
                    @Override
                    public void onClick(View view) {
                        //display random images
                        mButton.setText("Next");
                        int pos = r.nextInt(images.length);
                        mImageView.setImageResource(images[pos]);
    
                        mImageView.setTag(images[pos]);
    
    
                    }
                });
    
            }
        }