【问题标题】:ListView showing only the last item in the first position of ListViewListView 仅显示 ListView 第一个位置的最后一项
【发布时间】:2019-05-29 09:16:17
【问题描述】:

我想在列表视图中显示一些项目。问题是列表视图只显示列表视图第一个位置的最后一项。它显示没有。的项目正确,但剩余的位置保持空白。

无法理解我在哪里做错了。请问有什么帮助吗?我在这里附上截图

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;
    }
}

【问题讨论】:

  • 谢谢@Zoe,我会记住你对我未来帖子的建议

标签: java android listview listadapter


【解决方案1】:

它显示最后一项,因为您重复使用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);

【讨论】:

  • OMG,原来如此,我没注意,是复制粘贴错误,非常感谢
【解决方案2】:

第一个主题Topics vumika = new Topics(); 每次都添加到topicsArrayList通过更改其名称。所以反映了姓氏。因此,请使用以下内容:-

vumika.setTopicName("Adhunik Yug"); 是写入vumika 的姓氏

  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);

【讨论】:

  • 谢谢,这是复制粘贴错误,我没注意到
【解决方案3】:

复制粘贴错误,只有vumika在变化,变化如下

 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();
        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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-28
    • 2017-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多