自定义的ListView适配器显示错误值[单声道Android版] [英] Custom ListView Adapter Showing Incorrect Values [Mono Android]

查看:181
本文介绍了自定义的ListView适配器显示错误值[单声道Android版]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好堆栈溢出的Andr​​oid用户,

Hello Stack Overflow Android Users,

请与以下问题有所帮助:

Please help with the following question:

我写的一个应用程序,列出了鱼​​的种类的特征。
我使用一个自定义的ListView适配器类(我们称之为 FishSpeciesListAdapter )适配器。我有27种鱼类
记录为程序作为现在默认的(你最终能够增加
一些自己以及)。的问题是,当我适配器链接到实际列表视图的XML对象,它似乎相同5-6物种遍历所有的时间。

I'm writing an a feature for an app that lists species of fish. I'm using a custom ListView Adapter class (let's call this FishSpeciesListAdapter) for the adapter. I have 27 fish species recorded as a default for the program as for now (you will eventually be able to add some yourself as well). The problem is that when I link the adapter to the actual listview xml object, it seems to iterate over the same 5-6 species all the time.

问题必须在自定义适配器内撒谎,因为我已经测试了记录种和所有物种都是我传递给适配器列表中的不同。

The problem must lie within the custom adapter because I've tested the recorded species and all species are different within a List that I pass to the adapter.

这里的code,我设置适配器:

Here's the code where I set the adapter:

this.n_fishSpeciesListView = FindViewById<ListView> (Resource.Id.xml_fishSpeciesListView);
this.n_fishSpeciesListView.Adapter = new FishSpeciesListAdapter (this, 
this.n_model.SpecieManager.Species);

下面是自定义的ListView适配器code:

Here's the Custom ListView Adapter code:

public class FishSpeciesListAdapter : BaseAdapter
{
    Context n_context;
    List<AppCode.Specie> n_specieData;

    public FishSpeciesListAdapter (Context context, List<AppCode.Specie> specieData)
    {
        this.n_context = context;
        this.n_specieData = specieData;
    }

    public override int Count {
        get { return this.n_specieData.Count; }
    }

    public override Java.Lang.Object GetItem (int position)
    {
        return null;
    }

    public override long GetItemId (int position)
    {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public override View GetView (int position, View convertView, ViewGroup parent)
    {
        View v;
        if(convertView==null){

            LayoutInflater li = LayoutInflater.FromContext(parent.Context);
            v = li.Inflate(Resource.Layout.Adapter_FishSpeciesIcon, null);
            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));  
            }   
        }
        else
        {
            v = convertView;
        }
        return v;
    }
}

所以,当我执行上述code,这里是我所得到的截图:

So when I execute the above code, here's a screenshot of what I get:

正如你可以看到有重复的地方,我很积极的,有记录在列表中的变量至少27不同的物种。任何帮助,这是为什么,我该如何解决这个问题将是非常有益的。

As you can see there are duplicates where I'm am positive that there are at least 27 different species recorded in the passed List variable. Any help as to why this is and how I can fix this would be very helpful.

我读过它可重复使用的适配器的GetView方法查看currentView变量。我发现,信息出<一个href=\"http://stackoverflow.com/questions/11780816/getview-showing-wrong-images-in-a-custom-adapter\">this链接。我只是不知道如何在我的情况下解决这个问题。 code例子将是很好或详细的指示。感谢您的时间。

I've read that it may be reusing the "View currentView" variable in the GetView method of the adapter. I found that information out in this link. I just don't know how to fix this in my case. Code examples would be nice or detailed directions. Thank you for your time.

推荐答案

从我所看到的,您使用的 convertView 的对象是错误的。这个对象可以在任何情况下,意见被回收利用(例如列表项滚出视图)。它可以让你不必从你的布局XML再膨胀(这是一个昂贵的过程)。

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).

你应该做的是从你的布局XML膨胀,如果 convertView == NULL 。否则,使用 convertView 对象。

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

 if(convertView==null){

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

然后使用v来设置所有的值,并返回查看。

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;

这篇关于自定义的ListView适配器显示错误值[单声道Android版]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆