VansFannel
VansFannel

Reputation: 45921

ArrayAdapter: getView argument variable changed to final

I'm developing an Android 3.1 application and I'm very very new on Android development.

Here is a custom array adapter to use in a 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;
    }
}

On public View getView(int position, View convertView, ViewGroup parent) I have to change to final position argument. I have done it because I need to use it on public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) method.

Is there any problem if I change position to final? Is there any other way to use position on onCheckedChanged?

Upvotes: 1

Views: 927

Answers (3)

No problem VansFannel actually not required to declare as final. final modifier need only when we don't want change value of variable any where.

Upvotes: 2

Shubhayu
Shubhayu

Reputation: 13552

No there isn't. Usually position is just used to define how the item at that particular position should be created. I haven't yet seen position being changed in getView() yet. So you are safe to do it.

Upvotes: 1

smichak
smichak

Reputation: 4958

No problem. Making a variable or a parameter final means you can't re-assign a value to it, like:

position = ...

Since you're not assigning any value to it in getView this is OK.

Upvotes: 2

Related Questions