ListViewの各行にCheckBoxを配置したレイアウトを作っています。
CheckBoxの状態を、ListViewを配置しているFragmentで確認するために、Adapter内でCheckBoxにsetListenerをして、CheckBoxが押された時に、その状態を、状態を保持するオブジェクトにsetしています。
ここまでは良いのですが、ListViewのCheckBoxが一つでもCheckされていたら、Fragment上のButtonの見た目を変えたいんです。
そのために、そのAdapterがsetされているListViewが置いてあるFragmentを参照したいんですが、どうにもやり方がわかりません。教えていただけるとありがたいです。
それとも、直接参照しない方法でやるものなんでしょうか?
Java
1public class ListOfTasksAdapter extends BaseAdapter { 2 3 private Context context; 4 private LayoutInflater layoutInflater = null; 5 private List<Todo> tasks; 6 private SimpleDateFormat simpleDateFormat; 7 public CheckBoxesStateHolder stateHolder = new CheckBoxesStateHolder(); 8 private CheckBox checkBox; 9 10 public ListOfTasksAdapter(Context context){ 11 this.context = context; 12 this.layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 13 this.tasks = new ArrayList<Todo>(); 14 } 15 16 public void setList(ArrayList<Todo> tasks){ 17 this.tasks = tasks; 18 } 19 20 21 22 @Override 23 public int getCount() { 24 return tasks.size(); 25 } 26 27 @Override 28 public Object getItem(int position) { 29 return tasks.get(position); 30 } 31 32 @Override 33 public long getItemId(int position) { 34 return tasks.get(position).getId(); 35 } 36 37 @Override 38 public View getView(int position, View convertView, ViewGroup parent) { 39 final int p = position; 40 41 convertView = layoutInflater.inflate(R.layout.layout_list_task_row, parent, false); 42 43 simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日(E) hh:mm"); 44 45 ((TextView)convertView.findViewById(R.id.listTextView1)).setText(tasks.get(position).getString()); 46 ((TextView)convertView.findViewById(R.id.listTextView2)).setText(simpleDateFormat.format(new Date(tasks.get(position).getNoticeDateTime()))); 47 checkBox = (CheckBox)convertView.findViewById(R.id.checkbox); 48 checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 49 @Override 50 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 51 stateHolder.setState(p,isChecked); 52 } 53 }); 54 55 checkBox.setChecked(stateHolder.getState(position)); 56 57 return convertView; 58 } 59 60} 61
Java
1public class CheckBoxesStateHolder { 2 private ArrayList<Boolean> list = new ArrayList<>(); 3 4 public void setState(int position, boolean isChecked){ 5 if(list.size() < (position+1)){ 6 this.expandSize(position+1); 7 } 8 list.set(position, isChecked); 9 } 10 11 public boolean getState(int position){ 12 return list.get(position); 13 } 14 15 public void expandSize(int size){ 16 while(list.size() < size){ 17 list.add(false); 18 } 19 } 20 21 public boolean hasCheckedBox(){ 22 boolean result = true; 23 if(list.indexOf(true) == -1){ 24 result = false; 25 } 26 return result; 27 } 28} 29
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。