gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:58:37 26 4
gpt4 key购买 nike

我正在尝试选择 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com