2016-04-26 44 views
0

我正在使用android studio并尝试用数据填充ListView,该数据存储在设备内部存储的文件中。我可以创建一个列表,其中包含文件的确切数量,但它们应该都显示不同的信息。目前,它们都显示与ArrayList中的第一项相同的数据。使用ArrayList中的数据填充Android ListView将每个项目设置为与第一个项目相同

下面是代码:

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

我可以证实,不同的数据被存储。更改第一个项目是数组更改所有项目。

感谢您的帮助。

+1

你在哪里初始化了**数据**? – KishuDroid

+0

这是一个类变量'私人列表 data = new ArrayList <>();' – mgrantnz

+0

在你的方法中声明 – KishuDroid

回答

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)); 
+0

做了伎俩,谢谢堆。 – mgrantnz

0

您需要使用for循环,从JSON对象获得的所有数据,并将其添加到myData的

+0

你可以给我一个例子?我发现很难想象你的意思,谢谢。 – mgrantnz

1

的问题是在这里:

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

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

myData = ""; 
while ((strLine = br.readLine()) != null) { 
       myData = myData + strLine; 
      } 
相关问题