代码之家  ›  专栏  ›  技术社区  ›  Ghostx0

Android Json-列表视图的数组适配器-多个字符串、多个文本字段

  •  0
  • Ghostx0  · 技术社区  · 6 年前

    我想我做错了,只需要朝着正确的方向努力。我解析了我的json信息,并将它们设置为set字段,以便可以调用和显示每个字段。现在一次只对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 回复  |  直到 6 年前
        1
  •  2
  •   Asutosh Panda    6 年前

    如果要像这样传递多个字段,则需要创建对象类型的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());
    

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