成功上传图像后,从服务器刷新recycleview数据

时间:2017-02-03 09:46:00

标签: android android-recyclerview

我点击了recycleview中的图片并将新图片上传到服务器。如果图片上传成功,那么它应该反映在recycleview中。我正在使用此代码:

private void get_cardview_data(String response) {
        r_View_StudentInfo.setHasFixedSize(true);
        recyclerViewlayoutManager = new GridLayoutManager(this, 3);
        r_View_StudentInfo.setLayoutManager(recyclerViewlayoutManager);

        try {
            JSONArray jsonArray=new JSONArray(response);
            if(jsonArray.length()>0){
                for(int n = 0; n < jsonArray.length(); n++) {
                    getDataAdapter_studentInfo = new GetDataAdapter_StudentInfo();
                    JSONObject jsonObject = null;
                        jsonObject = jsonArray.getJSONObject(n);
                        image=  jsonObject.getString("pic_url");
                        if(image.equals("")){
                            getDataAdapter_studentInfo.setImageUrl(Config.NULL_IMAGE);
                        }
                        else {
                            getDataAdapter_studentInfo.setImageUrl(Config.SET_IMAGE+dbname+"/"+image);
                        }
                        getDataAdapter_studentInfo.setId(jsonObject.getInt(Config.JSON_ID));                       getDataAdapter_studentInfo.setSname(jsonObject.getString(Config.JSON_SNAME));
                        getDataAdapter_studentInfo.setRollno(jsonObject.getString(Config.JSON_ROLLNO));
                        getDataAdapter_studentInfo.setPname(jsonObject.getString(Config.JSON_PNAME));
                        getDataAdapter_studentInfo.setPmobile(jsonObject.getString(Config.JSON_PMOBILE));

                    getDataAdapter_studentInfos.add(getDataAdapter_studentInfo);
                    recyclerViewadapter = new RecyclerViewAdapter_StudentInfo(getDataAdapter_studentInfos, (Context) this);
              //      getDataAdapter_studentInfos.clear();
                    r_View_StudentInfo.setAdapter(recyclerViewadapter);
                    r_View_StudentInfo.getAdapter().notifyDataSetChanged();
                    text_nodata.setVisibility(View.GONE);
                }
            }
            else{
                text_nodata.setVisibility(View.VISIBLE);
            }

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

但这不是重装。如果我关闭应用程序然后启动它将显示上传的图像。请帮我解决。

public class RecyclerViewAdapter_StudentInfo extends RecyclerView.Adapter<RecyclerViewAdapter_StudentInfo.ViewHolder> {

    ImageLoader imageLoader;
    Context context;
    List<GetDataAdapter_StudentInfo> getDataAdapter_studentInfos;

    public RecyclerViewAdapter_StudentInfo(List<GetDataAdapter_StudentInfo> getDataAdapter, Context context){
        super();
        this.getDataAdapter_studentInfos = getDataAdapter;
        this.context = context;
    }

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

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        GetDataAdapter_StudentInfo getDataAdapter_document =  getDataAdapter_studentInfos.get(position);
         try {
            imageLoader = ServerImageParseAdapter.getInstance(context).getImageLoader();
            imageLoader.get(getDataAdapter_document.getImageUrl(),
                    ImageLoader.getImageListener(
                            holder.imageView,//Server Image
                            R.mipmap.ic_launcher,//Before loading server image the default showing image.
                           R.drawable.profile_image //Error image if requested image dose not found on server.
                    )
            );
            holder.imageView.setImageUrl(getDataAdapter_document.getImageUrl(), imageLoader);

          }catch (Exception e){ }


        holder.textView1.setText(String.valueOf(getDataAdapter_document.getId()));
        holder.textView2.setText(getDataAdapter_document.getSname());
        holder.textView3.setText(getDataAdapter_document.getRollno());
        holder.textView4.setText(getDataAdapter_document.getPname());
        holder.textView5.setText(getDataAdapter_document.getPmobile());
       // holder.imageView.setImageResource(getDataAdapter_document.getImageUrl());
    }

    public void refreshEvents(List<GetDataAdapter_StudentInfo> getDataAdapter_studentInfos) {
        this.getDataAdapter_studentInfos.clear();
        this.getDataAdapter_studentInfos.addAll(getDataAdapter_studentInfos);
        notifyDataSetChanged();
    }

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

    class ViewHolder extends RecyclerView.ViewHolder{

        public TextView textView1,textView2,textView3,textView4,textView5;
        public NetworkImageView imageView;
        public ViewHolder(View itemView) {
            super(itemView);

            imageView = (NetworkImageView)itemView.findViewById(R.id.student_image);
            textView1 = (TextView) itemView.findViewById(R.id.textView1) ;
            textView2 = (TextView) itemView.findViewById(R.id.textView2) ;
            textView3 = (TextView) itemView.findViewById(R.id.textView3) ;
            textView4 = (TextView) itemView.findViewById(R.id.textView4) ;
            textView5 = (TextView) itemView.findViewById(R.id.textView5) ;

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    GetDataAdapter_StudentInfo getDataAdapterDocument =  getDataAdapter_studentInfos.get(getAdapterPosition());
                    Intent intent=new Intent(context,StudentInfo_CardClick.class);
                    intent.putExtra("student_pic", getDataAdapterDocument.getImageUrl());
                    intent.putExtra("stud_name", getDataAdapterDocument.getSname());
                    intent.putExtra("stud_rollno", getDataAdapterDocument.getRollno());
                    intent.putExtra("stud_id", String.valueOf(getDataAdapterDocument.getId()));
                    intent.putExtra("stud_pname", getDataAdapterDocument.getPname());
                    intent.putExtra("stud_pmobile", getDataAdapterDocument.getPmobile());
                    context.startActivity(intent);

                }
            });


        }
    }
}




private void selected_section_id() {
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.C_SEND_SECTION_ID,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String response)
                    {
                        //Toast.makeText(Dashboard.this,response,Toast.LENGTH_LONG).show();
                  //      getDataAdapter_studentInfos.clear();
                        get_cardview_data(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(Dashboard.this,error.toString(),Toast.LENGTH_LONG).show();
                    }

                }){
            @Override
            protected Map<String,String> getParams(){
                Map<String,String> params = new HashMap<String, String>();
                params.put(Config.C_DB_NAME_D,dbname);
                params.put(Config.C_AC_YEAR_ID_SEC,ac_yearid);
                params.put(Config.C_CLASS_ID,value_class);
                params.put(Config.C_SECTION_ID,value_section);

                return params;
            }


        };
        RequestQueue requestQueue = Volley.newRequestQueue(Dashboard.this);
        requestQueue.add(stringRequest);
    }

2 个答案:

答案 0 :(得分:0)

从图像上传到服务器后获得的响应,执行此操作

if (image.equals("")) {
            getDataAdapter_studentInfo.setImageUrl(Config.NULL_IMAGE);
        } else {
            getDataAdapter_studentInfo.setImageUrl(Config.SET_IMAGE + dbname + "/" + image);
        }
        getDataAdapter_studentInfo.setId(jsonObject.getInt(Config.JSON_ID));
        getDataAdapter_studentInfo.setSname(jsonObject.getString(Config.JSON_SNAME));
        getDataAdapter_studentInfo.setRollno(jsonObject.getString(Config.JSON_ROLLNO));
        getDataAdapter_studentInfo.setPname(jsonObject.getString(Config.JSON_PNAME));
        getDataAdapter_studentInfo.setPmobile(jsonObject.getString(Config.JSON_PMOBILE));
        getDataAdapter_studentInfos.set(position, getDataAdapter_studentInfo);
        adapter.notifyDataSetChanged();

再次回忆所有列表api并在添加之前清除列表 所以它不会被追加并创造进一步的混乱

致电

....
            getDataAdapter_studentInfos.clear();
       if(jsonArray.length()>0){
                    for(int n = 0; n < jsonArray.length(); n++) {

....

答案 1 :(得分:0)

最后,我使用Glide代替Imageloader获取输出,并将属性设置为diskCacheStrategy(DiskCacheStrategy.NONE)skipMemoryCache(true)