-2

I've created three imageviews in the following way: ImageView imagev = new ImageView(getActivity());

Now whenever I click on these (each one of them) I want to load some information from string array. The thing is that these imageviews were created using another array that holds the drawables.

So my question is, how I can make onclick without knowing the id of these buttons that was, as far as I know randomly generated, while knowing that it holds of drawable (one of many)? I was considering the attempt to nest these imageviews, each one of them in linearlayout and attach onclicklistener on them, but it seems non efficient nor elegant attempt.

Edit:

    public View onCreateView(LayoutInflater inflater,
                                 ViewGroup container,
                                 Bundle savedInstanceState){


            View rootView = inflater.inflate(R.layout.majoraracane, container, false);
            ImageView cardpicker = (ImageView)rootView.findViewById(R.id.cardpick);
            ImageView imagev = new ImageView(getActivity());

            cardpicker.setOnClickListener(this);
            Resources resources = getResources();
            imagesCard = resources.obtainTypedArray(R.array.MajorArcana);
            names = resources.getStringArray(R.array.majorarcane);

            mt = new MyTask();
            mt.execute();

            return rootView;
        }

        @Override
        public void onClick(View view) {

            switch(view.getId()) {

                case R.id.cardpick:
                    rangen();
                   break;

            }
        }

public void GenNum(){

        int min = 0;
        int max = 21;
        Random r = new Random();
        i1 = r.nextInt(max - min + 1) + min;
        String wtf = names[i1];
        Log.e("random number", "generated " + i1);




        if (Arrays.asList(haha).contains(i1)){
            Log.e("HAHAH", "SAME NUMBER");
            GenNum();
        }
        else {
            ImageView imagev = new ImageView(getActivity());
            mycards.add(i1);
            int cor = i1;
            haha = new Integer[mycards.size()];
            mycards.toArray(haha);
            imagev.setImageResource(imagesCard.getResourceId(cor, R.drawable.ace_of_cups));
            int height = 230;
            int width = 135;
            LinearLayout.LayoutParams params =  new LinearLayout.LayoutParams(width, height);
            params.setMargins(5,5,5,5);

            params.gravity = Gravity.CENTER;
            imagev.setLayoutParams(params);
            LinearLayout mycardslayout = (LinearLayout)getActivity().findViewById(R.id.mycardslayout);
            imagev.animate().alpha(1.0f).setDuration(100).start();
            mycardslayout.addView(imagev);
          //  imagesCard.recycle();
        }

    }

This is how I solved it: created int newid. In the process while creating new imageview I've increased newid by 1. And then in my onclick retrieved the id of clicked item. Afterwards compared it.

This is how it looks like:

newid = newid +1;
imagev.setId(newid);
imagev.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int ccheck = imagev.getId();

                    if(ccheck == 1){
                        Log.e("Just clicked on", "first" + ccheck);

                    } else if(ccheck == 2){
                        Log.e("Just clicked on", "second" + ccheck);

                    } else if(ccheck == 3){
                        Log.e("Just clicked on", "third" + ccheck);

                    }
                }
            });

Not the best implementation but it's working.

Cœur
  • 37,241
  • 25
  • 195
  • 267

3 Answers3

1

Since you are creating the image views programmatically, you can set the onClick listeners also programmatically, such as:

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

              // Do whatever you want (imageView can be changed here)

       }
});
zed
  • 3,180
  • 3
  • 27
  • 38
1

To set an onClickListener to imagev, you have two options

1- set a new listener on imagev as follows:

   imagev.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //do you work
            }
        });

2- Set an id to that view using imagev.setId(/*int id*/); and then do just as you did to cardpicker

check these answer1 , answer2 for more detials

Community
  • 1
  • 1
Atef Hares
  • 4,715
  • 3
  • 29
  • 61
0

if you create views at runtime you can keep a reference to the view and set on clickListeners programmatically

Valdio
  • 101
  • 1
  • 6