将更多参数传递给ArrayAdapter中的getView()方法

时间:2014-10-24 04:06:37

标签: android parameters android-arrayadapter getview

在我的MyAdapter类中,我传递了(Context context, String[] values)个参数,如下面的代码所示。我想在getView()方法中使用String[] values变量。无论如何将这个变量传递给getView()方法?

class MyAdapter extends ArrayAdapter<String> {
    public MyAdapter(Context context, String[] values) {
        super(context, R.layout.row_layout_2, values);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater theInflater = LayoutInflater.from(getContext());
        View theView = theInflater.inflate(R.layout.row_layout_2, parent, false);
        String tvShow = getItem(position);
        TextView theTextView = (TextView) theView.findViewById(R.id.textView1);
        theTextView.setText(tvShow);
        ImageView theImageView = (ImageView) theView.findViewById(R.id.imageView1);
        theImageView.setImageResource(R.drawable.dot);
        return theView;
    }
}

1 个答案:

答案 0 :(得分:4)

您可以为商品存储实例变量

private final String[] items;

然后你的构造函数看起来像

public MyAdapter(Context context, String[] values) {
    super(context, R.layout.row_layout_2, values);
    this.items = values;
}

并有一个吸气剂,如

public String[] getItems() {
    return items;
}