1

I have an app that calling an API its resulting around 500 rows creation.In my app the row content can update in the detail page. So after update is there any possibility to update the row in the Recycler View without calling the API again.

Activity

AsyncHttpClient client = new AsyncHttpClient();
client.get("URL", new AsyncHttpResponseHandler() {
@Override
 public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
try {                        
    String jsonStr = new String(responseBody, "UTF-8");
    if (jsonStr != null) {
    try {  
        JSONArray jsonArray = new JSONArray(jsonStr);
        JSONObject cStatus = jsonArray.getJSONObject(jsonArray.length()-1);
        for (int i = 0; i < jsonArray.length(); i++) 
        {
        JSONObject c = jsonArray.getJSONObject(i);
        String firstName = c.getString("firstName");
        String subDistributerId = c.getString("subDistributerId");
        SubdistributorItem item = new SubdistributorItem();
        item.setSubDistributorName(firstName);
        item.setSubDistributorId(subDistributerId);
        ubdistributorItemList.add(item);
        }
       } catch (JSONException e) {
         e.printStackTrace();
       }
     } 
     Collections.sort(subdistributorItemList, new Comparator<SubdistributorItem>() {
     public int compare(SubdistributorItem o1, SubdistributorItem o2) {
     return o1.getSubDistributorName().compareToIgnoreCase(o2.getSubDistributorName());
    }
   });
    adapter = new SubdistributorRecyclerAdapter(getActivity(),subdistributorItemList);
     mRecyclerView.setAdapter(adapter);
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
   }
  }               
});

Adapter Class

public class SubdistributorRecyclerAdapter extends RecyclerView.Adapter<SubdistributorListRowHolder>  {
private List<SubdistributorItem> subdistributorItems;
private Context mContext;
private ArrayList<SubdistributorItem> arraylist;

public SubdistributorRecyclerAdapter(Context context, List<SubdistributorItem> subdistributorItems) {
    this.subdistributorItems = subdistributorItems;
    this.mContext = context;
    this.arraylist = new ArrayList<SubdistributorItem>();
    this.arraylist.addAll(subdistributorItems);
}

@Override
public SubdistributorListRowHolder onCreateViewHolder(ViewGroup viewGroup,  int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.subdistributor_list_item, null);
    SubdistributorListRowHolder mh = new SubdistributorListRowHolder(v);

    layout_subdistributor.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View v) {
      Intent i = new Intent(mContext, SubdistributorDetail.class);
                    Log.e("Tag shop ", "ShopKeeper Detail called");
                    i.putExtra("subDistributorStatus", txt_RechargeSubdistributor.getText().toString());
                    i.putExtra("subDistributorId", txt_subDistributorId.getText().toString());
                    mContext.startActivity(i);

        }
    });
    return mh;
}
@Override
public void onBindViewHolder(SubdistributorListRowHolder subDistributorListRowHolder, int i) {

    SubdistributorItem subdistributorItem = subdistributorItems.get(i);
    Log.e("Tag ", "adapter "+ subdistributorItem.getSubDistributorName());

}
@Override
public int getItemCount() {
    return (null != subdistributorItems ? subdistributorItems.size() : 0);
}
}

So can any one please help me to update a single row in a list without calling the API again.

Binil Surendran
  • 2,524
  • 6
  • 35
  • 58

5 Answers5

4

Try using this method, to update a single row.

notifyItemChanged(int position)
vishnu narayanan
  • 3,813
  • 2
  • 24
  • 28
  • am not getting the position that is my problem @vishnu narayanan – Binil Surendran Feb 22 '16 at 08:34
  • in onCreateViewHolder() method i have a click listener there, can i get the positon that which row that i clicked @vishnu narayanan – Binil Surendran Feb 22 '16 at 08:41
  • Recycler view unlike listview does not have a onItemClicked method. There are many ways to do a custom implementation. Try this, http://stackoverflow.com/questions/24885223/why-doesnt-recyclerview-have-onitemclicklistener-and-how-recyclerview-is-dif – vishnu narayanan Feb 22 '16 at 09:03
1

Change this

public static class SubdistributorListRowHolder extends RecyclerView.ViewHolder {

    private TextView textView_alphabet;
    private TextView textView_name;
    private TextView textView_tag;
    private ImageView imageViewUserImage;
    private ImageView imageViewMoreButton;
    private LinearLayout linearLayoutMainContent;

    public ViewHolder(View itemLayoutView) {
        super(itemLayoutView);
        textView_alphabet = (TextView) itemLayoutView.findViewById(R.id.textView_alphabet);
        textView_name = (TextView) itemLayoutView.findViewById(R.id.textView_name);
        textView_tag = (TextView) itemLayoutView.findViewById(R.id.textView_tag);
        imageViewUserImage = (ImageView) itemLayoutView.findViewById(R.id.imageViewUserImage);
        linearLayoutMainContent = (LinearLayout) itemLayoutView.findViewById(R.id.linearLayoutMainContent);
    }
}

 @Override
 public void onBindViewHolder(SubdistributorListRowHolder subDistributorListRowHolder, int i) {

 SubdistributorItem subdistributorItem = subdistributorItems.get(i);
 Log.e("Tag ", "adapter "+ subdistributorItem.getSubDistributorName());
 subDistributorListRowHolder.itemLayoutView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.e("Tag ", "Position "+ i);
        }
    });
 }

Over here itemLayoutView is the mail layout which is clickable Modify the SubdistributorListRowHolder like this put all your layout component and find view by id.

A J
  • 4,542
  • 5
  • 50
  • 80
1

Update item in your list subdistributorItems. Then call adapter.notifyItemChanged(int position). You can get a position in ClickListener using

layout_subdistributor.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(final View v) {
        if (mh.getAdapterPosition() != RecyclerView.NO_POSITION) {
          int position = mh.getAdapterPosition();
          // edit your object by calling subdistributorItems.get(position)
        }
    }
});
andrei_zaitcev
  • 1,358
  • 16
  • 23
0
getViewForPosition(int position) 

Obtain a view initialized for the given position. This method should be used by RecyclerView.LayoutManager implementations to obtain views to represent data from an RecyclerView.Adapter.

The Recycler may reuse a scrap or detached view from a shared pool if one is available for the correct view type. If the adapter has not indicated that the data at the given position has changed, the Recycler will attempt to hand back a scrap view that was previously initialized for that data without rebinding. description here

Mostafa Anter
  • 3,445
  • 24
  • 26
-1

Its simple. Make the changes in the ArrayList that is bonded with the recycler view and call notifyDataSetChanged() on the Recycler View.