0

I have an array of image Views which is generate dynamically on the run by the user

this is the code:

LinearLayout picLL = (LinearLayout) findViewById(R.id.cityInfoLN);
ImageView myImage = new ImageView(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//lp.add
lp.setMargins(32, 8, 32, 8);
myImage.setLayoutParams(lp);
myImage.setImageBitmap(bitmap1);
myImages[CityImageCount] = new ImageView(this);
myImages[CityImageCount].setId(CityImageCount);
myImages[CityImageCount] = myImage;
myImages[CityImageCount].setLayoutParams(lp);
myImages[CityImageCount].setImageBitmap(bitmap1);
myImages[CityImageCount].setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(final View v) {

        LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
        final View v1 = inflater.inflate(R.layout.image_dialog, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setView(v1);

        Button btn = (Button) v1.findViewById(R.id.editBTN);
        final TextView editText = (TextView) v1.findViewById(R.id.textView);
        editText.setText("هل تريد تعديل أم حذف الصورة");
        builder.setCancelable(true);
        final AlertDialog alert = builder.create();
        alert.getWindow().setGravity(Gravity.CENTER);
        alert.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        alert.show();
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view1) {
               // EditIndexs[]
              //  String name = v.getResources().getResourceName(v.getId());
               // String name2 = context.getString(v.getId());
             //   String name3 = getString(view1.getId());
              //  String name4 = getResources().getString(view1.getId());

                EditIndex = CityImageCount;
                int xxx = view1.getId();
                showFile5();
                alert.cancel();
            }
        });
    }
});
picLL.addView(myImages[CityImageCount]);

My problem is when the user click on any of the images to edit or delete it. How can I know which image is clicked?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Eman Fateen
  • 171
  • 1
  • 1
  • 10

2 Answers2

0

setTag() and getTag() methods of view might help you. here I paste the link of setTag() and getTag() usage. What is the main purpose of setTag() getTag() methods of View?

varatharajan
  • 229
  • 2
  • 15
0

First, you didn't correctly set the image view with this code. It's because you set the ImageView to the array twice with different ImageView (Please read the comment in the code:

// You're creating a ImageView
ImageView myImage = new ImageView(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

lp.setMargins(32, 8, 32, 8);
myImage.setLayoutParams(lp);
myImage.setImageBitmap(bitmap1);

// You're setting a new ImageView to array
myImages[CityImageCount] = new ImageView(this);
// then you set the Id
myImages[CityImageCount].setId(CityImageCount);

// But then you discard it by setting the array item with myImage
// so you're discarding the id.
myImages[CityImageCount] = myImage;

So, you need to set the ImageView to array like this:

ImageView myImage = new ImageView(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

lp.setMargins(32, 8, 32, 8);
myImage.setLayoutParams(lp);
myImage.setImageBitmap(bitmap1);

myImages[CityImageCount] = myImage;
myImages[CityImageCount].setId(CityImageCount);

(Note: Instead of array, you can use ArrayList)

Second, you need to use generateViewId() or ViewCompat.generateViewId() when setting the view id. You can't manually set view id with simple loop counter id like this:

myImages[CityImageCount].setId(CityImageCount);

it should be something like this:

// View.generateViewId() is only available from api 17
myImages[CityImageCount].setId(View.generateViewId());

// Use ViewCompat if you need to support API < 17
// myImages[CityImageCount].setId(ViewCompat.generateViewId());

Third, you need to handle the image click by checking the id:

 myImages[CityImageCount].setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(final View v) {

    // Check the id
    String id = v.getId();

    ...
 }

In case you need specific key for the image, you can use HashMap instead of Array like this:

Map<String, ImageView> map = new HashMap<String, ImageView>();

...
ImageView myImage = new ImageView(this);
map.put("Id1",myImage);

Last, separate your logic to something like this:

private ImageView generateImageView() {
  ImageView myImage = new ImageView(this);

  // set the ImageView properties here.
  ...

  return myImage;
} 

private void addImageViewToList(ImageView imageView) {
  // Add ImageView to the array
}

// hold previous click listener for ImageView.
private View.OnClickListener mImageViewClickListener;

// Get View.OnClickListener for ImageView, create it if not yet initialized.
private View.OnClickListener getImageViewClickListener() {
  if(mImageViewClickListener == null) {
     mImageViewClickListener = new View.OnClickListener() {
        @Override
        public void onClick(final View v) {
          // handle the ImageView click here
          ...
        }
  }
  return mImageViewClickListener;
}

So that you can arrange your code to something like this:

ImageView imageView = generateImageView();
imageView.setOnClickListener(getImageViewClickListener());
addImageViewToList(imageView);
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96