gpt4 book ai didi

java - ListView 仅显示 ListView 第一个位置的最后一项

转载 作者:行者123 更新时间:2023-12-01 06:48:59 25 4
gpt4 key购买 nike

我想在 ListView 中显示一些项目。问题是 ListView 仅显示 ListView 第一个位置的最后一项。它显示了编号。项目正确,但其余位置保持空白。

不明白我哪里做错了。有什么帮助吗?我在这里附上屏幕截图Screenshot

MainActivit.java

ListView topicList;
ArrayList<Topics> topics;
ArrayList<Topics> topicsArrayList;
TopicAdapter topicAdapter;

//Inside onCreat method
topicList = findViewById(R.id.lv_list);

//Array list to show items in the list view
topics = new ArrayList<>();
topicAdapter = new TopicAdapter(this, R.layout.list_item_row, topics);
topicList.setAdapter(topicAdapter);

//Array list to add the list items
topicsArrayList = new ArrayList<>();

Topics vumika = new Topics();
vumika.setTopicName("Vumika");
topicsArrayList.add(vumika);

Topics prachin_yug = new Topics();
vumika.setTopicName("Prachin Yug");
topicsArrayList.add(prachin_yug);

Topics madhya_yug = new Topics();
vumika.setTopicName("Madhya Yug");
topicsArrayList.add(madhya_yug);

Topics adhunik_yug = new Topics();
vumika.setTopicName("Adhunik Yug");
topicsArrayList.add(adhunik_yug);

//Add all the topics to the topic list
if (topicsArrayList != null || topicsArrayList.size() > 0) {
topics.addAll(topicsArrayList);
topicAdapter.notifyDataSetChanged();
}

TopicAdapter.java

public class TopicAdapter extends BaseAdapter {

private Context context;
private int layout;
private ArrayList<Topics> topics;
private ViewHolder viewHolder;

public TopicAdapter(Context context, int layout, ArrayList<Topics> topics) {
this.context = context;
this.layout = layout;
this.topics = topics;
}

public TopicAdapter(Context context, int layout, ArrayList<Topics> topics, ViewHolder viewHolder) {
this.context = context;
this.layout = layout;
this.topics = topics;
this.viewHolder = viewHolder;
}

@Override
public int getCount() {
return topics.size();
}

@Override
public Object getItem(int position) {
return topics.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

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

View row = convertView;
viewHolder = new ViewHolder();

if (row == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (inflater != null) {
row = inflater.inflate(layout, null);
viewHolder.topicName = row.findViewById(R.id.tv_topic_name);
row.setTag(viewHolder);

}
} else {
viewHolder = (ViewHolder) row.getTag();
}

Topics topic = topics.get(position);
viewHolder.topicName.setText(topic.getTopicName());

return row;
}

private class ViewHolder {
TextView topicName;
}
}

最佳答案

它显示最后一项,因为您重复使用 vumika.setTopicName() setter 方法。您必须为每个新主题对象设置名称。

正确的是

Topics vumika = new Topics();
vumika.setTopicName("Vumika");
topicsArrayList.add(vumika);

Topics prachin_yug = new Topics();
prachin_yug.setTopicName("Prachin Yug");
topicsArrayList.add(prachin_yug);

Topics madhya_yug = new Topics();
madhya_yug.setTopicName("Madhya Yug");
topicsArrayList.add(madhya_yug);

Topics adhunik_yug = new Topics();
adhunik_yug.setTopicName("Adhunik Yug");
topicsArrayList.add(adhunik_yug);

关于java - ListView 仅显示 ListView 第一个位置的最后一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56357069/

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