使用参数通过android volley库使用JsonArrayRequest从mysql获取详细信息

时间:2018-08-17 10:31:24

标签: android-studio mysqli android-volley

根据此视频,我想从mysql获取详细信息以回收view。 android volley tutorial(json arrayrequest我写代码。但是表中的所有数据都在回收视图中显示。我想使用参数过滤数据。如何将参数传递给api.i使用php制作api

  

Background.java

public class BackgroundTask {

   // DayPlanActivity dayPlanActivity=new DayPlanActivity();


    Context contaxt;
    ArrayList<gethourdetails> arrayList =new ArrayList<>();
    public BackgroundTask(Context contaxt)
    {
        this.contaxt=contaxt;
    }
    public ArrayList<gethourdetails> getlist()
    {
        JsonArrayRequest jsonArrayRequest=new JsonArrayRequest(Request.Method.POST, Constants.GETDALYDETAILS_URL,null,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        int count=0;
                        while (count<response.length())
                        {
                            try {
                                JSONObject jsonObject=response.getJSONObject(count);
                                gethourdetails getdet=new gethourdetails(jsonObject.getString("hour_code"),jsonObject.getString("qrytime"),jsonObject.getString("hourqty"));
                                arrayList.add(getdet);
                                count++;
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(contaxt,"Error...",Toast.LENGTH_SHORT).show();
                error.printStackTrace();
            }
        }
        )
        ;
        MySingleton.getInstance(contaxt).addToRequestque(jsonArrayRequest);
        return arrayList;
    }
}
  

gethourdetails.java

public class gethourdetails {

    private String hour_code;
    private String hrs_qty;
    private String qrytime;

    public gethourdetails(String hour_code, String hrs_qty, String qrytime) {
        this.hour_code = hour_code;
        this.hrs_qty = hrs_qty;
        this.qrytime = qrytime;
    }

    public String getHour_code() {
        return hour_code;
    }

    public void setHour_code(String hour_code) {
        this.hour_code = hour_code;
    }

    public String getHrs_qty() {
        return hrs_qty;
    }

    public void setHrs_qty(String hrs_qty) {
        this.hrs_qty = hrs_qty;
    }

    public String getQrytime() {
        return qrytime;
    }

    public void setQrytime(String qrytime) {
        this.qrytime = qrytime;
    }
}
  

ContactAdapter

public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.MyviewHolder> {

    ArrayList<gethourdetails > arrayList = new ArrayList<>();
    public ContactAdapter(ArrayList<gethourdetails > arrayList)
    {
        this.arrayList=arrayList;
    }

    @Override
    public MyviewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout,parent,false);
        MyviewHolder myviewHolder=new MyviewHolder(view);
        return myviewHolder;
    }

    @Override
    public void onBindViewHolder(MyviewHolder holder, int position) {
                  holder.hourcode.setText(arrayList.get(position).getHour_code());
                  holder.datetime.setText(arrayList.get(position).getQrytime());
                  holder.product.setText(arrayList.get(position).getHrs_qty());
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }
    public static class MyviewHolder extends RecyclerView.ViewHolder
    {
        TextView hourcode,datetime,product;
        public MyviewHolder(View itemView) {
            super(itemView);
            hourcode=(TextView)itemView.findViewById(R.id.tx_hourcode);
            datetime=(TextView)itemView.findViewById(R.id.tx_datetime);
            product=(TextView)itemView.findViewById(R.id.tx_product);
        }
    }
  

DayplanActivity.java

recyclerView=(RecyclerView)findViewById(R.id.reViewdet);
        layoutManager=new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        BackgroundTask backgroundTask=new BackgroundTask(DayPlanActivity.this);
        arrayList=backgroundTask.getlist();
        adapter=new ContactAdapter(arrayList);
        recyclerView.setAdapter(adapter);

0 个答案:

没有答案