2012-04-16 19 views
0

私はAndroid 3.1アプリケーションを開発しています。ArrayAdapter内のチェックボックスにアクセスするとNULLが返されます

私は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(final int position, final View convertView, ViewGroup parent) 

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

を変更しました。

ここで私のエラー

:この質問は、このEnable a button when user mark a checkbox inside a list item

+1

は、それはすべきではありませんボタンdButton =(ボタン)row.findViewById(R.id.downloadFormsButton);? – kosa

答えて

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

    ....... 
} 
+0

ありがとうございます。できます!!すばらしいです!! – VansFannel

関連する問題