RecyclerView中的CheckBox状态

时间:2016-02-08 09:48:04

标签: java android checkbox request

我创建了一个Checkbox,但是,每当我点击几个Items随机复选框时,都会被点击。我有一个Recycler视图,我也有一个适配器类。这是我的CheckBox方法可以有人告诉我这个问题吗?

public void CheckBox(View view) {
    final CheckBox checkBox = (CheckBox)view;
    if (checkBox.isChecked()) {

        System.out.println("SET TO CHECKED");
        //Input instance of selected course(CHECKED)
        // TODO: 2/5/16 Need to pass in Persisted ID
        mVolleySingleton = VolleySingleton.getInstance();
        mRequestQueue = mVolleySingleton.getRequestQueue();
        CharSequence ID_PUT_COURSES = ((TextView) ((RelativeLayout) view.getParent()).getChildAt(1)).getText();
        System.out.println(PUT);

        String URL_PUT_COURSES = "URL"+ID_PUT+"\n";
        System.out.print(PUT);
        StringRequest UrlPut = new StringRequest(Request.Method.PUT, URL_PUT, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                System.out.println(response + "reponse");

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                System.out.println("************Answer" + error + "error");
            }
        });
        mRequestQueue.add(UrlPutCourses);
        System.out.println("Added");
    }

else{
        System.out.println("SET TO UNCHECKED");
        //delete instance of selected course(UNCHECKED)
        mVolleySingleton = VolleySingleton.getInstance();
        mRequestQueue = mVolleySingleton.getRequestQueue();
        // TODO: 2/5/16 Need to pass in Persisted ID
        CharSequence DELETE = ((TextView) ((RelativeLayout) view.getParent()).getChildAt(1)).getText();
        System.out.print(ID_PUT_COURSES);
        String UR_DELETE = "URL"+ DELETE;
        StringRequest UrlDeleteCourses = new StringRequest(Request.Method.DELETE, UR_DELETE, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                System.out.println(response + "reponse");
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                System.out.println("************Answer" + error + "error");
            }
        });
        mRequestQueue.add(UR_DELETE);
        System.out.println("Deleted");
    }

}



ublic class AdapterSearch extends RecyclerView.Adapter<AdapterSearch.ViewSearch>{
private LayoutInflater mLayoutInflater;
private ArrayList<Search> ListSearch=new ArrayList<>();
public AdapterSearch(Context context){

    mLayoutInflater=LayoutInflater.from(context);

}
public void setListSearch(ArrayList<Search> ListSearch){
    this.ListSearch=ListSearch;
    notifyItemRangeChanged(0,ListSearch.size());
}

@Override
public ViewSearch onCreateViewHolder(ViewGroup parent, int viewType) {
    View view= mLayoutInflater.inflate(R.layout.custom_search,(ViewGroup)null);
    ViewSearch holder=new ViewSearch(view);
    return holder;
}

@Override
public void onBindViewHolder(ViewSearch holder, int position) {
    Search currentSearch=ListSearch.get(position);
    holder.mSearchText.setText(currentSearch.getMtitle());
    holder.mAnswerPointsSearch.setText(currentSearch.getMkey());
    holder.mSearchId.setText(currentSearch.getMid());
    holder.mCourseId.setText(currentSearch.getCourseId());
}

@Override
public int getItemCount() {
    return ListSearch.size();
}
public void setFilter(ArrayList<Search> Search) {
    ListSearch = new ArrayList<>();
    ListSearch.addAll(Search);
    notifyDataSetChanged();
}

static class ViewSearch extends RecyclerView.ViewHolder{
    private TextView mSearchText;
    private TextView mAnswerPointsSearch;
    private TextView mSearchId;
    private TextView mCourseId;



    public ViewSearch (View view){
        super(view);
        mSearchText=(TextView)itemView.findViewById(R.id.SearchText);
        mAnswerPointsSearch=(TextView)itemView.findViewById(R.id.AnswerPointsSearch);
        mSearchId=(TextView)itemView.findViewById(R.id.SearchId);
        mCourseId=(TextView)itemView.findViewById(R.id.CourseTextView);



    }
}

}

1 个答案:

答案 0 :(得分:0)

晚了好,我从来没有说过,这可能有助于其他人稍后绊倒这个问题,所以这里就是这样。

这可能不是最好的解决方案,但它应该有效。

Search课程中,您有一些变量,例如titlekey。将boolean isChecked添加到其中,默认情况下将其设为false。还要创建匹配的getter和setter。 (我在此示例中使用isChecked()&amp; setChecked()

onBindViewHolder()方法中,添加以下行:

CheckBox checkBox = (CheckBox) itemView.findViewById(R.id.CheckBoxID);
// Instantiating the checkbox

holder.checkBox.setChecked(currentSearch.isChecked());
// Every time the holder (one list item) is shown, this gets called. 
// It sets the checked state based on what's stored in currentSearch.

checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (checkBox.isChecked()) {
                currentSearch.setChecked(true)
            } else {
                currentSearch.setChecked(false);
            }
        });
}
// This listens for clicks on the checkbox, and changes the checked state of
// the boolean in currentSearch to the correct state based on 
// what it already was on.

请注意,之前我没有使用过复选框,所以有些事情(比如设置检查状态)可能与我上面描述的略有不同,但这应该是一个工作基础。

您也可能希望复选框只能坚持到您单击某个按钮或类似的东西,然后重置它们。您可能希望为此添加一个简单的for循环,例如:

public void resetAllCheckBoxes() {
    for (Search s : ListSearch) {
        s.setChecked(false);
    }
}