2014-03-28 218 views
0


我想創建一個帶有複選框的列表的BaseAdapter。問題是,當我嘗試獲取每個行項目的標記時,它將返回null。Android BaseAdapter:getTag()返回null

適配器:

import java.util.List; 

import android.app.Activity; 
import android.content.Context; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.CheckBox; 
import android.widget.TextView; 

public class TaskListAdapter extends BaseAdapter { 
    Context context; 
    List<Patient> rowItems; 

    public TaskListAdapter(Context context, List<Patient> items) { 
     super(); 
     this.context = context; 
     this.rowItems = items; 
    } 

    /*private view holder class*/ 
    private class ViewHolder { 
     TextView txtTitle; 
     TextView txtDesc; 
     CheckBox checkbox; 
    } 

    public View getView(final int position, View convertView, ViewGroup parent) { 
     ViewHolder holder = null; 

     LayoutInflater mInflater = (LayoutInflater) 
      context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
     if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.row, parent, false); 
      holder = new ViewHolder(); 
      holder.txtTitle = (TextView) convertView.findViewById(R.id.firstLine); 
      holder.txtDesc = (TextView) convertView.findViewById(R.id.secondLine); 
      holder.checkbox = (CheckBox)convertView.findViewById(R.id.checkbox); 
      convertView.setTag(holder); 

     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     Patient rowItem = (Patient) getItem(position); 

     holder.txtDesc.setText(rowItem.getName()); 
     holder.txtTitle.setText(rowItem.getAddress()); 
     holder.checkbox.setChecked(rowItem.isSelected()); 

     holder.checkbox.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       CheckBox checkbox = (CheckBox)v.findViewById(R.id.checkbox); 
       Patient rowItem = (Patient) checkbox.getTag(); 
         // rowItem is null now for some reason 
       rowItem.setSelected(checkbox.isChecked()); 
      } 
     }); 

     return convertView; 
    } 

    @Override 
    public int getCount() { 
     return rowItems.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return rowItems.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return rowItems.indexOf(getItem(position)); 
    } 
} 

和模型類:

public class Patient { 
    private String name; 
    private String address; 
    private boolean checked; 

    public Patient(String name, String address, boolean checked) { 
     super(); 
     this.name = name; 
     this.address = address; 
     this.checked = checked; 
    } 

    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public String getAddress() { 
     return address; 
    } 
    public void setAddress(String address) { 
     this.address = address; 
    } 
    public boolean isSelected() { 
     return checked; 
    } 
    public void setSelected(boolean checked) { 
     this.checked = checked; 
    } 

} 

我試圖讓每一行的標籤,所以我可以保存複選框狀態視圖時滾動。 謝謝。

+0

你能後的logcat的文件以及確切的行logcat狀態錯誤發生在? – Tukajo

回答

1

您從未真正爲您的CheckBox視圖(R.id.checkbox)設置標籤。至少沒有你分享的代碼在哪裏。所以OnClickListener中的rowItem只能爲null。解決方法:

holder.checkbox.setTag(rowItem); 

而不是設置患者作爲複選框的標籤,你也只是參考下圖所示的rowItem:

final Patient rowItem = (Patient) getItem(position); 

holder.txtDesc.setText(rowItem.getName()); 
holder.txtTitle.setText(rowItem.getAddress()); 
holder.checkbox.setChecked(rowItem.isSelected()); 

holder.checkbox.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     CheckBox checkbox = (CheckBox)v.findViewById(R.id.checkbox); 
     rowItem.setSelected(checkbox.isChecked()); 
    } 
}); 
+0

第一種方法就像一個魅力,謝謝!當我嘗試第二種方式(onCheckedChangeListener)時,它似乎並沒有保存複選框狀態,當列表滾動時。儘管如此,因爲我使用了onClick()方法,所以並不重要。 – user3261344

+0

其實對於你的情況,OnCheckedChangeListener會很糟糕。只要視圖被設置爲選中/取消選中,無論是通過編程方式還是通過用戶操作,都會調用onCheckChanged。所以你一定要爲你的情況使用OnClickListener。 (從答案中刪除該部分) – kgmaize