【问题标题】:Android Json - Array Adapter to list view - Multiple strings, multiple text fieldsAndroid Json - 列表视图的数组适配器 - 多个字符串,多个文本字段
【发布时间】: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


【解决方案1】:

如果你想像这样传递多个字段,你需要创建一个对象类型的 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&lt;PersonData&gt;类型作为参数。

然后在您的适配器中使用这些值,如下所示 -

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

不要复制完整的代码,试着了解我是如何做到的,并在你的项目中实现它。您的实现在适配器中可能略有不同,这只是如何执行此类操作的演示。

【讨论】:

  • 感谢您的帮助。我将调整我的代码并尝试让它在考虑到这一点的情况下工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-21
  • 1970-01-01
相关资源
最近更新 更多