我正在使用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));
我可以证实,不同的数据被存储。更改第一个项目是数组更改所有项目。
感谢您的帮助。
你在哪里初始化了**数据**? – KishuDroid
这是一个类变量'私人列表 data = new ArrayList <>();' –
mgrantnz
在你的方法中声明 – KishuDroid