【发布时间】: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<Pair<String, String>> 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