2017-06-13 55 views
0

嗨,大家好,我試圖設置在xml文件表示ListViewitem_todo.xml)的單行聲明的CheckBoxOnCheckedChangeListener,在這裏複選框的代碼:如何設置OnCheckedChangeListener到小區的複選框,在ListView

<CheckBox 
    android:id="@+id/checkBox1" 
    android:layout_width="40dp" 
    android:layout_height="40dp" 
    android:layout_gravity="center_vertical" 
    android:text="" /> 

這裏的列表視圖的代碼的部分(fragment_bacheca.xml):

<ListView 
     android:id="@+id/listView1" 
     android:layout_marginTop="20dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

然後我用一個自定義適配器:`

public class TaskListAdapter extends ArrayAdapter<TaskCell>{ 

    public TaskListAdapter(Context context, int textViewResourceId, List<TaskCell> objects){ 
     super(context, textViewResourceId, objects); 
    } 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = (LayoutInflater) getContext() 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(R.layout.item_todo, null); 
     TextView task_subject = (TextView) convertView.findViewById(R.id.task_subject); 
     TextView task_type = (TextView) convertView.findViewById(R.id.task_type); 
     TextView task_data = (TextView) convertView.findViewById(R.id.task_data); 
     CheckBox cb = (CheckBox) convertView.findViewById(R.id.checkBox1); 

     TaskCell t = getItem(position); 
     task_subject.setText(t.getMateria()); 
     task_type.setText(t.getTipo()); 
     task_data.setText(t.getData()); 
     if(t.getFatto().equals("1")) { 
      cb.setChecked(true); 
      task_subject.setPaintFlags(task_subject.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); 
     } 
     else{ 
      cb.setChecked(false); 
     } 

     return convertView; 
    } 

TaskCell對於todo_item的數據的類。

在這裏,我呼籲聽衆片段的代碼:

taskView = getActivity().getLayoutInflater().inflate(R.layout.item_todo, null); 
    checkbox_task = (CheckBox) taskView.findViewById(R.id.checkBox1); 

    checkbox_task.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton compoundButton, boolean b) { 
      adapter.notifyDataSetChanged(); 
     } 
    }); 

    list = new LinkedList<>(); 
    adapter = new TaskListAdapter(getContext(), R.layout.item_todo, list); 
    lv.setAdapter(adapter); 

的問題是,什麼都沒有發生,並與調試我看到onCheckedChanged永遠達不到。

任何人都知道爲什麼?謝謝大家

+1

它可能不是原因,而是你所提到的'todo_item.xml'作爲問題描述的文件名,但我在代碼中看到了'item_todo'。我在代碼中看不到'todo_item.xml'。 –

+0

我在解釋中犯了一個錯誤,我打算item_todo.xml – AESolutions

+0

'OnCheckedChangeListener()'應該在適配器內而不是片段內部實現。 –

回答

0

嘗試實現像這樣ViewHolder模式適配器類,

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 
    // Check if an existing view is being reused, otherwise inflate the view 
    if (convertView == null) { 
     convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_todo, parent, false); 
     holder = new ViewHolder(convertView); 
     ... 
     ... 
     holder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       buttonView.setChecked(isChecked); 
      } 
     }); 
     ... 
     ... 
     convertView.setTag(holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    // Get the data item for this position 
    TaskCell t = getItem(position); 
    ... 
    ... 
    if(t.getFatto().equals("1")) { 
     holder.cb.setChecked(true); 
    } else { 
     holder.cb.setChecked(false); 
    } 
    return convertView; 
} 

static class ViewHolder { 
    ... 
    ... 
    CheckBox cb; 

    ViewHolder(View v) { 
     checkBox = (CheckBox) v.findViewById(R.id.checkBox1); 
    } 
}