0

How can i use pagination in list view while parsing data from json ? I used json to pares the data from url but now i want pagination on button click the url page no. change to 2 and the data pares again how can i do that my working code for json is below -

public class FirstFragment extends Fragment {

public AfricaFragment(){}
private static final String TAG = FirstFragment.class.getSimpleName();

// Movies json url
private static final String url = "http://urlling&page=1";
private ProgressDialog pDialog;
private List<Movie> movieList = new ArrayList<Movie>();
private ListView listView;
private CustomListAdapter adapter;

@Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    final View rootView = inflater.inflate(R.layout.activity_main, container, false);

    listView = (ListView) rootView.findViewById(R.id.list);
    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?>  adapterView, View view, int Position,
                long offset)  { 
            // TODO Auto-generated method stub
            Movie item = (Movie) adapter.getItem(Position);             
            Intent intent = new Intent(rootView.getContext(), SingleArticle.class);
            single.title = item.getTitle();
            single.author = item.getAuthor();


            startActivity(intent);


            }        
        });

    //pDialog = new ProgressDialog(this);
    // Showing progress dialog before making http request

    return rootView;
 }

@Override
public void onStart(){
    super.onStart();
    // calling adapter changes here, just
    // to avoid getactivity()null

    //adapter = new CustomListAdapter(this, movieList);
    pDialog = new ProgressDialog(getActivity());
    pDialog.setMessage("Loading...");
    pDialog.show();

    // changing action bar color
    //getActivity().getActionBar().setBackground(
            //new ColorDrawable(Color.parseColor("#1b1b1b")));

    // Creating volley request obj
    JsonArrayRequest movieReq = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());
                    hidePDialog();

                    // Parsing json
                    for (int i = 0; i < response.length(); i++) {
                        try {

                            JSONObject obj = response.getJSONObject(i);

                            Movie movie = new Movie();
                            movie.setTitle(obj.getString("title"));

                            movie.setAuthor(obj.getString("author"));


                            // adding movie to movies array
                            movieList.add(movie);

                            adapter = new CustomListAdapter(getActivity(), movieList);
                            adapter.notifyDataSetChanged();
                            listView.setAdapter(adapter);

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

                        }

                    }

                }
            },new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                    new AlertDialog.Builder(getActivity())
                    .setTitle("No Connectivity ")
                    .setMessage("Please chechk your internet connectivity!")
                    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) { 
                            // continue with delete
                        }
                     })
                    //.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                        //public void onClick(DialogInterface dialog, int which) { 
                            // do nothing
                        //}
                     //})
                    .setIcon(android.R.drawable.ic_dialog_alert)
                     .show();
                    hidePDialog();

                }
            });
    AppController.getInstance().addToRequestQueue(movieReq);

    listView.setAdapter(adapter);

}

private View getActionBar() {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}

private void hidePDialog() {
if (pDialog != null) {
    pDialog.dismiss();
    pDialog = null;
}
}

}
Hitesh Matnani
  • 533
  • 2
  • 8
  • 26
  • 1
    You can call another request on scroll ends of list view. Ref: http://stackoverflow.com/questions/20458475/android-listview-to-load-more-items-when-reached-end – Suneel Prakash Sep 23 '14 at 12:09
  • How can i implement this in a fragment as i am parsing data in a fragment activity ? – Hitesh Matnani Sep 23 '14 at 12:16
  • Create a method parseandSetData() in fragment and call fragmentInstance.parseandSetData(resultList); Append it to exiting list and nottifydatasetchanged for adapter – Suneel Prakash Sep 23 '14 at 12:18

0 Answers0