gpt4 book ai didi

android - 在 ListView 中重复项目?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:36:12 25 4
gpt4 key购买 nike

一切正常,但现在我不知道发生了什么,我有 10 个项目要显示在 listView 中,它工作正常直到第 6 个项目,然后它再次从第一个项目重复??

    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 模式的确切标准。

错误:

此处您正在执行 findViewById() 并在 View 为 null 时设置数据,因此它将在第一组项目中正常运行,之后它将为您显示下一组项目的相同数据。

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 模式的正确方法是在当前 View 为 null 时查找 View (这将是第一次发生)并仅在完成后设置数据。所以最终 findViewById() 过程将第一次完成,下一次将通过使用附加标签获取 View 。

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;

}

关于android - 在 ListView 中重复项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23995052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com