自定义列表适配器重复条目 [英] Custom list adapter repeats entries

查看:238
本文介绍了自定义列表适配器重复条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个将与数组中的条目填充一个ListView。

I am trying to create a ListView that will be populated with the entries from an array.

所以这是我的项目布局:

So this is my item layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="60dip" >  

    <ImageView android:id="@+id/list_item_image"
        android:layout_height="wrap_content"
        android:padding="2dip" 
        android:layout_gravity="center_vertical|center_horizontal" 
        android:layout_width="50dip"/>  
    <TextView android:id="@+id/list_item" 
        android:layout_height="fill_parent"
        android:textSize="25sp"
        android:layout_width="fill_parent" 
        android:ellipsize="marquee" 
        android:gravity="center_vertical" 
        android:padding="5dip" >
    </TextView>
</LinearLayout>

我试图改变 layout_height 的LinearLayout ,但我遇到了一些问题。如果我保持高度的 WRAP_CONTENT ,则显示了正确的条目我的名单 - 第1项,第2项,第3项,依此类推,直到第12项。但是,如果我改变高度 60dip ,条目重复第六项之后(我得到的第1项,第2项,第3项,第4项,第5项,第6项,第1项,第2项,第3项...)。如果我一直在做它越大,重复的条目更频繁。

I tried changing the layout_height of the LinearLayout but I ran into some problems. If I keep the height at wrap_content, my list is displayed with the correct entries -- Item 1, Item 2, Item 3, and so on until Item 12. However if I change the height to 60dip, the entries repeat after the sixth entry (I get Item 1, Item 2, Item 3, Item 4, Item 5, Item 6, Item 1, Item 2, Item 3...). If I keep on making it larger, the entries repeat more frequently.

这是从一个片段的 ListAdapter 在这里我设置列表中的条目:

This is a snippet from the ListAdapter where I set the list entries:

public View getView(int position, View convertView, ViewGroup parent) {

    LinearLayout layout;

    if (convertView == null){
         layout = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.items_list_item, parent, false);

         TextView title = (TextView) layout.findViewById(R.id.list_item);
         title.setText(menuItems[position]);

         ImageView icon = (ImageView) layout.findViewById(R.id.list_item_image);
         int logo = getResources().getIdentifier(menuIcons[position], "drawable", getPackageName());
         icon.setImageResource(logo);

    } else {
         layout = (LinearLayout) convertView;
    }
  return layout;
}

任何人遇到这个问题?我不明白是怎么回事,因为我认为它应该是直着从阵列抓住。

Anybody else encountered this problem? I do not understand what is going on since I thought it should be straight-forward grabbing from the array.

编辑:包括我的整个getView()方法。请原谅得到图标的丑陋的方式,我还没有想通出来呢,

included the whole of my getView() method. Pardon the ugly way of getting the icons, I haven't figured it out yet,

推荐答案

您没有张贴足够code,但在你的适配器的getView(...)尽量利用convertView的。

You didn't post enough code, but in your Adapter's getView(...) try to make use of the convertView.

public View getView(int position, View convertView, ViewGroup parent){
   if(convertView == null){
      convertView = mInflater.inflate(R.layout.my_listitem_row, parent, false);
   }

   //...fill the TextViews on your layout

   return convertView;
}

抓取的图标应该是那么容易,因为

Fetching the icons should be as easy as

icon.setImageResource(R.drawable.my_icon); //the res/drawable folder has the my_icon.png file

这篇关于自定义列表适配器重复条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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