1

I need to add my Items one by one to my RcyclerView for example add first item to recyclerView and show then add another item....

I have a volley that connect to a service then get me some data, in this volley I call a method :

private void makeJsonArryReq(String uri) {
    userPointList = new ArrayList<>();
    orginal = new ArrayList<>();

    final JsonArrayRequest req = new JsonArrayRequest(uri,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {

                    orginal = MarketingPoints_JSONParser.FeedOriginal(response.toString(), mUser_Code, ReportActivity.this);
                    userPointList = MarketingPoints_JSONParser.parseFeed(response.toString(), mUser_Code, ReportActivity.this);
                    FillList();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d("Custom Log Error", "Error: " + error.getMessage());
        }
    });
    AppController.getInstance().addToRequestQueue(req, tag_json_arry);
}

when I call this method FillList(); it fill and show first item on RecyclerView,How can I listen to adapter for finalized and call FillList(); again for next item .

My FillList():

public void FillList() {
        Geocoder geocoder = new Geocoder(activity, Locale.getDefault());
            List<Address> addresses;
            StepModel stp = null;
            List<StepModel> Step = new ArrayList<>();
            if (check < userPointList.size()) {
                try {
                    addresses = geocoder.getFromLocation(userPointList.get(check).getLat(), userPointList.get(check).getLng(), 1);
                    stp = new StepModel();
                    stp.setsCity(addresses.get(0).getAdminArea());
                    stp.setsStreet(addresses.get(0).getThoroughfare());
                    stp.setSlat(userPointList.get(check).getLat());
                    stp.setSlng(userPointList.get(check).getLng());
                    stp.setSdate(userPointList.get(check).getDate());
                    stp.setStime(userPointList.get(check).getTime());
                    stp.setsCounts(userPointList.get(check).getCounts());
                    stp.setSprofilePhoto(userPointList.get(check).getProfilePhoto());
                    stp.setsUserCode(userPointList.get(check).getUserCode());
                    Step.add(stp);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                pointAdapter = new PointsList_Adapter(Step, ReportActivity.this,this, city, street);
                Points_Recycler.setAdapter(pointAdapter);
                pointAdapter.notifyDataSetChanged();
                check++;
            }else {
                return;
            }
    }

Explain :

I connect to a service then I get a json then I convert it and store in a List.In my json I have two specific data (Lat and lng) that I should get name country and name street(For example 100 lat lng) then show on recyclerView but it get long time because I should fill recyclerView on by one.

My adapter :

public class PointsList_Adapter extends RecyclerView.Adapter<PointsList_Adapter.ViewHolder> {
    int count = 0;
    private List<StepModel> mpList;
    public static Activity activity;
    public static boolean flagItem = true;
    public static boolean flagToast = true;

    private mstep Mstep;
    public PointsList_Adapter(List<StepModel> userCodeList, Activity activity) {
        this.mpList = userCodeList;
        this.activity = activity;

    }

    @Override
    public PointsList_Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // create a new view
        View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.listpoints_cardview, null);

        // create ViewHolder
        ViewHolder viewHolder = new ViewHolder(itemLayoutView);
        return viewHolder;
    }
    @Override
    public void onBindViewHolder(PointsList_Adapter.ViewHolder viewHolder, final int position) {

        String dateValue = mpList.get(position).getDate();
        final String countValue = String.valueOf(mpList.get(position).getCounts());
        final Double lat = mpList.get(position).getLat();
        final Double lng = mpList.get(position).getLng();
        final String userCode = mpList.get(position).getUserCode();
        final String time = mpList.get(position).getTime();
        final int counts = mpList.get(position).getCounts();
        final String city = mpList.get(position).getCity();
        final String street = mpList.get(position).getStreet();

        viewHolder.txtDate.setText(dateValue);
        viewHolder.txtPoints.setText(lat + " , " + lng);
        viewHolder.txtTime.setText(time);
        viewHolder.txtAddress.setText(city + " , " + street);
        viewHolder.txtCount.setText(countValue);
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView txtDate, txtPoints, txtTime, txtAddress, txtCount;
        public LinearLayout lLstPoints;

        public ViewHolder(View itemLayoutView) {
            super(itemLayoutView);
            txtDate = (TextView) itemLayoutView.findViewById(R.id.txtDate);
            txtPoints = (TextView) itemLayoutView.findViewById(R.id.txtPoints);
            txtTime = (TextView) itemLayoutView.findViewById(R.id.txtTime);
            txtAddress = (TextView) itemLayoutView.findViewById(R.id.txtAddress);
            txtCount = (TextView) itemLayoutView.findViewById(R.id.txtCount);
            lLstPoints = (LinearLayout) itemLayoutView.findViewById(R.id.lLstPoints);
        }
    }
}

2 Answers2

1

After retrieving all data from server at once, inside Filllist() method you should use for, while or do..while loops to iterate your code.You have used if conditional loop that will run only once.

Else, you can create a method inside adapter to add item to your list and then call notifydatasetchanged() method there.

public void add(Object obj){
     mArraylist.add(obj);
     notifyDataSetChanged();
}
shalini
  • 355
  • 4
  • 17
1

Please add below method in your adapter:

  public reloadList(ArrayList<String> mpList) {
    this.mpList.addAll(mpList);
}

When you want to update list or want to add one more item in your listview, use below code:

 public void FillList() {
        Geocoder geocoder = new Geocoder(activity, Locale.getDefault());
        List<Address> addresses;
        StepModel stp = null;
        List<StepModel> Step = new ArrayList<>();
        if (check < userPointList.size()) {
            try {
                addresses = geocoder.getFromLocation(userPointList.get(check).getLat(), userPointList.get(check).getLng(), 1);
                stp = new StepModel();
                stp.setsCity(addresses.get(0).getAdminArea());
                stp.setsStreet(addresses.get(0).getThoroughfare());
                stp.setSlat(userPointList.get(check).getLat());
                stp.setSlng(userPointList.get(check).getLng());
                stp.setSdate(userPointList.get(check).getDate());
                stp.setStime(userPointList.get(check).getTime());
                stp.setsCounts(userPointList.get(check).getCounts());
                stp.setSprofilePhoto(userPointList.get(check).getProfilePhoto());
                stp.setsUserCode(userPointList.get(check).getUserCode());
                Step.add(stp);
            } catch (IOException e) {
                e.printStackTrace();
            }

            // Just update your list and call notifyDataSetChanged()
            pointAdapter .reloadList(Step);
            pointAdapter .notifyDataSetChanged();
            check++;
        }else {
            return;
        }
    }

Initialize your adapter as below:

ArrayList<StepModel> userCodeList = new ArrayList<StepModel>();
pointAdapter = new PointsList_Adapter(userCodeList , activity) ;

Thank You.

AndiGeeky
  • 11,266
  • 6
  • 50
  • 66
  • My adapter extends RecyclerView.Where,where I add your method. when add your method get me error Ivalid method declaration;return type requierd.and in activity when I add this pointAdapter.reloadList(); say me can not resolve method reloadList(). –  Aug 08 '16 at 05:43
  • @ Jo Jo Roid: Change your `List` to `ArrayList` – AndiGeeky Aug 08 '16 at 05:50
  • I use from your code but crash me. NullPointerException ==> pointAdapter.reloadList(Step); –  Aug 08 '16 at 05:53
  • @Jo Jo Roid: You need to initialize your adapter with blank data before calling `FillList()` method :) – AndiGeeky Aug 08 '16 at 05:55
  • I'm teasing you.I am sorry a lot. But how( You need to initialize....) ;( –  Aug 08 '16 at 05:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/120386/discussion-between-andigeeky-and-jo-jo-roid). – AndiGeeky Aug 08 '16 at 05:59