0

I am clicking on an image in recycleview and uploading new image to the server.If the image uploads successfully then it should reflects in recycleview. I am using this code:

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();
        }
    }

But this is not reloading. If I close the app and then start it will be showing uploaded images.Please help me to resolve.

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);
    }
user
  • 11
  • 5
  • please do provide some more info. Whether you are newly creating adapter and adding items to recycler view ? – Dharma Sai Seerapu Feb 06 '17 at 12:56
  • @Tricky Solutions I am displaying data from json to recycleview and after that uploading new image to server. That newly updated image is not reflecting in the recycleview. I have edited my question with some more code.Please solve this problem. – user Feb 08 '17 at 05:20
  • Possible duplicate of [Best way to update data with a RecyclerView adapter](http://stackoverflow.com/questions/30053610/best-way-to-update-data-with-a-recyclerview-adapter) – rohitanand Feb 08 '17 at 05:58
  • @Sourav Ganguly why to use swap here? When I close app and again open then uploaded image will reflects.I think I have to change something in OnRessume().I tried to call same method once again inside OnRessume() but no use. – user Feb 08 '17 at 06:04
  • where exactly are you getting the json response – g7pro Feb 08 '17 at 06:08
  • @GIBIN THOMAS Calling selected_section_id() to get json response. – user Feb 08 '17 at 06:14

2 Answers2

0

from the response you get once image is uploaded to server, do this

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();

OR

Recall the all list api once more and clear the list before adding so it won't get appended and create further mess

by calling

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

....
g7pro
  • 757
  • 4
  • 11
  • I have used recyclerViewadapter.notifyDataSetChanged(); before r_View_StudentInfo.setAdapter(recyclerViewadapter); and also tried to to call api once again.I didnt get it..So I am not getting in which part of the code am doing mistake. – user Feb 08 '17 at 06:10
  • Does the response have the latest data? – g7pro Feb 08 '17 at 06:29
  • getDataAdapter_studentInfos.set(position,getDataAdapter_studentInfo); from the respons eyou get once image is uploaded – g7pro Feb 08 '17 at 06:31
  • Yes.When I upload new image old image is replacing with new image with the same name.I have checked. – user Feb 08 '17 at 06:33
  • go to the bottom and see whether it get appended to end of list completly,Simce you have't cleared the list before adding new values – g7pro Feb 08 '17 at 06:37
0

Finally I got the output using Glide instead of Imageloader and by setting properties as diskCacheStrategy(DiskCacheStrategy.NONE) and skipMemoryCache(true).

user
  • 11
  • 5