繁体   English   中英

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

[英]Android button click next display of listview of data in JSON

嗨,您可以通过单击下一步按钮帮助我如何在json中显示接下来的10个数据。 我有50个数据,我想先显示10个。然后,当我单击下一个按钮时,11-20将显示在列表视图中。 我会在下面张贴我的代码,但我不知道该怎么做。 另外,当我单击上一个按钮时,它将返回到上一个列表视图,即1-10。 谢谢!

    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();
    }

定义一个名为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);
}

好的,有几种方法可以实现您想要的目标。 我将向您解释我将如何做:

首先,在doctorData数组列表中 ,您需要显示所有项目(50个项目)。

创建一个partialDoctorData数组列表 ,并仅将其从doctorData的前10个项中分配出来,好吗? 并将此新数组列表添加到SimpleAdaper。

因此,您需要代替代码:

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

因此,当用户单击下一步按钮时,您可以清除partialDoctorData的内容,从原始doctorData arrayList的11-20个项目中添加,然后直接调用

 myadapter.notifyDataSetChanged();

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

尝试;)

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

暂无
暂无

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

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