如何在列表视图中为第二个文本视图添加第二个字符串数组

时间:2014-10-05 16:54:53

标签: arrays listview

最近,我在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);

1 个答案:

答案 0 :(得分:0)

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

希望这会对你有所帮助!如果有任何问题,请随时询问。