简体   繁体   English

Android按钮单击JSON中数据的列表视图的下一个显示

[英]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. 嗨,您可以通过单击下一步按钮帮助我如何在json中显示接下来的10个数据。 i have 50 data and i want to display first 10. Then when I click the next button, 11-20 will display in listview. 我有50个数据,我想先显示10个。然后,当我单击下一个按钮时,11-20将显示在列表视图中。 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. 另外,当我单击上一个按钮时,它将返回到上一个列表视图,即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. 定义一个名为Doctors的类,其字段为String name和String Specialty,然后将Doctors添加到可以迭代或转换为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. 首先,在doctorData数组列表中 ,您需要显示所有项目(50个项目)。

Create a partialDoctorData arraylist and assing to it only the first 10 items from doctorData, ok? 创建一个partialDoctorData数组列表 ,并仅将其从doctorData的前10个项中分配出来,好吗? and add this new arraylist to the SimpleAdaper. 并将此新数组列表添加到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 因此,当用户单击下一步按钮时,您可以清除partialDoctorData的内容,从原始doctorData arrayList的11-20个项目中添加,然后直接调用

 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) (您不必重复创建新的SimpleAdapter的步骤,只需更改arraylist的值并调用此方法,该列表的内容将用partialDoctorData的内容进行更新)

Try ;) 尝试;)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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