CustomAdapter - CheckedTextView

时间:2017-02-17 12:51:26

标签: android custom-adapter checkedtextview

我在CheckTextView中“记住”Checkbox的状态时遇到问题。 现在当我点击它时,它会改变状态,但是在不同的CheckBox上。

class CustomAdapterSmartwatch extends ArrayAdapter<String> {


static class ViewHolder {
    protected CheckedTextView cTextView;
}

ViewHolder holder;


public CustomAdapterSmartwatch(Context context, ArrayList<String> variables) {
    super(context,R.layout.row_smartwatch_resources,variables);
}

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        holder = new ViewHolder();
        LayoutInflater smartwatchinflater = LayoutInflater.from(getContext());
        convertView = smartwatchinflater.inflate(R.layout.row_smartwatch_resources,parent,false);
        holder.cTextView = (CheckedTextView) convertView.findViewById(R.id.row_smartwatch_checked);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.cTextView.setText(getItem(position));

    holder.cTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (holder.cTextView.isChecked()) {
                holder.cTextView.setChecked(false);
            } else {
                holder.cTextView.setChecked(true);
            }
        }
    });

    return convertView;
}

}

现在我想弄清楚使用Holder,但我听说可以使用SparseBooleanArray。有人可以解释如何使其有效吗?

编辑1 好的,我有ListView,由CustomAdapter控制。该适配器只包含1个项目(CheckedTextView)。 问题是滚动后这些复选框不记得它们的状态。 我看到你的答案有解释,但现在点击任何“CheckedTextView”后,他们的复选框状态不会改变。

编辑2

    @NonNull
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        holder = new ViewHolder();
        LayoutInflater smartwatchinflater = LayoutInflater.from(getContext());
        convertView = smartwatchinflater.inflate(R.layout.row_smartwatch_resources,parent,false);
        holder.cTextView = (CheckedTextView) convertView.findViewById(R.id.row_smartwatch_checked);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.cTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("MainActivity","Position = " + position);
            CheckedTextView checkedText = (CheckedTextView) v;
            checkedState.put(position, !checkedText.isChecked());

            Log.d("MainActivity","State = " + checkedState.get(position));

        }
    });

    holder.cTextView.setText(getItem(position));
    holder.cTextView.setChecked(checkedState.get(position));

    return convertView;
}

编辑3

class CustomAdapterSmartwatch extends ArrayAdapter<String> {

public SparseBooleanArray checkedState = new SparseBooleanArray();
private CheckedTextView cTextView;


public CustomAdapterSmartwatch(Context context, ArrayList<String> variables) {
    super(context,R.layout.row_smartwatch_resources,variables);
}

@NonNull
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    LayoutInflater smartwatchinflater = LayoutInflater.from(getContext());
    convertView = smartwatchinflater.inflate(R.layout.row_smartwatch_resources,parent,false);
    cTextView = (CheckedTextView) convertView.findViewById(R.id.row_smartwatch_checked);

    cTextView.setText(getItem(position));
    cTextView.setChecked(checkedState.get(position));

    cTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("MainActivity","Position = " + position);
            CheckedTextView checkedText = (CheckedTextView) v;
            checkedText.toggle();

            if (checkedText.isChecked())
                checkedState.put(position,true);
            else
                checkedState.delete(position);
            Log.d("MainActivity","State = " + checkedState.get(position));

        }
    });
    return convertView;
}

}

3 个答案:

答案 0 :(得分:0)

您必须为每个CheckTextView保留一个布尔标志。这可以是数组,List或SparseBooleanArray。您选择的数据结构应该是适配器的字段:

class CustomAdapterSmartwatch extends ArrayAdapter<String> {
    private SparseBooleanArray checkedState = new SparseBooleanArray();

    // ...
}

您不希望将其放入持有者,因为持有人仅在可见行中退役。另一方面,您希望保持每行的已检查状态,可见或不可见。

现在,当用户点击CheckedTextView

时,您可以设置存储的已检查状态
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // ...

    holder.cTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CheckedTextView checkedText = (CheckedTextView)v;
            checkedState.put(position, checkedText.isChecked());
        }
    });
}

每当您为视图充气或重新使用持有者时,您都会恢复已检查状态:

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // ...

    holder.cTextView.setText(getItem(position));
    holder.cTextView.setChecked(checkedState.get(position));

    // ...
}

答案 1 :(得分:-1)

使用以下更新替换您的代码:

class CustomAdapterSmartwatch extends ArrayAdapter<CustomAdapterSmartwatch.MyPojo> {

    class MyPojo {
        boolean checked;
        String data;

        public boolean isChecked() {
            return checked;
        }

        public void setChecked(boolean checked) {
            this.checked = checked;
        }

        public String getData() {
            return data;
        }

        public void setData(String data) {
            this.data = data;
        }
    }

    static class ViewHolder {
        protected CheckedTextView cTextView;
    }

    ViewHolder holder;


    public CustomAdapterSmartwatch(Context context, ArrayList<MyPojo> variables) {
        super(context, R.layout.row_smartwatch_resources, variables);
    }

    @Nullable
    @Override
    public MyPojo getItem(int position) {
        return super.getItem(position);
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            holder = new ViewHolder();
            LayoutInflater smartwatchinflater = LayoutInflater.from(getContext());
            convertView = smartwatchinflater.inflate(R.layout.row_smartwatch_resources,parent,false);
            holder.cTextView = (CheckedTextView) convertView.findViewById(R.id.row_smartwatch_checked);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final MyPojo item = getItem(position);

        holder.cTextView.setText(item.getData());
        holder.cTextView.setChecked(item.isChecked());

        holder.cTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean nextStatus = !item.isChecked();
                item.setChecked(nextStatus);
                holder.cTextView.setChecked(nextStatus);
            }
        });

        return convertView;
    }
}

答案 2 :(得分:-1)

您只需在onClick()方法中使用该视图。

holder.cTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(v instanceOf CheckedTextView) {
                CheckedTextView c = (CheckedTextView) v;
                c.setChecked(!c.isChecked());
            }
        }
    });