2012-04-11 49 views
0

雖然我很清楚,我無法在我的onClick方法中引用非最終變量,但我有興趣瞭解如何解決我目前面臨的問題。「不能引用內部類中的非最終變量」如何根據複選框的狀態來管理TextView?

我有一個ArrayAdapter。如果用戶按下複選框AsyncTask並且用戶或者訂閱或退訂他自己的隊列,取決於複選框是否被選中。

我想這個狀態在我txtStatus的TextView的文本中的每個項目中得到反映。如何根據複選框的狀態設置TextView的文本?

這裏是我的getView方法的代碼的一部分:

@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    View row = convertView; 

    final CheckBox queueCBox; 
    userControl = new UserController(context); 
    final String queueName = data[position].label; 
    if(row == null) 
    { 
     LayoutInflater inflater = ((Activity)context).getLayoutInflater(); 
     row = inflater.inflate(layoutResourceId, parent, false); 

     final QueueHolder holder = new QueueHolder(); 
     holder.queuecbox = (CheckBox)row.findViewById(R.id.queues_item_checkbox); 
     queueCBox = (CheckBox)row.findViewById(R.id.queues_item_checkbox); 
     holder.txtLabel = (TextView)row.findViewById(R.id.queues_item_label); 
     holder.txtStatus = (TextView)row.findViewById(R.id.queues_item_status); 

     holder.queuecbox.setOnClickListener(new OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       final String[] param = new String[1]; 
       param[0] = queueName; 

       if(queueCBox.isChecked()) 
       { 
        holder.txtStatus.setText("Tilmeldt"); 
        Log.d("ClickTest","SWITCH ON"); 
        new JoinQueueTask().execute(param); 


       } 
       else 
       { 
        holder.txtStatus.setText("Frameldt"); 
        Log.d("ClickTest","SWITCH OFF"); 
        new LeaveQueueTask().execute(param); 
       } 
      } 
     }); 
     row.setTag(holder); 
    } 
    else 
    { 
     final QueueHolder holder = (QueueHolder)row.getTag(); 
    } 

    QueueItem queue = data[position]; 
    String name = queue.label.replace("-", " "); 
    holder.txtLabel.setText(name); // holder cannot be resolved 
    holder.queuecbox.setChecked(queue.value); // holder cannot be resolved 

    return row; 
} 

回答

6

保持你持有它的方式。只是設置onClickListener,嘗試這樣做,

QueueHolder holder = null; 
if(row == null) 
{ 
    LayoutInflater inflater = ((Activity)context).getLayoutInflater(); 
    row = inflater.inflate(layoutResourceId, parent, false); 

    holder = new QueueHolder(); 
    holder.queuecbox = (CheckBox)row.findViewById(R.id.queues_item_checkbox); 
    queueCBox = (CheckBox)row.findViewById(R.id.queues_item_checkbox); 
    holder.txtLabel = (TextView)row.findViewById(R.id.queues_item_label); 
    holder.txtStatus = (TextView)row.findViewById(R.id.queues_item_status); 
    row.setTag(holder); 
} 
else 
{ 
    holder = (QueueHolder)row.getTag(); 
} 

QueueItem queue = data[position]; 
String name = queue.label.replace("-", " "); 
holder.txtLabel.setText(name); // holder cannot be resolved 
holder.queuecbox.setChecked(queue.value); // holder cannot be resolved 

final QueueHolder fianlHolder = holder; 

finalHolder.queuecbox.setOnClickListener(new OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      final String[] param = new String[1]; 
      param[0] = queueName; 

      if(queueCBox.isChecked()) 
      { 
       finalHolder.txtStatus.setText("Tilmeldt"); 
       Log.d("ClickTest","SWITCH ON"); 
       new JoinQueueTask().execute(param); 


      } 
      else 
      { 
       finalHolder.txtStatus.setText("Frameldt"); 
       Log.d("ClickTest","SWITCH OFF"); 
       new LeaveQueueTask().execute(param); 
      } 
     } 
    }); 
return row; 
} 
+0

如果我這樣做,當我嘗試設置標籤的文本和複選框後來在方法狀態這則持有人不能得到解決。 holder.txtLabel.setText(name); holder.queuecbox.setChecked(queue.value); – CodePrimate 2012-04-11 07:42:21

+0

你嘗試過嗎?據我所知,在這種情況下,您的持有者對象將變爲final,而不是持有者的成員變量。我可能對'最後'部分錯了。 – Shubhayu 2012-04-11 07:48:33

+0

是的。我用整個getView()編輯了我的原始文章。看看最後幾條線 – CodePrimate 2012-04-11 07:49:32

相關問題