mgrantnz mgrantnz - 7 months ago 23
Java Question

Populating an Android ListView with data from an ArrayList is setting each item to be the same as the first

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.

Answer

Your are not initialize myData in external while loop. So initialize your myData in external while loop.

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