2012-12-27 6 views
0

J'ai 4 groupes de contacts (Type1,2,3, Aucun). je veux charger des images 3 pour Type1,2,3, si le contact appartiennent à Aucun alors ListView ne doit pas contenir une image .. Ceci est mon codeCharger dynamiquement des images dans une liste

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

problème est si j'assigne des contacts à Tapez 1 images correspondantes pour ce type chargé correctement, mais quand je fais défiler la liste même image sera chargée à certains non affecté, y at-il un problème avec mon code s'il vous plaît dites-moi

Répondre

1

faites le class Holder que l'autre question a suggéré. Et faites le titulaire pour chaque adaptateur que vous créez.

Pour répondre à votre question, essayez cette édition:

 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

Merci son travail bien .. – Pradeep

1

Essayez de faire une classe titulaire pour vos vues. Ensuite, dans le bloc if (v == null), vous pouvez utiliser setTag() et utiliser getTag() dans le bloc else. Voici du code.

Cette classe contient vos vues. La première partie de la méthode getView() devrait alors ressembler à ceci:

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

pour l'approche Remerciez belle mais toujours même problème – Pradeep

Questions connexes