简体   繁体   English

在列表视图中更改图像按钮的图像

[英]Change Image of image button in list view

I checked on Stackoverflow, some people asked about changing the image, but non of the solutions helped me. 我检查了Stackoverflow,有人问到要更改映像,但是没有一个解决方案对我有帮助。

I have an ImageButton in listItem of list view. 我在列表视图的listItem中有一个ImageButton I want to change the image of ImageButton on clicking the ImageButton . 我想在单击ImageButton更改ImageButton的图像。 It is displaying the toast but not changing the image. 它显示的是吐司,但没有改变图像。

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks in advance. 提前致谢。

Below is my code... 下面是我的代码...

public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.fragment_contacts_item_list, null);
    contactName = (TextView)rowView.findViewById(R.id.name_contact);
    contactNumber = (TextView)rowView.findViewById(R.id.number_contact);
    contactPic = (ImageView)rowView.findViewById(R.id.quickContactBadge_contact);
    contactStatusButton = (ImageButton)rowView.findViewById(R.id.configuration_button);
    convertView = rowView;

    contactArray = new ArrayList<ContactModel>();
    int status = contactList.get(position).getButtonStatus();
    if(status == 0)
        contactStatusButton.setImageResource(R.drawable.grey);
    else if(status == 1) //toggle button is on
        contactStatusButton.setImageResource(R.drawable.green);
    else
        contactStatusButton.setImageResource(R.drawable.red);

    contactStatusButton.setVisibility(View.VISIBLE);
    contactStatusButton.setBackgroundColor(Color.TRANSPARENT);
    contactStatusButton.setTag(position);
    contactStatusButton.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View view) {
            int position = (Integer) view.getTag();
            model.setName(contactList.get(position).getName());
            model.setNumber(contactList.get(position).getNumber());
            model.setPhoto(contactList.get(position).getPhoto());

            ContactAdapter.contactArray.add(contactList.get(position));
            contactStatusButton.setImageResource(R.drawable.green);
            Toast.makeText(context, "contact Number: " + contactList.get(position).getNumber(), Toast.LENGTH_SHORT).show();

        }
    });

    model = new ContactModel(contactList.get(position).getName(), contactList.get(position).getNumber(), 0);
    model.setName(contactList.get(position).getName());
    model.setNumber(contactList.get(position).getNumber());
    model.setButtonStatus(contactList.get(position).getButtonStatus());

    Bitmap pic = Utilities.getPhoto(contactList.get(position).getPhoto());
    model.setPhoto(contactList.get(position).getPhoto());

    contactName.setText(contactList.get(position).getName());
    contactNumber.setText(contactList.get(position).getNumber());
    contactPic.setImageBitmap(pic);

    return convertView;

} }

Your views all seem to be defined as members of the enclosing class. 您的所有视图似乎都被定义为封闭类的成员。 That means they are constantly getting reassigned to different views whenever getView is called. 这意味着每当调用getView时,它们就会不断地重新分配给不同的视图。

Instead, you should assign your views locally to the method, and make them final so that they can be referenced inside your click handler: 相反,您应该在本地将视图分配给该方法,并使其成为最终视图,以便可以在单击处理程序中引用它们:

final View rowView = inflater.inflate(R.layout.fragment_contacts_item_list, null);
final TextView contactName = (TextView)rowView.findViewById(R.id.name_contact);
final TextView contactNumber = (TextView)rowView.findViewById(R.id.number_contact);
final ImageView contactPic = (ImageView)rowView.findViewById(R.id.quickContactBadge_contact);
final ImageButton contactStatusButton = (ImageButton)rowView.findViewById(R.id.configuration_button);

You're also making the same mistake with model in the click handler. 您还会在点击处理程序中对model犯同样的错误。 You need to make sure you're using the model associated with the row, not whatever model happens to be left in the class member. 您需要确保使用与该行关联的模型,而不要在类成员中碰巧保留任何模型。

Also, you should note that you're not actually recycling the rows. 另外,您应该注意,您实际上并没有回收行。 You're creating a new row every time. 您每次都在创建新行。 This is defeating one of the main purposes of ListView, which is to not have to create new views for each row. 这违反了ListView的主要目的之一,即不必为每一行创建新视图。 You might want to find one of the many ListView tutorials out there to help you do this correctly. 您可能想要找到许多ListView教程之一,以帮助您正确地完成此操作。

I think it will mostly solve you problem to invalidate the view after you have updated its image resource. 我认为,更新视图的图像资源后,大多数可以解决使视图无效的问题。 Give this a try for your onClick(): 尝试一下onClick():

@Override
    public void onClick(View view) {
        int position = (Integer) view.getTag();
        model.setName(contactList.get(position).getName());
        model.setNumber(contactList.get(position).getNumber());
        model.setPhoto(contactList.get(position).getPhoto());

        ContactAdapter.contactArray.add(contactList.get(position));
        contactStatusButton.setImageResource(R.drawable.green);
        contactStatusButton.invalidate(); //The View now needs to be redrawn
        Toast.makeText(context, "contact Number: " + contactList.get(position).getNumber(), Toast.LENGTH_SHORT).show();

    }

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

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