Java ListView正在填充相同的值

Java ListView正在填充相同的值,java,android-studio,listview,arraylist,listadapter,Java,Android Studio,Listview,Arraylist,Listadapter,发生的情况是,我有一个ArrayList,当我循环该列表以使用列表适配器将该对添加到具有两个独立列的listview时,我尝试将该对的第一个字符串添加到某个textview,然后将第二个字符串添加到某个textview,然后它在整个列表中循环,然后向所有行添加相同的值 列表适配器的代码: package com.example.test2; import android.content.Context; import android.util.Log; import android.util.

发生的情况是,我有一个ArrayList,当我循环该列表以使用列表适配器将该对添加到具有两个独立列的listview时,我尝试将该对的第一个字符串添加到某个textview,然后将第二个字符串添加到某个textview,然后它在整个列表中循环,然后向所有行添加相同的值

列表适配器的代码:

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

    }
在listview布局中查看的输出:
Pair{jpy 1.8649}
在textview
firstName
中Pair的第一个值迭代了8次,在textview
lastName
中Pair的第二个值迭代了8次,8是
ArrayList news
的长度,我所要做的就是删除for循环,只做news.get(position)。首先,不要对新闻执行循环,因为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;
    }

Log.Ith中的“n2”是存储在新闻列表中的内容,因此第二个代码显示列表;[对价{美元-0.8068},对价{欧元0.5327},对价{1.2172},对价{nzd-2.7538},对价{cad 0.7586},对价{aud-1.7719},对价{chf 0.9591},对价{1.8649}]
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;
    }