Android按钮单击下一步显示JSON格式的数据列表视图
您好,您可以帮助我如何通过单击next按钮以json显示接下来的10个数据吗。我有50个数据,我想显示前10个。然后,当我单击“下一步”按钮时,11-20将显示在listview中。我会把我的代码贴在下面,我不知道怎么做。此外,当我单击“上一步”按钮时,它将返回到上一个listview,即1-10。谢谢Android按钮单击下一步显示JSON格式的数据列表视图,android,json,listview,Android,Json,Listview,您好,您可以帮助我如何通过单击next按钮以json显示接下来的10个数据吗。我有50个数据,我想显示前10个。然后,当我单击“下一步”按钮时,11-20将显示在listview中。我会把我的代码贴在下面,我不知道怎么做。此外,当我单击“上一步”按钮时,它将返回到上一个listview,即1-10。谢谢 doctordata = new ArrayList<Map<String, String>>(); try { jsonObject =
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();
}
doctordata=newarraylist();
试一试{
jsonObject=新的jsonObject(d);
jsonArray=jsonObject.optJSONArray(“医生”);
int-arraylength=jsonArray.length();
对于(int i=0;i
定义一个名为“医生”的类,包含字符串名称和字符串专业字段,并将医生添加到一个列表中,您可以迭代或转换为数组
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);
}
好的,有几种方法可以实现您想要实现的目标。我将向您解释我将如何做: 首先,在doctorDataarraylist中,您拥有需要显示的所有项目(50项) 创建一个partialDoctorDataarraylist,并只分配doctorData中的前10项,好吗?并将这个新的arraylist添加到SimpleDaper 因此,您需要执行以下操作,而不是代码:
final SimpleAdapter myadapter = new SimpleAdapter(MainActivity.this, **partialDoctorData**, R.layout.doctor_list, from, views);
list.setAdapter(myadapter);
因此,当用户单击next按钮时,可以清除partialDoctorData内容,从原始doctorData arrayList中添加11-20项,并直接调用
myadapter.notifyDataSetChanged();
(不必重复创建新SimpleAdapter的步骤,只需更改arraylist的值并调用此方法,列表的内容将使用partialDoctorData的内容进行更新)
试试看;) 试试这个:
当加载10个项目时,您可以使用分页,之后您将调用agin api获取下10个项目请将您期望的json添加到您的问题中您想要从
doctordata
中获取前10个项目吗?或者jsonArray
?@jipr311名称和专业有50个数据,我想显示前10个,然后单击“下一步”按钮,11-20将显示等等JsonArray@Maloubobola抱歉,如果你对我的问题感到困惑,我只是新的,我想试试jsondo你有例子吗?对不起,先生,我不知道如何开始,因为我只搜索基本内容,但你真的不需要一个例子,它非常简单,你需要两个数组列表,一个有50个项目,另一个只有10个。最后一个是在创建时位于适配器内部的,每次您希望更改listview的内容时,在您的情况下,单击一个按钮,更改第二个数组的内容,并且只调用方法myadapter.notifyDataSetChanged()啊,好的,我想我会删除doctorData。谢谢你,顺便说一句