0

I'm working with the android studio and trying to populate a ListView with data, which is stored in files on the devices internal storage. I am able to create a list with the exact number of item as there are files but they should all be displaying different information. At the moment, they are all displaying that same data as the first item in the ArrayList.

Here is the code:

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

        int counter = 0;

        int fileNameNumber = 1;

        filename = "newAssessment(" + fileNameNumber + ").json";

        internalFile = new File(directory, filename);

        JSONObject jsonObject;

        while (internalFile.exists()) {

            files.add(counter, internalFile);

            try {
                FileInputStream fis = new FileInputStream(internalFile);
                DataInputStream in = new DataInputStream(fis);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));

                String strLine;

                while ((strLine = br.readLine()) != null) {
                    myData = myData + strLine;
                }
            }catch(IOException e){
                e.printStackTrace();
            }

                try {
                    jsonObject = new JSONObject(myData);

                    String assessmentDate = jsonObject.getString("assessmentDate");
                    String orchard = jsonObject.getString("orchard");

                    data.add(counter, assessmentDate + " : " + orchard);
                }catch(JSONException e){
                    e.printStackTrace();
                }

                counter++;
                fileNameNumber++;


            filename = "newAssessment(" + fileNameNumber + ").json";

            internalFile = new File(directory, filename);
        }

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

I can confirm that different data is being stored. Changing the item which comes first is the array changes all the items.

Thanks for the help.

4

3 回答 3

1

问题在这里:

while ((strLine = br.readLine()) != null) {
                myData = myData + strLine;
            }

当 while 循环再次重复时,myData 仍然具有较旧的数据。在上述 while 循环再次重复之前,您需要将 myData 初始化为一个新字符串。

myData = "";
while ((strLine = br.readLine()) != null) {
                myData = myData + strLine;
            }
于 2016-04-26T04:36:11.930 回答
1

您没有在外部 while 循环中初始化 myData 。因此,在外部 while 循环中初始化您的 myData。

while (internalFile.exists()) {

            files.add(counter, internalFile);

            try {
                FileInputStream fis = new FileInputStream(internalFile);
                DataInputStream in = new DataInputStream(fis);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));

                String strLine;

                while ((strLine = br.readLine()) != null) {
                    myData = myData + strLine;
                }
            }catch(IOException e){
                e.printStackTrace();
            }

                try {
                    jsonObject = new JSONObject(myData);

                    String assessmentDate = jsonObject.getString("assessmentDate");
                    String orchard = jsonObject.getString("orchard");

                    data.add(counter, assessmentDate + " : " + orchard);
                }catch(JSONException e){
                    e.printStackTrace();
                }

                counter++;
                fileNameNumber++;
                myData = "";


            filename = "newAssessment(" + fileNameNumber + ").json";

            internalFile = new File(directory, filename);
        }

        LVAssessments.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, data));
于 2016-04-26T04:26:36.097 回答
0

您需要使用 for 循环从 json 对象中获取所有数据并将它们添加到 myData

于 2016-04-26T04:15:41.353 回答