从内部Android存储中读取JSON数据并填充ListView

时间:2016-04-21 03:12:01

标签: java android json listview

我有一堆json文件保存到android应用程序内部存储中,如下所示:

            JSONObject jsonToSave = createJSONObject();

            filenames = getFilenames(filepath);

            int filenameNumber = filenames.size() + 1;

            ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
            File directory = contextWrapper.getDir(filepath, Context.MODE_PRIVATE);

            filename = "newAssessment(" + filenameNumber + ").json";
            internalFile = new File(directory , filename);

            try {
                FileOutputStream fos = new FileOutputStream(internalFile);
                fos.write(jsonToSave.toString().getBytes());

            }
            catch (IOException e){
                e.printStackTrace();
            }

对于上下文,这里是getFilenames和createJSONObject方法:

private List<String> getFilenames(String path) {

    File files = new File(path);

    FileFilter filter = new FileFilter() {

        private final List<String> exts = Arrays.asList("json");

        @Override
        public boolean accept(File pathname) {
            String ext;
            String path = pathname.getPath();
            ext = path.substring(path.lastIndexOf(".") + 1);
            return exts.contains(ext);
        }
    };

    final File [] filesFound = files.listFiles(filter);
    List<String> list = new ArrayList<String>();
    if (filesFound != null && filesFound.length > 0){
        for (File file : filesFound){
            list.add(file.getName());
        }
    }
    return list;
}


private JSONObject createJSONObject(){
    String orchard = orchardList.getSelectedItem().toString();
    String description = ETAssessmentDescription.getText().toString();
    String type = typeList.getSelectedItem().toString();
    String assessorStaff = ETAssessorStaff.getText().toString();
    String date = ETAssessmentDate.getText().toString();
    String additionalAssessorStaff = ETAdditionalAssessorStaff.getText().toString();
    String sampleSize = ETSampleSize.getText().toString();
    String lineSize = ETLineSize.getText().toString();

    boolean fruitCollected;

    JSONObject jsonObj = new JSONObject();

    if(CBFruitCollected.isChecked()){
        fruitCollected = true;
    }else{
        fruitCollected = false;
    }

    try{
        jsonObj.put("orchard", orchard);
        jsonObj.put("description", description);
        jsonObj.put("type", type);
        jsonObj.put("assessorStaff", assessorStaff);
        jsonObj.put("assessmentDate", date);
        jsonObj.put("additionalAssessorStaff", additionalAssessorStaff);
        jsonObj.put("sampleSize", sampleSize);
        jsonObj.put("lineSize", lineSize);
        jsonObj.put("fruitCollected", fruitCollected);
    }
    catch(JSONException ex){
        ex.printStackTrace();
    }

    return jsonObj;
}

我要做的是使用每个文件中的几个字段填充ListView。这是我到目前为止,使用相同的getFilenames方法:

filenames = getFilenames(filepath);

    ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
    File directory = contextWrapper.getDir(filepath, Context.MODE_PRIVATE);



    for (String filename : filenames) {

        internalFile = new File(directory, filename);
        try {
            FileInputStream fis = new FileInputStream(internalFile);
            DataInputStream in = new DataInputStream(fis);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));

            String assessmentDate = "";
            String orchard = "";
            String strLine;
            int dataCounter = 0;

            while ((strLine = br.readLine()) != null) {
                myData = myData + strLine;
                try {
                    JSONObject object = new JSONObject(myData);
                    assessmentDate = object.getString("assessmentDate");
                    orchard = object.getString("orchard");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                data.add(dataCounter, assessmentDate + " : " + orchard);

                dataCounter++;
            }
            in.close();
        }
        catch(IOException e){
            e.printStackTrace();
        }



    }
    LVAssessments.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, data));

我确信有一些我很想念的简单。提前感谢您的帮助

0 个答案:

没有答案