user2909006
user2909006

Reputation: 253

How to use setId() for imageView

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

Upvotes: 3

Views: 7326

Answers (1)

Chintan Soni
Chintan Soni

Reputation: 25267

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?

Upvotes: 3

Related Questions