2012-04-18 33 views
1

我正在開發Android 3.1應用程序,我對Android開發非常新。ArrayAdapter:getView參數變量更改爲最終

這裏是一個自定義的數組適配器在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更改爲最後是否有任何問題? 有沒有其他的方式可以使用positiononCheckedChanged

回答

2

沒問題。製作一個變量或最後一個參數意味着你不能一個價值進行重新分配給它,如:

position = ... 

因爲你並沒有使用在getView任何價值,它這是確定。

1

不,沒有。通常position只是用來定義如何創建該特定位置的物品。我還沒有看到在getView()中改變位置。所以你可以安全地做到這一點。

2

沒問題VansFannel實際上不需要聲明爲final。最終修飾符只需要當我們不想在任何地方更改變量值時。