如何获取imageView Android数组中的元素ID

时间:2018-12-11 08:44:58

标签: java android android-studio

我有一个由用户运行时动态生成的图像视图数组

这是代码:

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]);

我的问题是用户单击任何图像进行编辑或删除时。我怎么知道点击了哪个图像?

2 个答案:

答案 0 :(得分:0)

视图的

setTag()和getTag()方法可能会对您有所帮助。在这里,我粘贴了setTag()和getTag()用法的链接。 What is the main purpose of setTag() getTag() methods of View?

答案 1 :(得分:0)

首先,您没有使用此代码正确设置图像视图。这是因为您使用不同的ImageView将ImageView设置为数组两次(请阅读代码中的注释:

// 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;

因此,您需要将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);

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

(注意:您可以使用ArrayList代替数组)

第二,设置视图ID时需要使用generateViewId()ViewCompat.generateViewId()。您不能通过以下简单的循环计数器ID手动设置视图ID:

myImages[CityImageCount].setId(CityImageCount);

应该是这样的:

// 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());

第三,您需要通过检查ID处理图片点击:

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

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

    ...
 }

如果您需要图像的特定键,可以使用HashMap代替Array,如下所示:

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

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

最后,将您的逻辑分离为以下内容:

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;
}

这样您就可以将代码安排如下:

ImageView imageView = generateImageView();
imageView.setOnClickListener(getImageViewClickListener());
addImageViewToList(imageView);