0

I create an imageView here in my coding in my gridView adapter 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);
        Log.d("GridViewAdapter", "new imageView added");
    }

Is there a way that I can assign view an id so that I can use that id in another class to set an image?

I am going about setting the image like this:

/// Let's save to a .jpg file ...
File file = new File(mContext.getFilesDir().getAbsolutePath() + "/test2.jpg");
FileOutputStream out;
try {
    file.createNewFile();
    out = new FileOutputStream(file);
    appBmp.compress(Bitmap.CompressFormat.JPEG, 80, out);
    out.close();

    // Load back the image file to confirms it works        
    Bitmap bitmap = BitmapFactory.decodeFile( file.getAbsolutePath() );
    ImageView imageView1 = (ImageView)v.findViewById(R.id.layout1);
    imageView1.setImageBitmap( bitmap );            
}

So that way I could just set that id in the findViewById and it would set the image to that imageView.

I have already tried setting up an ids.xml file with different id values and then assigning it to the imageView by doing this:

view.setId(R.id.layout1);

but this didn't work.

Lars Höppner
  • 17,834
  • 2
  • 41
  • 67
user2909006
  • 255
  • 4
  • 22

1 Answers1

0

You can just set any integer with View.setId(), you do not need to define them in an xml (which would make your approach static not dynamic). This might not work because you need eclipse/android studio/adb to create the reference files for the xml ids.

You only have to check that you assign unique ids. See this post for solving this problem: Programmatic Views how to set unique id's?. Mind that Id does not need to be uniqe by definition, but if they aren't you wont get them back by id. Also maybe setTag will be more what you need?

Community
  • 1
  • 1
Patrick Favre
  • 29,166
  • 6
  • 96
  • 114
  • Ok so I looked at the link and so maybe I should use something like this: `view.setTag(ViewId(), "imageViewGRID");`? and then also, how can I make it so that I can make use of the class that was in the link? – user2909006 Dec 22 '13 at 17:41
  • Also, I am using eclipse so I can create the reference xml files for the ids but when I do this, and then go to find the view with findViewById() I get a NPE – user2909006 Dec 23 '13 at 02:04