0

我有 4 组联系人(类型 1、2、3、无)。我想为 Type1、2、3 加载不同的图像 3,如果联系人属于无,那么列表视图不应包含任何图像。这是我的代码

@Override       
public View getView(int position, View convertView,
ViewGroup parent) {             
// return super.getView(position, convertView, parent);

        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) ContactsListActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.contacts_list_row_view, null);
        }

        try {
            contactsData = (ContactsItem) getItem(position);
        } catch (Exception e) {

        }

        if (null != contactsData){
            final CheckBox contactsSelectedCheck = (CheckBox) v.findViewById(R.id.contact_selected_check);
            TextView contactNameText = (TextView) v.findViewById(R.id.contact_name_text);
            TextView contactNumberText = (TextView) v.findViewById(R.id.contact_number_text);
            ImageView contactImage = (ImageView) v.findViewById(R.id.contact_image);

            contactNameText.setText(contactsData.getContactName());
            contactNumberText.setText(contactsData.getContactNumber());             

            if(contactNameText != null && contactNumberText != null){
            if(contactsData.getContactProfileType() == DBConstants.TYPE_1){                 contactImage.setImageDrawable(getResources().getDrawable(R.drawable.icon_1));
            } else if(contactsData.getContactProfileType() == DBConstants.TYPE_2){                  contactImage.setImageDrawable(getResources().getDrawable(R.drawable.icon_2));
            } else if(contactsData.getContactProfileType() == DBConstants.TYPE_3){                  contactImage.setImageDrawable(getResources().getDrawable(R.drawable.icon_3));
            }else{

            }
            }

            if (selectedContactsTable.containsKey(contactsData.getContactNumber())) {
                contactsSelectedCheck.setChecked(true);             
            } else {
                contactsSelectedCheck.setChecked(false);                
            }

            contactsSelectedCheck.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (contactsSelectedCheck.isChecked()) {
                        LinearLayout r_layout = (LinearLayout) v.getParent();
                        TextView contactName = (TextView) r_layout.getChildAt(1);
                        TextView contactNumber = (TextView) r_layout.getChildAt(2);
                        selectedContactsTable.put(contactNumber.getText().toString(), contactName.getText().toString());
                    }else{
                        LinearLayout r_layout = (LinearLayout) v.getParent();
                        TextView contactNumber = (TextView) r_layout.getChildAt(2);
                        selectedContactsTable.remove(contactNumber.getText().toString());
                    }
                }
            });

        }
        return v;
    }
}

问题是如果我将一些联系人分配给类型 1 对应的图像,这种类型加载正确,但是当我滚动列表时,相同的图像也会加载到一些未分配的图像,我的代码有什么问题,请告诉我

4

2 回答 2

1

尝试为您的观点创建一个持有者类。然后在 if(v==null) 块中,您可以使用 setTag() 并在 else 块中使用 getTag() 。这是一些代码。

public static class ViewHolder {
    TextView contactNameText;
    TextView contactNumberText;
    ImageView contactImage;
}

这是一个包含您的视图的持有者类。getView() 方法的第一部分应如下所示:

View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater) ContactsListActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.contacts_list_row_view, null);
        holder = new ViewHolder();
        holder.contactNameText = (TextView) v.findViewById(R.id.contact_name_text);
        holder.contactNumberText = (TextView) v.findViewById(R.id.contact_number_text);
        holder.contactImage = (ImageView) v.findViewById(R.id.contact_image);
        v.setTag(holder);
    }
    else{
        holder = (ViewHolder) v.getTag();
    }

    try {
        contactsData = (ContactsItem) getItem(position);
    } catch (Exception e) {

    }

    if (null != contactsData){
        final CheckBox contactsSelectedCheck = (CheckBox) v.findViewById(R.id.contact_selected_check);
        holder.contactNameText.setText(contactsData.getContactName());
        holder.contactNumberText.setText(contactsData.getContactNumber());             

        if(contactNameText != null && contactNumberText != null){
        if(contactsData.getContactProfileType() == DBConstants.TYPE_1){                 holder.contactImage.setImageDrawable(getResources().getDrawable(R.drawable.icon_1));
        } else if(contactsData.getContactProfileType() == DBConstants.TYPE_2){                  holder.contactImage.setImageDrawable(getResources().getDrawable(R.drawable.icon_2));
        } else if(contactsData.getContactProfileType() == DBConstants.TYPE_3){                  holder.contactImage.setImageDrawable(getResources().getDrawable(R.drawable.icon_3));
        }else{

        }
        }
于 2012-12-27T14:50:08.100 回答
1

做另一个问题建议的班级持有人。并为您创建的每个适配器制作支架。

要回答您的问题,请尝试以下编辑:

        contactImage.setVisibility(View.Visible);
       if(contactsData.getContactProfileType() == DBConstants.TYPE_1){                 contactImage.setImageDrawable(getResources().getDrawable(R.drawable.icon_1));
        } else if(contactsData.getContactProfileType() == DBConstants.TYPE_2){                  contactImage.setImageDrawable(getResources().getDrawable(R.drawable.icon_2));
        } else if(contactsData.getContactProfileType() == DBConstants.TYPE_3){                  contactImage.setImageDrawable(getResources().getDrawable(R.drawable.icon_3));
        }else{
            contactImage.setVisibility(View.GONE);
        }
于 2012-12-27T14:59:10.907 回答