2012-04-16 66 views
0

我開發的是Android 3.1的應用程序。訪問到ArrayAdapter內一個複選框返回NULL

我有一個ListView哪些項目的複選框。有ListViewButton

我試圖啓用按鈕,如果有一個或多個複選框選中。這裏是我的代碼:

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) 

因爲Eclipse已經建議我吧。

哪裏是我的錯誤?

注意:這個問題是有關這個Enable a button when user mark a checkbox inside a list item

+1

它不應該是按鈕dButton =(按鈕)row.findViewById(R.id.downloadFormsButton);? – kosa 2012-04-16 15:25:38

回答

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 2012-04-16 15:47:01