简体   繁体   English

单击按钮时android更改列表视图内的图像视图

[英]android change image view inside list view when button is clicked

I know that this question might be a duplicate of another question but i tried to follow what was suggested in solutions to similar questions without any success. 我知道这个问题可能是另一个问题的重复,但是我尝试遵循类似问题的解决方案中的建议,但没有成功。 I am relatively new to android so please kindly point me in the right direction. 我是android的新手,所以请指出正确的方向。

Scenario: I have a list view that has a custom layout file containing a small image and a text view. 方案:我有一个列表视图,该列表视图包含一个包含小图像和文本视图的自定义布局文件。 The list view displays all items as intended but what I want to do is as follows: When a button is clicked, a condition is evaluated and if the result is true, i want to change the default image to another one. 列表视图显示了所有预期的项目,但我想要做的如下:单击按钮时,将评估一个条件,如果结果为true,我想将默认图像更改为另一图像。 I am aware that I have to perform the image switching in the get view method that I have to override. 我知道我必须在必须覆盖的get view方法中执行图像切换。

public View getView(int position, View convertView, ViewGroup parent) {
            View itemView=convertView;
            if (itemView==null) {
                itemView=getLayoutInflater().inflate(R.layout.check_list_view, parent, false);
            }
            MailObject currentMailObject=mailList.get(position);
            ImageView ivCurrentMail=(ImageView)itemView.findViewById(R.id.ivMailIcon);
            if (mailList.get(position).isScanned()) {
                ivCurrentMail.setImageResource(R.drawable.ic_mail_scanned);

            }


            return itemView;
        }

The code for the button is: 该按钮的代码是:

btnSave.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), scanResult, Toast.LENGTH_LONG).show();

            if(!scannedMails.contains(scanResult))
            {
                scannedMails.add(scanResult);
                for (int i = 0; i < mailList.size(); i++) {
                    if (mailList.get(i).getCode().equalsIgnoreCase(scanResult)) {
                        int position=i;
                        mailList.get(i).setScanned(true);

                        ImageView ivCurrent = (ImageView) lvMails.getAdapter().getView(position, null, null).findViewById(R.id.ivMailIcon);
                        ivCurrent.setImageResource(R.drawable.ic_mail_scanned);

                        lvMails.invalidateViews();

                    }
                }

            }           






        }
    });

The XML file for a list view item is as follows: 列表视图项的XML文件如下:

<ImageView
    android:id="@+id/ivMailIcon"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:src="@drawable/ic_mail_not_scanned" />

    <TextView
        android:id="@+id/tvMailItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="17dp"
        android:layout_toRightOf="@+id/ivMailIcon"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

I did test if the condition evaluates as it should by displaying a Toast message on list view item click and the results were positive but the image just does not change. 我通过在列表视图项目单击时显示Toast消息来测试条件是否可以评估,结果是肯定的,但是图像只是没有变化。 Please let me know where I went wrong or missed something. 请让我知道我做错了什么或错过了什么。 Thanks 谢谢

You're setting the same image twice. 您要设置两次相同的图像。 In both cases, you use the setImageResource() method for the ImageView that points to R.id.ivMailIcon to set the drawable as R.drawable.ic_mail_scanned . 在这两种情况下,都可以对指向R.id.ivMailIconImageView使用setImageResource()方法,以将可绘制R.drawable.ic_mail_scanned设置为R.drawable.ic_mail_scanned

You are probably "setting" to image again correctly on a click, but since it's the same image, it won't appear to change. 您可能会“设置”一次单击以再次正确显示图像,但是由于它是同一图像,因此看起来不会改变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM