ListView Adapter 随机排列项目
ListViewAdapter shuffles items
所以我有一个列表视图从基于 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;
}
编辑:此外,如果我反复滚动到底部而不向上滚动,我可以欺骗它来洗牌一些上面的项目,如屏幕截图(橙色)所示。
滚动时 ListView
回收旧视图。但是只有当新行膨胀时,您才更新该值。 Inflation 新行不会一直出现,它会重复使用旧行。这就是您再次看到旧值的原因。
将 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;
}
所以我有一个列表视图从基于 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;
}
编辑:此外,如果我反复滚动到底部而不向上滚动,我可以欺骗它来洗牌一些上面的项目,如屏幕截图(橙色)所示。
滚动时 ListView
回收旧视图。但是只有当新行膨胀时,您才更新该值。 Inflation 新行不会一直出现,它会重复使用旧行。这就是您再次看到旧值的原因。
将 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;
}