我认为我认为这一切都是错误的,只需要朝着正确的方向努力。我解析了我的json信息并将它们设置为设置字段,因此可以调用和显示每个字段。现在这只适用于1个字段,我不能使用适配器加载多个字段。我是否需要将所有这些数组编译成一个通过自定义适配器调用的数组?这是我的代码:
public class LocalJsonFileActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
BufferedReader jsonReader = new BufferedReader(new InputStreamReader(this.getResources().openRawResource(R.raw.localjsonfile)));
StringBuilder jsonBuilder = new StringBuilder();
for (String line = null; (line = jsonReader.readLine()) != null;) {
jsonBuilder.append(line).append("\n");
}
JSONTokener tokener = new JSONTokener(jsonBuilder.toString());
JSONArray jsonArray = new JSONArray(tokener);
ArrayList<String> name = new ArrayList<String>();
for (int index = 0; index < jsonArray.length(); index++) {
JSONObject jsonObject = jsonArray.getJSONObject(index);
name.add(jsonObject.getString("name"));
}
ArrayList<String> bloodtype = new ArrayList<String>();
for (int index = 0; index < jsonArray.length(); index++) {
JSONObject jsonObject = jsonArray.getJSONObject(index);
bloodtype.add(jsonObject.getString("bloodtype"));
}
ArrayList<String> type = new ArrayList<String>();
for (int index = 0; index < jsonArray.length(); index++) {
JSONObject jsonObject = jsonArray.getJSONObject(index);
type.add(jsonObject.getString("type"));
}
ArrayList<String> dob = new ArrayList<String>();
for (int index = 0; index < jsonArray.length(); index++) {
JSONObject jsonObject = jsonArray.getJSONObject(index);
String series = jsonObject.getString("dob");
if (series.equals("December")) {
dob.add(jsonObject.getString("dob"));
}
}
setListAdapter(new ArrayAdapter<String>
(this, R.layout.usercard, R.id.txttype, type));
} catch (FileNotFoundException e) {
Log.e("jsonFile", "file not found");
} catch (IOException e) {
Log.e("jsonFile", "ioerror");
} catch (JSONException e) {
Log.e("jsonFile", "error while parsing json");
}
}
}
layout main只是一个空白列表视图。我的布局卡有4个字段1,即图像视图。但我似乎无法显示超过1条信息。在单个字段中读取数据然后输出很好。我只想将文本添加到所有字段。
答案 0 :(得分:2)
如果要传递这样的多个字段,则需要创建对象类型的ArrayList。现在哪个对象?做一个这样的课 -
public class PersonData {
private String name, bloodType, type, dob;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBloodType() {
return bloodType;
}
public void setBloodType(String bloodType) {
this.bloodType = bloodType;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
}
在LocalJsonFileActivity中,解析并存储这样的数据 -
ArrayList<PersonData> data = new ArrayList<PersonData>();
for (int index = 0; index < jsonArray.length(); index++) {
JSONObject jsonObject = jsonArray.getJSONObject(index);
PersonData mPersonData = new PersonData();
mPersonData.setName(jsonObject.getString("name"));
mPersonData.setBloodType(jsonObject.getString("bloodtype"));
mPersonData.setType(jsonObject.getString("type"));
String series = jsonObject.getString("dob");
if (series.equals("December")) {
mPersonData.setDob(jsonObject.getString("dob"));
}
data.add(mPersonData);
}
使用自定义适配器或修改正在使用的适配器的构造函数,以将数据作为参数类型ArrayList<PersonData>
。
然后在适配器中使用这些值 -
holder.TextViewName.setText(data.get(position).getName());
holder.TextViewBloodType.setText(data.get(position).getBloodType());
holder.TextViewType.setText(data.get(position).getType());
holder.TextViewDOB.setText(data.get(position).getDob());
不要复制完整代码,尝试了解我是如何做到的并在项目中实现它。你的实现在适配器中可能会有所不同,这只是一个如何做这些事情的演示。