【问题标题】:ListView is getting populated with same valuesListView 正在填充相同的值
【发布时间】:2021-07-07 13:10:41
【问题描述】:

发生的情况是我有一个 ArrayList> 并且当我遍历该列表以使用列表适配器将这对添加到具有两个单独列的列表视图时,我尝试添加这对第一个字符串到某个文本视图,然后第二个字符串到某个文本视图,然后循环整个列表,然后将相同的值添加到所有行。

列表适配器代码:

package com.example.test2;

import android.content.Context;
import android.util.Log;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;


public class ThreeColumn_ListAdapter extends ArrayAdapter<Pair<String, String>> {
    private LayoutInflater mInflater;
    private ArrayList<Pair<String, String>> news;
    private int mViewResourceId;


    public ThreeColumn_ListAdapter(Context context, int textViewResourceId, ArrayList<Pair<String, String>> news) {
        super(context, textViewResourceId, news);
        this.news = news;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mViewResourceId = textViewResourceId;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = mInflater.inflate(mViewResourceId, null);
        TextView firstName = convertView.findViewById(R.id.textFirstName);
        TextView lastName = convertView.findViewById(R.id.textLastName);
        //TextView favFood = convertView.findViewById(R.id.textFavFood);

        Log.i("n2",news.toString());
        for(int j=0;j<news.size();j++){
           firstName.setText(news.get(j).first);
           lastName.setText(news.get(j).second);

        }


        return convertView;
    }

    }

ArrayList中存储了什么>新闻:

[Pair{usd -0.8068}, Pair{eur 0.5327}, Pair{gbp 1.2172}, Pair{nzd -2.7538}, Pair{cad 0.7586}, Pair{aud -1.7719}, Pair{chf 0.9591}, Pair{jpy 1.8649}]

在列表视图布局中查看的输出: Pair{jpy 1.8649} 对在 textview 中的第一个值 firstName 迭代 8 次,对在 textview 中的第二个值 lastName 迭代 8 次,8 是 ArrayList&lt;Pair&lt;String, String&gt;&gt; news 的长度

【问题讨论】:

  • 什么是 Log.i 中的“n2”
  • 这是存储在新闻列表中的内容,因此第二个代码显示了一个列表; [对{usd -0.8068},对{eur 0.5327},对{gbp 1.2172},对{nzd -2.7538},对{cad 0.7586},对{aud -1.7719},对{chf 0.9591},对{jpy 1.8649 }]

标签: java android-studio listview arraylist listadapter


【解决方案1】:

想通了,我所要做的就是删除 for 循环并只执行 news.get(position).first 而不是为新闻执行循环,因为 listview 会自然循环。

以前是这样的:

public View getView(int position, View convertView, ViewGroup parent) {
        convertView = mInflater.inflate(mViewResourceId, null);
        TextView firstName = convertView.findViewById(R.id.textFirstName);
        TextView lastName = convertView.findViewById(R.id.textLastName);
        //TextView favFood = convertView.findViewById(R.id.textFavFood);

        Log.i("n2",news.toString());
        for(int j=0;j<news.size();j++){
           firstName.setText(news.get(j).first);
           lastName.setText(news.get(j).second);

        }


        return convertView;
    }

正确方法:

 public View getView(int position, View convertView, ViewGroup parent) {
        convertView = mInflater.inflate(mViewResourceId, null);
        TextView firstName = convertView.findViewById(R.id.textFirstName);
        TextView lastName = convertView.findViewById(R.id.textLastName);
        //TextView favFood = convertView.findViewById(R.id.textFavFood);
        firstName.setText(news.get(position).first);
        lastName.setText(news.get(position).second);


        return convertView;
    }

【讨论】:

    猜你喜欢
    • 2015-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多