动态加载图像到listview



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

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

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

做另一个问题建议的类Holder。为你创建的每一个适配器做Holder。

要回答你的问题,试着编辑:

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

尝试为您的视图创建一个holder类。然后在if(v==null)块中,您可以使用setTag()并在else块中使用getTag()。下面是一些代码:

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

这是一个包含视图的holder类。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{
        }
        }

相关内容

  • 没有找到相关文章

最新更新