【问题标题】:Load images to listview dynamically动态加载图像到列表视图
【发布时间】:2012-12-27 14:27:21
【问题描述】:

我有 4 组联系人(类型 1、2、3、无)。我想为 Type1、2、3 加载不同的图像 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;
    }
}

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

【问题讨论】:

    标签: android android-listview android-scroll


    【解决方案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);
            }
    

    【讨论】:

      【解决方案2】:

      尝试为您的观点创建一个持有者类。然后在 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{
      
              }
              }
      

      【讨论】:

      • 感谢您的好方法,但问题仍然存在
      猜你喜欢
      • 1970-01-01
      • 2011-04-07
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多