重复ListView中的项目? [英] Repeating items in ListView?
问题描述
一切正常,但是现在我什么也没明白,我在listView中显示了10个项目,直到第6个项目都可以正常工作,然后从第1个项目再次重复?
Everything was working fine but now I am not getting what is happening,I have 10 items to display in the listView,its working fine till the 6th item and after that it repeats again from the 1st item again??
public class GroupDetailsAdapter extends BaseAdapter {
List<GetSetGroupDetails> group_details = new ArrayList<GetSetGroupDetails>();
Context context;
Typeface face;
public GroupDetailsAdapter(List<GetSetGroupDetails> group_details,
Context context) {
super();
this.group_details.clear();
this.group_details = group_details;
this.context = context;
face = Typeface.createFromAsset(context.getAssets(),
"HelveticaNeueLTStd-Th.otf");
}
@Override
public int getCount() {
// TODO Auto-generated method stub
int count = group_details.size();
return count;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
ViewHolder holder;
if (v == null) {
try {
LayoutInflater vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.group_item, null);
holder = new ViewHolder();
holder.tv_group_name = (TextView) v
.findViewById(R.id.tv_group_name);
String group_name = group_details.get(position).getGroup_name();
holder.tv_group_name.setText(group_name);
holder.tv_group_name.setTypeface(face);
holder.tv_group_reg_id = (TextView) v
.findViewById(R.id.tv_group_reg_id);
String groupRegId = group_details.get(position)
.getGroup_reg_id();
holder.tv_group_reg_id.setText(groupRegId);
holder.tv_group_reg_id.setTypeface(face);
holder.tv_subscriber_count = (TextView) v
.findViewById(R.id.tv_subscriber_count);
holder.tv_subscriber_count.setText(group_details.get(position)
.getSubscriber_count());
v.setTag(holder);
} catch (Exception e) {
e.printStackTrace();
}
} else
holder = (ViewHolder) v.getTag();
return v;
}
class ViewHolder {
TextView tv_group_name;
TextView tv_group_reg_id;
TextView tv_subscriber_count;
}
}
推荐答案
您正面临该数据重复问题,只是因为您没有遵循实现View-Holder模式的确切标准.
You are facing that data repeat issue, just because you aren't following the exact standards of implementing View-Holder pattern.
在这里,您正在执行findViewById(),并且如果view为null则设置数据,因此它对于第一组项目将运行良好,后记将为您显示下一组项目相同的数据.
Here you are doing findViewById() and setting data if view is null, so it will be running fine for first set of items, afterword it will show you the same data for the next sets of items.
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
ViewHolder holder;
if (v == null) {
try {
LayoutInflater vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.group_item, null);
holder = new ViewHolder();
holder.tv_group_name = (TextView) v
.findViewById(R.id.tv_group_name);
String group_name = group_details.get(position).getGroup_name();
holder.tv_group_name.setText(group_name);
holder.tv_group_name.setTypeface(face);
holder.tv_group_reg_id = (TextView) v
.findViewById(R.id.tv_group_reg_id);
String groupRegId = group_details.get(position)
.getGroup_reg_id();
holder.tv_group_reg_id.setText(groupRegId);
holder.tv_group_reg_id.setTypeface(face);
holder.tv_subscriber_count = (TextView) v
.findViewById(R.id.tv_subscriber_count);
holder.tv_subscriber_count.setText(group_details.get(position)
.getSubscriber_count());
v.setTag(holder);
} catch (Exception e) {
e.printStackTrace();
}
} else
holder = (ViewHolder) v.getTag();
return v;
}
正确:
实现View Holder模式的正确方法是,如果当前视图为null(这将是第一次发生),则查找视图并仅在执行后设置数据.因此,最终findViewById()
过程将是第一次完成,而下次将使用附加的标签获取视图.
Correct:
Correct way to implement View Holder pattern is to find views if current view is null (that would happen for the first time) and set data only after doing it. So eventually findViewById()
process will be done for the first time and next time onwards it will get views by using attached tags.
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
ViewHolder holder;
if (v == null) {
LayoutInflater vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.group_item, null);
holder = new ViewHolder();
holder.tv_group_name = (TextView) v
.findViewById(R.id.tv_group_name);
holder.tv_group_reg_id = (TextView) v
.findViewById(R.id.tv_group_reg_id);
holder.tv_subscriber_count = (TextView) v
.findViewById(R.id.tv_subscriber_count);
v.setTag(holder);
} catch (Exception e) {
e.printStackTrace();
}
} else
holder = (ViewHolder) v.getTag();
String group_name = group_details.get(position).getGroup_name();
holder.tv_group_name.setText(group_name);
holder.tv_group_name.setTypeface(face);
String groupRegId = group_details.get(position)
.getGroup_reg_id();
holder.tv_group_reg_id.setText(groupRegId);
holder.tv_group_reg_id.setTypeface(face);
holder.tv_subscriber_count.setText(group_details.get(position)
.getSubscriber_count());
return v;
}
这篇关于重复ListView中的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!