【发布时间】:2018-04-02 01:06:51
【问题描述】:
我认为这一切都错了,只需要朝着正确的方向前进。我解析了我的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 条信息。读取单个字段中的数据然后输出非常棒。我只想将文本添加到所有字段。
【问题讨论】:
-
你明白我在回答中的解释了吗?
-
是的,我确实感谢您的建议。我将制作一个自定义视图适配器来传递数组。手指交叉生病让它工作;)谢谢。
-
酷。编码愉快!
标签: android arrays json listview