Skip to content Skip to sidebar Skip to footer

Custom Listview Adapter Showing Incorrect Values [mono Android]

Hello Stack Overflow Android Users, Please help with the following question: Problem I'm writing an a feature for an app that lists species of fish. I'm using a custom ListView Ada

Solution 1:

From what I see, you're using the convertView object wrong. This object is available in case any Views have been recycled (for example a list item scrolled out of view). It is available so that you don't have to inflate from your layout xml again (which is a costly process).

What you should do is inflate from your layout xml if convertView==null. Otherwise use the convertView object.

if(convertView==null){

        LayoutInflaterli= LayoutInflater.FromContext(parent.Context);
        v = li.Inflate(Resource.Layout.Adapter_FishSpeciesIcon, null);
}
else{
     v=convertView;
}

Then use v to set all your values and return the View.

    ImageView iconImage = (ImageView)v.FindViewById(Resource.Id.xml_adapter_fishSpeciesIconImage);
        TextView nameText = (TextView)v.FindViewById(Resource.Id.xml_adapter_fishSpeciesNameText);
        TextView scientificNameText = (TextView)v.FindViewById(Resource.Id.xml_adapter_fishSpeciesScientificNameText);

 nameText.Text = this.n_specieData[position].Name;
 scientificNameText.Text = this.n_specieData[position].ScientificName;

 if (this.n_specieData[position].RelatedMedia.AttachedPhotos.Count < 1)
 {
       iconImage.SetImageResource(Resource.Drawable.Icon); 
 }
 else
 {
       iconImage.SetImageBitmap(BitmapFactory.DecodeByteArray(this.n_specieData[position].RelatedMedia.AttachedPhotos[0], 0, this.n_specieData[position].RelatedMedia.AttachedPhotos[0].Length));  
 }   
 return v;

Solution 2:

you need to reuse convertView when it already exists, or create a new view when it doesn't. In all cases, you have to give your textViews/imageViews the right properties :

publicoverride View GetView (int position, View convertView, ViewGroup parent)
{
    if(convertView==null){

        LayoutInflater li = LayoutInflater.FromContext(parent.Context);
        convertView = li.Inflate(Resource.Layout.Adapter_FishSpeciesIcon, null);
    }
    ImageView iconImage = (ImageView)convertView.FindViewById(Resource.Id.xml_adapter_fishSpeciesIconImage);
    TextView nameText = (TextView)convertView.FindViewById(Resource.Id.xml_adapter_fishSpeciesNameText);
    TextView scientificNameText = (TextView)convertView.FindViewById(Resource.Id.xml_adapter_fishSpeciesScientificNameText);

    nameText.Text = this.n_specieData[position].Name;
    scientificNameText.Text = this.n_specieData[position].ScientificName;

    if (this.n_specieData[position].RelatedMedia.AttachedPhotos.Count < 1)
    {
        iconImage.SetImageResource(Resource.Drawable.Icon); 
    }
    else
    {
        iconImage.SetImageBitmap(BitmapFactory.DecodeByteArray(this.n_specieData[position].RelatedMedia.AttachedPhotos[0], 0, this.n_specieData[position].RelatedMedia.AttachedPhotos[0].Length));  
    }   
    }

    return convertView;
}

Post a Comment for "Custom Listview Adapter Showing Incorrect Values [mono Android]"