3

I have an imageView created here in my GridViewAdapter class:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Try to reuse the views
    ImageView view = (ImageView) convertView;
    boolean checked = (mCheckBox==null)?false:(((CheckBox)  mCheckBox).isChecked());
    // if convert view is null then create a new instance else reuse it
    if (view == null) {
        view = new ImageView(Context);
        //view.setTag(ViewId(), "imageViewGRID");
        view.setId(R.id.1);
        Log.d("GridViewAdapter", "new imageView added");
    }

But I'm not sure on how to use .setId so that I can use this imageView in another class in this line:

ImageView imageView1 = (ImageView)v.findViewById(R.id.1);

Can someone please clear up how this works?

Please note that I have also tried:

view.setTag("imageView4Grid");

with

ImageView imageView1 = (ImageView)v.findViewWithTag("imageView4Grid");

but I get an error

user2909006
  • 253
  • 6
  • 22

1 Answers1

3

Heyyy, check this link: Android: View.setID(int id) programmatically - how to avoid ID conflicts?.

From API 17 and onwards, View class introduced static method generateViewId() that will generate a value suitable for use in setId(int).

setId(int id) simlpy takes positive integer as an input. You are trying to fetch id from the resource. Is this what you are trying do: How to set Id of dynamic created layout?

Community
  • 1
  • 1
Chintan Soni
  • 24,761
  • 25
  • 106
  • 174
  • Yeah so I created an xml file with the ids and then whenever I set the id in the one class and then try to get that view in another class, it always returns null. I asked a question on it but so far the one person who tried to answer it hasn't been responding to any feedback. Thanks for the links by the way! – user2909006 Dec 23 '13 at 18:07