0

私の MyAdapter クラスでは、(Context context, String[] values)以下のコードに示すようにパラメーターを渡しています。String[] valuesgetView() メソッドで変数を使用したい。この変数を 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 に答える