我正在开发一个 Android 3.1 应用程序。
我有一个ListView
哪些项目是复选框。有ListView
一个Button
.
如果选中了一个或多个复选框,我正在尝试启用该按钮。这是我的代码:
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 向我推荐了它。
我的错误在哪里?
注意:当用户在列表项中标记复选框时,此问题与启用按钮有关