如何将第二个字符串数组放入列表视图中的第二个文本视图
问题描述:
最近,我在youtube上关注了如何制作自定义适配器和列表视图的derek banas教程。 链接到视频:Derek Banas List View Tutorial如何将第二个字符串数组放入列表视图中的第二个文本视图
看完视频后,我看到derek只为一行文本视图插入了一个字符串数组。我有第二个字符串数组和行的第二个文本视图。我已经进入了第一个数组,但我如何输入字符串数组,因此每行有两个文本视图。
这是我的适配器。
class HangarAdapter extends ArrayAdapter<String> {
public HangarAdapter(Context context,String[] values) {
super(context, R.layout.hangar_layout, values);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater theInflater = LayoutInflater.from(getContext());
View theView = theInflater.inflate(R.layout.hangar_layout, parent, false);
TextView TextView1 = (TextView) theView.findViewById(R.id.textView1);
final TextView TextView2 = (TextView) theView.findViewById(R.id.textView2);
TextView1.setText(getItem(position));
ImageView theImageView = (ImageView) theView.findViewById(R.id.imageView);
return theView;
}
编辑:
ListView hangarList = (ListView) findViewById(R.id.hangarList);
ListAdapter adapter = new HangarAdapter(this, ship);
hangarList.setAdapter(adapter);
答
class HangarAdapter extends ArrayAdapter<String> {
String []ship1;
String []ship2;
public HangarAdapter(Context context,String[] values1,String[] values2) {
super(context, R.layout.hangar_layout, values);
ship1 = values1;
ship2=values2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater theInflater = LayoutInflater.from(getContext());
View theView = theInflater.inflate(R.layout.hangar_layout, parent, false);
TextView TextView1 = (TextView) theView.findViewById(R.id.textView1);
final TextView TextView2 = (TextView) theView.findViewById(R.id.textView2);
TextView1.setText(ship1[position]);
TextView2.setText(ship2[position]);
ImageView theImageView = (ImageView) theView.findViewById(R.id.imageView);
return theView;
}
在类
现在,在您的调用这个适配器做到这样,
ListView hangarList = (ListView) findViewById(R.id.hangarList);
ListAdapter adapter = new HangarAdapter(this, ship,ship2); // ship2 will be your second string array which you wanna set for second textview.
hangarList.setAdapter(adapter);
希望这将帮助你!如果有任何问题,请随时询问。
+0
谢谢!你真的帮我:D – 2014-10-05 18:08:55
+0
我很高兴我帮你! – Programmer 2014-10-06 02:05:13
传递第二个数组在HangarAdapter的构造函数中说secArray,然后设置这个数组TextView2.setText(secArray [position]));或者在HangerAdapter本身中声明secArray并以相同的方式访问。如果你发布了整个Adapter类,那么我可以编辑你的类来解决你的问题。 – Programmer 2014-10-05 17:02:06
这是整个适配器类 – 2014-10-05 17:04:32
好吧,那么请发布你从哪里调用HangarAdapter类。或至少使用此适配器类的代码块。 – Programmer 2014-10-05 17:08:03