2012-12-27 72 views
0

我有4組聯繫人(Type1,2,3,無)。我想加載不同的圖像3 Type1,2,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相應的圖像爲這種類型加載正確,但當我滾動列表相同的圖像將加載到一些未分配也,有沒有問題與我的代碼請告訴我

回答

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

由於其做工精細.. – Pradeep

1

嘗試爲您的意見持有人類。然後在if(v == null)塊中,可以在else塊中使用setTag()並使用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{ 

     } 
     } 
+0

感謝不錯的方法,但仍同樣的問題 – Pradeep