ListViewAdapter随机播放项目

时间:2018-07-30 06:04:59

标签: android listview android-arrayadapter custom-arrayadapter

所以我有一个列表视图,它从基于Room的SQLite数据库中提取数据。它看起来似乎很好地读取了数据,但是当页面加载时,我将获得第一个〜10个条目(显示的大小),并且当我尝试向下滚动列表视图时,它会重新开始并开始获取条目在列表的开头。屏幕截图上的红色圆圈显示了列表的顶部和底部以及当前的重复情况。

public class NeighborhoodListAdapter
    extends ArrayAdapter<LivingCity> {

    private List<LivingCity> neighborhood;
    Context context;

    // View lookup cache
    private static class ViewHolder {
        TextView cityName;
    }

    public NeighborhoodListAdapter(List<LivingCity> neighborhood, Context context) {
        super(context, R.layout.object_main_citydetailsrow, neighborhood);
        this.context = context;
        this.neighborhood = neighborhood;
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        LivingCity city = getItem(position);
        ViewHolder viewHolder; 

        if (convertView == null) {

            viewHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.object_main_citydetailsrow, parent, false);
            viewHolder.cityUid  = (TextView) convertView.findViewById(R.id.cityrow_uid);
                viewHolder.cityUid.setText(String.format(Locale.ENGLISH,"#%d", city.getUid()));
                }//city.getUid() returns an int
            }catch (Exception e){
                Log.d("editCity: ", e.toString());
            }


            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        // Return the completed view to render on screen
        return convertView;
    }
}

我以

开始
void loadNeighborhood() {
    neighborhoodDB = Neighborhood.getAppDatabase(this);
    neighborhood = neighborhoodDB.getNeighborhood();
}

从数据库助手中拉出排序列表

public List<LivingCity> getNeighborhood() {
        List<LivingCity> neighborhood = cityDao().getAll();

        Collections.sort(neighborhood,
                new Comparator<LivingCity>() {


            @Override
            public int compare(LivingCity city1, LivingCity city2) {
                return Integer.valueOf(city1.getUid()).compareTo(city2.getUid());
            } //I thought this sort operation might be whats wrong but same error with it commented out
        });

        return neighborhood;
    }

编辑:此外,如果我反复滚动至底部而又不向上滚动,则可以欺骗它改组某些上部项目,如屏幕截图所示(橙色)。

Shuffling listview

1 个答案:

答案 0 :(得分:5)

滚动ListView会回收旧视图。但是,只有在新行膨胀时才更新值。新行不会一直膨胀,它会重用旧行。这就是您再次看到旧值的原因。 如下所示,将viewHolder.cityUid.setText();行下面的if-else行移动到

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Get the data item for this position
    LivingCity city = getItem(position);
    ViewHolder viewHolder; 

    if (convertView == null) {
        viewHolder = new ViewHolder();
        LayoutInflater inflater = LayoutInflater.from(getContext());
        convertView = inflater.inflate(R.layout.object_main_citydetailsrow, parent, false);

        viewHolder.cityUid  = (TextView) convertView.findViewById(R.id.cityrow_uid);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    //city.getUid() returns an int
    viewHolder.cityUid.setText(String.format(Locale.ENGLISH,"#%d", city.getUid()));

    // Return the completed view to render on screen
    return convertView;
}