滚动listView时,CheckBox设置未选中。我的解决方案干扰了whit setOnCheckedChangeListener

时间:2015-11-25 10:58:23

标签: android listview checkbox android-checkbox

我有一个ListView有很多CheckBox个,当我们在真实时检查TextToSpeech。现在的问题是,当我滚动ListView时,我检查过的所有CheckBox都被重置为未选中状态。所以找到了解决方案的任何教程。但是使用我使用的解决方案,CheckBox的状态将保存在变量中,并且当CheckBox再次可见时会重新检查。所以TextToSpeech再次开始。这是我的代码(我知道这不是一个好代码):

public class MyAdapter extends ArrayAdapter<Model> {
private TreeSet mSeparatorsSet = new TreeSet();
private final List<Model> list;
private final Activity context;
public String textToSay;

public MyAdapter(Activity context, List<Model> list) {
    super(context, R.layout.row, list);

    this.context = context;
    this.list = list;
}

@Override
public int getItemViewType(int position) {
    if (list.get(position).getVersion() == 0) {
        return 0;
    }
    return 1;
}

@Override
public int getViewTypeCount() {
    return 2;
}

static class ViewHolder {
    public TextView text;
    public TextView text2;
    public CheckBox checkbox;
    public TextView textSeparator;
}

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

    if (this.getItemViewType(position)== 0){
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            viewHolder = new ViewHolder();
            convertView = inflator.inflate(R.layout.title_row, null);
            viewHolder.textSeparator = (TextView) convertView.findViewById(R.id.textView);

            convertView.setTag(viewHolder);
            convertView.setTag(R.id.textView, viewHolder.textSeparator);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.textSeparator.setText(list.get(position).getName());
        return convertView;
    }

    if (convertView == null) {
        LayoutInflater inflator = context.getLayoutInflater();
        convertView = inflator.inflate(R.layout.row, null);
        viewHolder = new ViewHolder();

        viewHolder.text = (TextView) convertView.findViewById(R.id.label);
        viewHolder.text2 = (TextView)convertView.findViewById(R.id.label2);
        viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.checkBox);

        convertView.setTag(viewHolder);
        convertView.setTag(R.id.label, viewHolder.text);
        convertView.setTag(R.id.checkBox, viewHolder.checkbox);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    viewHolder.checkbox.setTag(position); // This line is important.
    viewHolder.text.setText(list.get(position).getName());
    viewHolder.text2.setText(list.get(position).getName2());
    viewHolder.checkbox.setChecked(list.get(position).isSelected());
    final CheckBox chk = (CheckBox)convertView.findViewById(R.id.checkBox);
    final View finalConvertView = convertView;

    chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int getPosition = (Integer) buttonView.getTag();
            list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.

            if (isChecked) {
                String textPart1 = " ";
                String textPart2 = " ";
                String textToSay = " ";

                TextView label2 = (TextView) finalConvertView.findViewById(R.id.label2);
                TextView label1 = (TextView) finalConvertView.findViewById(R.id.label);



                textPart1 = label2.getText().toString();
                if ((getPosition + 1) < list.size()) {
                    if(list.get(getPosition+1).getVersion() == 0){
                        textPart2 = list.get(getPosition + 2).getName();
                    } else {
                        textPart2 = list.get(getPosition + 1).getName();
                    }
                }
                textToSay = textPart1 +
                        "\n" + "." +
                        "\n" + "." +
                        "\n" + "." +
                        "\n" + "." +
                        "\n" + "." +
                        "\n" + "." +
                        "\n" + "." +
                        "\n" + "." +
                        "\n" + "." +
                        "\n" + "." +
                        "\n" + "." +
                        textPart2;

                OneFragment.startSpeak(textToSay);

            }
        }

    });
    return convertView;
}
}

所以任何人都知道如何解决我的问题?

更新

我找到了解决方案。我使用方法onCheckedChange()来存储结果,并使用onClickListener来存储事件。这是新代码:

 public class MyAdapter extends ArrayAdapter<Model> {


private TreeSet mSeparatorsSet = new TreeSet();
private final List<Model> list;
private final Activity context;
public String textToSay;
private int mCurrentTtsPosition = -1;

public MyAdapter(Activity context, List<Model> list) {
    super(context, R.layout.row, list);

    this.context = context;
    this.list = list;
}

@Override
public int getItemViewType(int position) {
    if (list.get(position).getVersion() == 0) {
        return 0;
    }
    return 1;
}

@Override
public int getViewTypeCount() {
    return 2;
}

public void onStopTts(){
    mCurrentTtsPosition = -1;
    notifyDataSetChanged();
}

static class ViewHolder {
    public TextView text;
    public TextView text2;
    public CheckBox checkbox;
    public TextView textSeparator;
}

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

    if (this.getItemViewType(position)== 0){
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            viewHolder = new ViewHolder();
            convertView = inflator.inflate(R.layout.title_row, null);
            viewHolder.textSeparator = (TextView) convertView.findViewById(R.id.textView);

            convertView.setTag(viewHolder);
            convertView.setTag(R.id.textView, viewHolder.textSeparator);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.textSeparator.setText(list.get(position).getName());
        return convertView;
    }

    if (convertView == null) {
        LayoutInflater inflator = context.getLayoutInflater();
        convertView = inflator.inflate(R.layout.row, null);
        viewHolder = new ViewHolder();

        viewHolder.text = (TextView) convertView.findViewById(R.id.label);
        viewHolder.text2 = (TextView)convertView.findViewById(R.id.label2);
        viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.checkBox);

        convertView.setTag(viewHolder);
        convertView.setTag(R.id.label, viewHolder.text);
        convertView.setTag(R.id.checkBox, viewHolder.checkbox);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    viewHolder.checkbox.setTag(position); // This line is important.
    viewHolder.text.setText(list.get(position).getName());
    viewHolder.text2.setText(list.get(position).getName2());
    viewHolder.checkbox.setChecked(list.get(position).isSelected());
    viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int getPosition = (Integer) buttonView.getTag();
            list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
        }
    });
    final CheckBox chk = (CheckBox)convertView.findViewById(R.id.checkBox);
    final View finalConvertView = convertView;

    if (position == mCurrentTtsPosition){
        chk.setChecked(true);
    }

    chk.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int getPosition = (Integer) chk.getTag();


            if (chk.isChecked()) {
                //  mCurrentTtsPosition = getPosition;

                String textPart1 = " ";
                String textPart2 = " ";
                String textToSay = " ";

                TextView label2 = (TextView) finalConvertView.findViewById(R.id.label2);
                TextView label1 = (TextView) finalConvertView.findViewById(R.id.label);


                textPart1 = label2.getText().toString();
                if ((getPosition + 1) < list.size()) {
                    if (list.get(getPosition + 1).getVersion() == 0) {
                        textPart2 = list.get(getPosition + 2).getName();
                    } else {
                        textPart2 = list.get(getPosition + 1).getName();
                    }
                }
                textToSay = textPart1 +
                        "\n" + "." +
                        "\n" + "." +
                        "\n" + "." +
                        "\n" + "." +
                        "\n" + "." +
                        "\n" + "." +
                        "\n" + "." +
                        "\n" + "." +
                        "\n" + "." +
                        "\n" + "." +
                        "\n" + "." +
                        textPart2;

                OneFragment.startSpeak(textToSay);

            }
        }
    });

    return convertView;
}
}

2 个答案:

答案 0 :(得分:0)

如果mCurrentTtsPosition!= currentPosition,则在调用startSpeak之前将位置存储在global int(mCurrentTtsPosition)中。使用此mCurrentTtsPosition重新检查getView上的视图,但在这种情况下,请不要调用startSpeak。在播放结束时,使用您的片段调用适配器的公共方法将mCurrentTtsPosition重置为-1。

public class MyAdapter extends ArrayAdapter<Model> {


private TreeSet mSeparatorsSet = new TreeSet();
  private final List<Model> list;
  private final Activity context;
  public String textToSay;
  private int mCurrentTtsPosition = -1;

  public MyAdapter(Activity context, List<Model> list) {
      super(context, R.layout.row, list);

      this.context = context;
      this.list = list;
  }

  @Override
  public int getItemViewType(int position) {
      if (list.get(position).getVersion() == 0) {
          return 0;
      }
      return 1;
  }

  @Override
  public int getViewTypeCount() {
      return 2;
  }

  public void onStopTts(){
    mCurrentTtsPosition = -1;
    notifyDataSetChanged();
  }

  static class ViewHolder {
      public TextView text;
      public TextView text2;
      public CheckBox checkbox;
      public TextView textSeparator;
  }

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

      if (this.getItemViewType(position)== 0){
          if (convertView == null) {
              LayoutInflater inflator = context.getLayoutInflater();
              viewHolder = new ViewHolder();
              convertView = inflator.inflate(R.layout.title_row, null);
              viewHolder.textSeparator = (TextView) convertView.findViewById(R.id.textView);

              convertView.setTag(viewHolder);
              convertView.setTag(R.id.textView, viewHolder.textSeparator);

          } else {
              viewHolder = (ViewHolder) convertView.getTag();
          }
          viewHolder.textSeparator.setText(list.get(position).getName());
          return convertView;
      }

      if (convertView == null) {
          LayoutInflater inflator = context.getLayoutInflater();
          convertView = inflator.inflate(R.layout.row, null);
          viewHolder = new ViewHolder();

          viewHolder.text = (TextView) convertView.findViewById(R.id.label);
          viewHolder.text2 = (TextView)convertView.findViewById(R.id.label2);
          viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.checkBox);

          convertView.setTag(viewHolder);
          convertView.setTag(R.id.label, viewHolder.text);
          convertView.setTag(R.id.checkBox, viewHolder.checkbox);
      } else {
          viewHolder = (ViewHolder) convertView.getTag();
      }

      viewHolder.checkbox.setTag(position); // This line is important.
      viewHolder.text.setText(list.get(position).getName());
      viewHolder.text2.setText(list.get(position).getName2());
      viewHolder.checkbox.setChecked(list.get(position).isSelected());
      final CheckBox chk = (CheckBox)convertView.findViewById(R.id.checkBox);
      final View finalConvertView = convertView;

      if (position == mCurrentTtsPosition){
        chk.setChecked(true);
      }

      chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
              int getPosition = (Integer) buttonView.getTag();
              list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.

              if (isChecked && getPosition != mCurrentTtsPosition) {
                  mCurrentTtsPosition = getPosition;

                  String textPart1 = " ";
                  String textPart2 = " ";
                  String textToSay = " ";

                  TextView label2 = (TextView) finalConvertView.findViewById(R.id.label2);
                  TextView label1 = (TextView) finalConvertView.findViewById(R.id.label);



                  textPart1 = label2.getText().toString();
                  if ((getPosition + 1) < list.size()) {
                      if(list.get(getPosition+1).getVersion() == 0){
                          textPart2 = list.get(getPosition + 2).getName();
                      } else {
                          textPart2 = list.get(getPosition + 1).getName();
                      }
                  }
                  textToSay = textPart1 +
                          "\n" + "." +
                          "\n" + "." +
                          "\n" + "." +
                          "\n" + "." +
                          "\n" + "." +
                          "\n" + "." +
                          "\n" + "." +
                          "\n" + "." +
                          "\n" + "." +
                          "\n" + "." +
                          "\n" + "." +
                          textPart2;

                  OneFragment.startSpeak(textToSay);

              }
          }

      });
      return convertView;
  }
}

答案 1 :(得分:0)

嗨请在列表中再添加一个字段,例如&#34;已选择&#34;并将其设置为false并检查如下。也改变&#34;选择&#34;的价值如果选中,则为true。

更改getView(),如下所示。

if(list.get(position).getSelected()){
// set the checkbox selected
}else{
// set the checkbox unselected
}

并在setOnCheckedChangeListener()中添加以下内容

if(isChecked){
list.get(position).put("selected",true));
}else{
list.get(position).put("selected",false));
}