简体   繁体   中英

How Do I call API in recycler View with the help of radio button

today I try to call api to display recycleView with the help of 2(male-female) RadioButton .

Here 2 RadioButton and behind "choose your gender" recycleView hide

在此处输入图片说明

After api call my recycleView Item shown this

在此处输入图片说明

"what happing in api:- (i) I have some item(locker) list, which is shown as image

(ii) after api call i have information of some locker(item) which is locked.

(iii) I want show locked locker with some diff color and unlocked locker with some diff. color. (Currently all are in same color, after api call also)

(iv)Now I want to just check my code and let me understand where am I wrong.

(v) I am new in android and want to realize my fault.

 //this is male button code(same female)
    rgGender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            selectedId = rgGender.getCheckedRadioButtonId();
            switch (selectedId) {
                case R.id.rb_male: {
                    booleanLockerList = new ArrayList<>();
                    lockerModels = new ArrayList<LockerModel>();
                    BooleanModelClass booleanLocker = new BooleanModelClass();

                    for (int i = 0; i < malelockerNumber.size(); i++) {
                        booleanLocker.setId("");
                        booleanLocker.setCardid("");
                        booleanLocker.setCby("");
                        booleanLocker.setCdate("");
                        booleanLocker.setJimid("");
                        booleanLocker.setStatus("");
                        booleanLocker.setExittime("");
                        booleanLocker.setIntime("");
                        booleanLocker.setLocker_num("m" + String.valueOf(i+1));
                        booleanLocker.setGender("m");
                        booleanLocker.setKey(false);
                        booleanLockerList.add(booleanLocker);
                    }
                    textHide.setVisibility(View.GONE);
                    gender = "1";
                    String url="http://api.vyosystech.com/safra.php/islockerfree";
                    StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                            new Response.Listener<String>()
                            {
                                @Override
                                public void onResponse(String response)
                                {
                                    Log.d("Correct API",response);
                                    JSONObject jsonObject=null;
                                    JSONObject jsonArrayObject=null;


                                    try {
                                        jsonObject=new JSONObject(response);
                                        String msg=jsonObject.getString("msg");
                                        String code=jsonObject.getString("code");
                                        if((msg.equals("Locker booked")&&(code.equals("200"))))
                                        {
                                            JSONArray jsonData=jsonObject.getJSONArray("data");
                                            int totalLocker=jsonData.length();
                                            Log.d("totallocker",String.valueOf(totalLocker));
                                            if(totalLocker!=0)
                                            {
                                                for(int i=0;i<totalLocker;i++) {

                                                    jsonArrayObject = jsonData.getJSONObject(i);
                                                    LockerModel lockerModel = new LockerModel();
                                                    lockerModel.setId(jsonArrayObject.getString("id"));
                                                    lockerModel.setCardid(jsonArrayObject.getString("cardid"));
                                                    lockerModel.setIntime(jsonArrayObject.getString("intime"));
                                                    lockerModel.setExittime(jsonArrayObject.getString("exittime"));
                                                    lockerModel.setLocker_num(jsonArrayObject.getString("locker_num"));
                                                    lockerModel.setJimid(jsonArrayObject.getString("jimid"));
                                                    lockerModel.setStatus(jsonArrayObject.getString("status"));
                                                    lockerModel.setCby(jsonArrayObject.getString("cby"));
                                                    lockerModel.setGender(jsonArrayObject.getString("gender"));
                                                    lockerModel.setCdate(jsonArrayObject.getString("cdate"));
                                                    lockerModels.add(lockerModel);
                                                }
                                            }
                                            else
                                            {
                                                Toast.makeText(TempLockerBook.this,totalLocker+" Locker Are Booked",Toast.LENGTH_SHORT).show();
                                            }
                                        }
                                        else
                                        {
                                            Toast.makeText(TempLockerBook.this, msg, Toast.LENGTH_SHORT).show();
                                        }
                                    }

                                    catch(JSONException e)
                                    {
                                        e.printStackTrace();
                                    }


                                        if((lockerModels.size()!=0)&&(booleanLockerList.size()!=0))
                                        {
                                            for(int i=0;i<lockerModels.size();i++)
                                            {

                                                for(int j=0;j<booleanLockerList.size();j++)
                                                {
                                                    if(lockerModels.get(i).getLocker_num().equals(booleanLockerList.get(j).getLocker_num()))
                                                    {
                                                        BooleanModelClass boo = new BooleanModelClass();
                                                        boo.setKey(true);
                                                        booleanLockerList.add(boo);
                                                        break;
                                                    }

                                                }

                                            }
                                            Toast.makeText(TempLockerBook.this, lockerModels.size()+" already booked", Toast.LENGTH_SHORT).show();

                                        }
                                        else if(lockerModels.size()==0) {
                                            Toast.makeText(TempLockerBook.this, "lockerModels list error", Toast.LENGTH_SHORT).show();
                                        }
                                        else if(booleanLockerList.size()==0) {
                                            Toast.makeText(TempLockerBook.this, "booleanlist list error", Toast.LENGTH_SHORT).show();
                                        }
                                        else
                                        {
                                            Toast.makeText(TempLockerBook.this, "other error", Toast.LENGTH_SHORT).show();
                                        }

                                }
                            },
                            new Response.ErrorListener()
                            {
                                @Override
                                public void onErrorResponse(VolleyError error)
                                {
                                    error.printStackTrace();

                                    Toast.makeText(getApplicationContext(),"Something Wrong",Toast.LENGTH_LONG).show();
                                }
                            }){
                        @Override
                        protected Map<String,String> getParams()
                        {
                            Map<String,String> params = new HashMap<String, String>();
                            params.put("key", "2509ncta709s2qo6p54zhewrfev467ha");
                            params.put("jimid", "0");
                            params.put("gender", "1");
                            return params;
                        }

                    };

                    RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
                    requestQueue.add(stringRequest);

                    for (int fill = 0; fill < apiSize; fill++) {
                        for (int fill2 = 0; fill2 < staticSize; fill2++) {
                            if (booleanLockerList.get(fill).getLocker_num().equals(lockerModels.get(fill2).getLocker_num()))
                                ;
                            {
                                booleanLockerList.get(fill2).setKey(true);
                                count++;
                            }
                        }
                    }
                    setAdapterLocation();
                    break;
                }

(vi) now this is my adapter class-

 public void onBindViewHolder(final TempAdapter.ViewHolder holder, final int position) {


    holder.myTextView.setText(lockerNumber.get(position));
    Boolean first1=booleanLockerList.get(position).getKey();
    if((first1))
    {
        holder.click.setBackgroundColor(context.getResources().getColor(R.color.blue));

    }
    else
    {
        holder.click.setBackground(context.getResources().getDrawable(R.drawable.border));
    }

(vii) Here(currently) first adapter run and then api hit. I din't recognize the error(not error) but not exact working.

如果要在获取数据后重新绑定适配器,则需要将新数据传递给适配器,然后调用notifyDataSetChanged();。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM