0

私は Android 3.1 アプリケーションを開発しています。

ListViewどの項目がチェックボックスであるかがあります。とListViewありButtonます。

1 つ以上のチェック ボックスがオンになっている場合、そのボタンを有効にしようとしています。これが私のコードです:

public class FormAdapter extends ArrayAdapter<Form>
{
    private Context context;
    private int layoutResourceId;
    private List<Form> forms;
    private ArrayList<String> checkedItems;

    public ArrayList<String> getCheckedItems()
    {
        return checkedItems;
    }

    public FormAdapter(Context context, int textViewResourceId,
            List<Form> objects)
    {
        super(context, textViewResourceId, objects);

        this.context = context;
        this.layoutResourceId = textViewResourceId;
        this.forms = objects;
        this.checkedItems = new ArrayList<String>();
    }

    @Override
    public View getView(final int position, final View convertView, ViewGroup parent)
    {
        View row = convertView;
        if (row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
        }

        Form f = forms.get(position);
        if (f != null)
        {
            CheckBox checkBox = (CheckBox)row.findViewById(R.id.itemCheckBox);
            if (checkBox != null)
            {
                checkBox.setText(f.Name);
                checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
                {
                    public void onCheckedChanged(CompoundButton buttonView,
                            boolean isChecked)
                    {
                        Form f = forms.get(position);
                        if (isChecked)
                        {
                            checkedItems.add(f.FormId);
                        }
                        else
                        {
                            checkedItems.remove(checkedItems.indexOf(f.FormId));
                        }

                        Button dButton = (Button)convertView.findViewById(R.id.downloadFormsButton);
                        dButton.setEnabled(checkedItems.size() > 0);
                    }
                });
            }
        }

        return row;
    }
}

dButtonしかし、それは NULLであるため機能しません。

私は変わった

public View getView(int position, View convertView, ViewGroup parent)

これに

public View getView(final int position, final View convertView, ViewGroup parent)

日食が私にそれを提案したからです。

私のエラーはどこですか?

:この質問は、ユーザーがリスト項目内のチェックボックスをマークしたときにボタンを有効にすることに関連しています

4

1 に答える 1

1

問題は次のステートメントにあります。

Button dButton = (Button)convertView.findViewById(R.id.downloadFormsButton);

ここではfindViewById、リスト アイテム ビューで呼び出していますが、そのメソッドは、 の下のビュー ツリーのみを参照していますconvertView

最善の策は、このボタンをアクティビティからアダプターのコンストラクターに渡すことです。次のようになります。

public class FormAdapter extends ArrayAdapter<Form>
{
    private Button btnDownload;
    ........


    public FormAdapter(Context context, int textViewResourceId, List<Form> objects, Button btn)
    {
        ........
        btnDownload = btn;
        ........
    }

    @Override
    public View getView(final int position, final View convertView, ViewGroup parent)
    {
        .......

            btnDownload.setEnabled(checkedItems.size() > 0);

        .......
    }
}

そしてあなたの活動で:

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    .......

    Button btn  = (Button)this.findViewById(R.id.downloadFormsButton);

    .......

    FormAdapter adapter = new FormAdapter(this, textResId, objects, btn);

    .......
}
于 2012-04-16T15:27:39.133 に答える