Android:如何在RecyclerView中获取复选框的选中项以将其发送到服务器

标签 android android-recyclerview android-checkbox

我正在尝试选择 checkbox 的选中项目,获取选中项目的位置,以便将其发送到服务器。

我的适配器是:

public class FollowTopicsAdapter extends RecyclerView.Adapter<FollowTopicsAdapter.MyViewHolder>  {

    ArrayList<String> topics_title, topics_id;
    ArrayList<String> checked_items= new ArrayList<>();
    Context context;
    LinearLayout linearLayout;
    CheckBox checkBox;

    public FollowTopicsAdapter(Context context, ArrayList<String> topics_title, ArrayList<String> topics_id){
        this.topics_title=topics_title;
        this.context=context;
        this.topics_id=topics_id;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_for_follow_topics,parent,false);

        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.checkBox.setText(topics_title.get(position));
        holder.checkBox.setTag(topics_title.get(position));

        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b)
                {
                    checked_items.add(position,topics_id.get(position));
                    holder.checkBox.setChecked(b);
                    linearLayout.setBackgroundResource(R.drawable.checked);
                }
                else
                {
                    checked_items.remove(position);
                    holder.checkBox.setChecked(b);
                    linearLayout.setBackgroundResource(R.drawable.custom_checkbox);
                }

            }
        });
    }


    @Override
    public int getItemCount() {
        return topics_title.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        CheckBox checkBox;

        public MyViewHolder(View view) {
            super(view);
            linearLayout=(LinearLayout)itemView.findViewById(R.id.linearLayout);
            checkBox= (CheckBox)itemView.findViewById(R.id.checkbox);
        }
    }
}

最佳答案

是的,我得到了答案:

public class AdapterForFollowTopics extends BaseAdapter {

    private Context context;
    ArrayList<String> topics_title, topics_id;
    ArrayList<String> checked_items= new ArrayList<>();
    private static LayoutInflater inflater=null;
    ArrayList<String> lstChk= new ArrayList<>();

    public AdapterForFollowTopics(Context context, ArrayList<String> topics_title, ArrayList<String> topics_id){
        this.context = context;
        this.topics_title = topics_title;
        this.topics_id= topics_id;
        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

    @Override
    public Object getItem(int i) {
        return topics_title.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    public class Holder
    {
        CheckBox check;
        LinearLayout lv;
    }



    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final Holder holder = new Holder();
        final View rowView;

        rowView = inflater.inflate(R.layout.layout_for_follow_topics, null);
        holder.check = (CheckBox) rowView.findViewById(R.id.checkbox);
        holder.lv = (LinearLayout) rowView.findViewById(R.id.linearLayout);
        holder.check.setText(topics_title.get(position));
        rowView.setTag(holder);

        if(lstChk.contains(topics_title.get(position)))
        {
            holder.check.setChecked(true);
        }

        holder.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(compoundButton.isChecked())
                {
                    compoundButton.setChecked(true);
                    lstChk.add(topics_title.get(position));
                    holder.lv.setBackgroundResource(R.drawable.checked);

                }
                else
                {
                    compoundButton.setChecked(false);
                    lstChk.remove(topics_title.get(position));
                    holder.lv.setBackgroundResource(R.drawable.custom_checkbox);
                }
            }
        });

        return rowView;

    }
}

关于Android:如何在RecyclerView中获取复选框的选中项以将其发送到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37853111/

相关文章:

Android中"UserManger.isUserAGoat()"的合适案例

android - 访问数据库时遇到问题

java - Recycler View 中的 Imageview 在每个位置加载相同的图像

android - Activity 中的复选框状态更改

java - 具有 onclick 功能的动态复选框列表

android - Android ActionBar 中的操作项不可点击

android - GridView 项目内的 RecyclerView 破坏了 GridView 的 OnClickListener

java - Android 不知道如何修复此索引越界异常 FirebaseRecyclerview

android - RecyclerView 未显示

java - 填充微调器