单击一张卡时,从另一个类更新数据库和刷新活动

时间:2017-09-19 04:32:31

标签: android api android-recyclerview android-volley cardview

我希望当任何人点击带有Status = undone的CardView时,应该运行API并更新数据库,并在RecyclerView中加载刷新的数据。

这是我的UpdateStatus.class,包含RecyclerView数据:

TextView Cid,Tno,Tname,Cono,Work;
private static final String 
URL_GETDATA="";
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private List<ListItem> listItems;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_update_status);
    Bundle bundle = getIntent().getExtras();
    String cid = bundle.getString("checkID");
    String tno = bundle.getString("trainno");
    String tname = bundle.getString("trainname");
    String cno = bundle.getString("coachno");
    String works = bundle.getString("work");

    Cid=(TextView)findViewById(R.id.idValue);
    Cid.setText(cid);

    Tno=(TextView)findViewById(R.id.tnoValue);
    Tno.setText(tno);

    Tname=(TextView)findViewById(R.id.tnameValue);
    Tname.setText(tname);

    Cono=(TextView)findViewById(R.id.cnoValue);
    Cono.setText(cno);

    Work=(TextView)findViewById(R.id.workValue);
    Work.setText(works);

    recyclerView=(RecyclerView)findViewById(R.id.recyclerview);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    listItems=new ArrayList<>();
    loadRecyclerViewData();
}

private void loadRecyclerViewData(){
    final AlertDialog dialog=new SpotsDialog(this, "Loading Data ...");
    dialog.show();

    StringRequest stringRequest=new StringRequest(Request.Method.POST, URL_GETDATA,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    dialog.dismiss();
                    Log.v("Response",response);
                    try {
                        JSONArray jsonArray=new JSONArray(response);
                        for (int i=0;i<jsonArray.length();i++){
                            ListItem item;
                            JSONObject jsonObject=jsonArray.getJSONObject(i);
                            if(jsonObject.getString("status").equals("done")){
                                item=new ListItem(
                                        jsonObject.getString("id"),
                                        jsonObject.getString("checkID"),
                                        jsonObject.getString("plannedDate"),
                                        jsonObject.getString("actualDate"),
                                        jsonObject.getString("status"),
                                        R.drawable.done
                                );
                            }else {
                                item=new ListItem(
                                        jsonObject.getString("id"),
                                        jsonObject.getString("checkID"),
                                        jsonObject.getString("plannedDate"),
                                        jsonObject.getString("actualDate"),
                                        jsonObject.getString("status"),
                                        R.drawable.update
                                );
                            }

                            listItems.add(item);
                        }
                        adapter=new MyAdapter(listItems,getApplicationContext());
                        recyclerView.setAdapter(adapter);

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

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    dialog.dismiss();
                    Toast.makeText(getApplicationContext(),error.getMessage(),Toast.LENGTH_LONG).show();
                }
            }){
        @Override
        protected Map<String, String> getParams() {
            Bundle bundle = getIntent().getExtras();
            // Posting params to login url
            Map<String, String> params = new HashMap<String, String>();
            params.put("checkID",bundle.getString("checkID"));
            return params;
        }
    };
    RequestQueue requestQueue= Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

这是我的Adapter.class,其中加载CardView数据onClick on Card状态被检查,并且API将使用Volley运行,UpdateStatus活动必须使用新数据刷新:

private List<ListItem> listItems;
private Context context;
private static final String 

public MyAdapter(List<ListItem> listItems, Context context) {
    this.listItems = listItems;
    this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
    final ListItem listItem=listItems.get(position);

    holder.planned.setText(listItem.getPlanned());
    holder.actual.setText(listItem.getActual());
    holder.status.setImageResource(listItem.getmImageResourceid());
    Intent i=new Intent(context,UpdateStatus.class);
    holder.status.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
                if(listItem.getStatus().equals("undone")){
                    //update code here
                }
                Toast.makeText(context,"You Clicked "+listItem.getStatus(),Toast.LENGTH_SHORT).show();
        }
    });

}

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

public class ViewHolder extends RecyclerView.ViewHolder{

    public TextView planned,actual;
    public ImageView status;
    public LinearLayout linearLayout;

    public ViewHolder(View itemView) {
        super(itemView);

        planned=(TextView)itemView.findViewById(R.id.planned_date);
        actual=(TextView)itemView.findViewById(R.id.actual_date);
        status=(ImageView)itemView.findViewById(R.id.status);
        linearLayout=(LinearLayout)itemView.findViewById(R.id.linearLayout);

    }
}

1 个答案:

答案 0 :(得分:0)

在OnResponse中,当你在recyclerView.notifyDataSetChanged()

之后向你的recyclerView添加一个新数据时调用方法recyclerView.setAdapter(adapter)

请参阅以下代码中的更改:

 @Override
            public void onResponse(String response) {
                dialog.dismiss();
                Log.v("Response",response);
                try {
                    JSONArray jsonArray=new JSONArray(response);
                    for (int i=0;i<jsonArray.length();i++){
                        ListItem item;
                        JSONObject jsonObject=jsonArray.getJSONObject(i);
                        if(jsonObject.getString("status").equals("done")){
                            item=new ListItem(
                                    jsonObject.getString("id"),
                                    jsonObject.getString("checkID"),
                                    jsonObject.getString("plannedDate"),
                                    jsonObject.getString("actualDate"),
                                    jsonObject.getString("status"),
                                    R.drawable.done
                            );
                        }else {
                            item=new ListItem(
                                    jsonObject.getString("id"),
                                    jsonObject.getString("checkID"),
                                    jsonObject.getString("plannedDate"),
                                    jsonObject.getString("actualDate"),
                                    jsonObject.getString("status"),
                                    R.drawable.update
                            );
                        }

                        listItems.add(item);
                    }
                    adapter=new MyAdapter(listItems,getApplicationContext());
                    recyclerView.setAdapter(adapter);
                    recyclerView.notifyDataSetChanged()

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

            }
        },

或者为了获得更好的用户体验,您可以使用recyclerView.notifyItemInserted(int position)代替recyclerView.notifyDataSetChanged()。见documentation