我正在开发一个 Android 3.1 应用程序,我对 Android 开发非常陌生。
这是一个在 ListView 中使用的自定义数组适配器:
public class FormAdapter extends ArrayAdapter<Form>
{
private Context context;
private int layoutResourceId;
private List<Form> forms;
public ArrayList<String> checkedItems;
private Button downloadButton;
public ArrayList<String> getCheckedItems()
{
return checkedItems;
}
public FormAdapter(Context context, int textViewResourceId,
List<Form> objects, Button downloadButton)
{
super(context, textViewResourceId, objects);
this.context = context;
this.layoutResourceId = textViewResourceId;
this.forms = objects;
this.checkedItems = new ArrayList<String>();
this.downloadButton = downloadButton;
}
@Override
public View getView(final int position, 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));
}
downloadButton.setEnabled(checkedItems.size() > 0);
}
});
}
}
return row;
}
}
我public View getView(int position, View convertView, ViewGroup parent)
必须更改为最后的position
论点。我已经这样做了,因为我需要在public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
方法上使用它。
如果我更改position
为final有什么问题吗?有没有其他方法可以使用position
on onCheckedChanged
?