简体   繁体   中英

Android button click next display of listview of data in JSON

hi can you help me how to display the next 10 data in json by click the next button. i have 50 data and i want to display first 10. Then when I click the next button, 11-20 will display in listview. Ill post my code below and i dont have any idea how to do it. Also when i click previous button it will go back to previous listview which is 1-10. Thanks!

    doctordata = new ArrayList<Map<String, String>>();
    try {
        jsonObject = new JSONObject(d);
        jsonArray = jsonObject.optJSONArray("Doctors");
        int arraylength = jsonArray.length();
        for (int i = 0; i < arraylength; i++) {
            Map<String, String> doctormap = new HashMap<String, String>();
            JSONObject jsonChildNode = jsonArray.getJSONObject(i);
            doctor = jsonChildNode.optString("Name").toString();
            specialty = jsonChildNode.optString("Specialty").toString();
            doctormap.put("name", doctor);
            doctormap.put("specialty", specialty);
            doctordata.add(doctormap);
        }
        String[] from = {"name", "specialty"};
        int[] views = {R.id.doctorlist_name, R.id.doctorlist_specialty,};
        final SimpleAdapter myadapter = new SimpleAdapter(MainActivity.this, doctordata, R.layout.doctor_list, from, views);
        list.setAdapter(myadapter);
    } catch (JSONException e) {
        e.printStackTrace();
    }

Define a class called Doctors, with fields String name and String Specialty, and add the Doctors to a list that you can iterate or convert to Array.

class Doctors {
        private final String specialty;
        private final String name;
        public Doctors (){
            specialty= "Spe1";
            name = "name";
        }
    }

public String convertToJson(){
        Gson gson = new Gson();

        return  gson.toJson(this);
}

Ok, there are several ways to do what do you want to achieve. I will explain you how I would do it:

Firts, in the doctorData arraylist you have all the items (50 items) that you need to show.

Create a partialDoctorData arraylist and assing to it only the first 10 items from doctorData, ok? and add this new arraylist to the SimpleAdaper.

So you will need to do instead of your code:

final SimpleAdapter myadapter = new SimpleAdapter(MainActivity.this, **partialDoctorData**, R.layout.doctor_list, from, views);
list.setAdapter(myadapter);

So when the user click in the next button, you can clean the partialDoctorData content, add from the 11-20 items from the original doctorData arrayList and and and directly call to the

 myadapter.notifyDataSetChanged();

(you don't have to repeat the step to create a new SimpleAdapter, only changing the values of the arraylist and calling to this method, the content of the list is going to be updated with the content of the partialDoctorData)

Try ;)

当将加载10个项目后,您可以使用分页,然后您将调用agin api获取下一个10个项目

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM