0

在我的 MyAdapter 类中,我正在传递(Context context, String[] values)参数,如下面的代码所示。我想String[] values在 getView() 方法中使用该变量。无论如何要将此变量传递给 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;
    }
}
4

1 回答 1

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;
}
于 2014-10-24T04:12:35.863 回答