Android阵列的内容正在被无缘无故地更改
我试图在我的活动中创建一个可扩展的列表视图,在顶层显示葡萄酒的类型,在第二层显示单独的葡萄酒瓶。我正在从我创建的CSV文件中读取我的所有数据,该文件中填充了8瓶特定的葡萄酒,目前它们都属于一个类别。但我遇到了一个问题,我正在将csv文件中的数据读取到一个数组中,我可以在读取数据时将其报告到日志中,并正确显示。但一旦我尝试将它放入适配器,然后放入listview,数组中就会充满8个相同的Wine对象,它们是我文件中最后一个对象 下面是我用来读取文件和创建Wine对象数组的代码 编辑:在while循环完成填充后,我更改了代码以检查数组写入,得到了相同的结果。这是代码的较新版本Android阵列的内容正在被无缘无故地更改,android,arrays,arraylist,expandablelistview,Android,Arrays,Arraylist,Expandablelistview,我试图在我的活动中创建一个可扩展的列表视图,在顶层显示葡萄酒的类型,在第二层显示单独的葡萄酒瓶。我正在从我创建的CSV文件中读取我的所有数据,该文件中填充了8瓶特定的葡萄酒,目前它们都属于一个类别。但我遇到了一个问题,我正在将csv文件中的数据读取到一个数组中,我可以在读取数据时将其报告到日志中,并正确显示。但一旦我尝试将它放入适配器,然后放入listview,数组中就会充满8个相同的Wine对象,它们是我文件中最后一个对象 下面是我用来读取文件和创建Wine对象数组的代码 编辑:在while循
handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
Log.i(myTag, "Notify Change");
//By the time I get to here every object in the array is identical
for(int i = 0; i < chrd.length; i++){
Log.i(myTag,i + " " + chrd[i].toString());
}
super.handleMessage(msg);
}
};
Runnable r = new Runnable(){
public void run()
{
current = new Chardonnay();
//final int ITEMS = 15;
int count = 0;
try {
File myFile = new File ("/sdcard/chardonnay.txt");
fis = new FileInputStream(myFile);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
String line;
while ((line = reader.readLine()) != null) {
String[] RowData = line.split(",");
current.setName(RowData[0]);
current.setPlace(RowData[1]);
current.setDescription(RowData[2]);
current.setYear(Integer.valueOf(RowData[3]));
current.setPriceBottle(Integer.valueOf(RowData[4]));
current.setPriceGlass(Integer.valueOf(RowData[5]));
chrd[count] = current;
Log.i(myTag, count + " " + chrd[count]);
count++;
}
for(int i = 0; i < chrd.length; i++){
Log.i(myTag,i + " " + chrd[i]);
}
}
catch (IOException ex) {
// handle exception
ex.printStackTrace();
}
handler.sendEmptyMessage(1);
try {
fis.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
};
Thread thread = new Thread(r);
thread.start();
}
我尝试过同样的逻辑概念,但使用ArrayList而不是Wine[],它也有同样的问题。我被难住了,我从来没有见过数组的内容会因为这样的原因而改变。也许我忽略了一些相对简单的事情,有人知道这里可能发生了什么吗?您将同一个对象(
current
)分配给chrd
的所有单元格,这就是您最终得到最后一个值的原因。您应该在循环内初始化current
,以解决此问题
while ((line = reader.readLine()) != null) {
current = new Chardonnay();
String[] RowData = line.split(",");
current.setName(RowData[0]);
current.setPlace(RowData[1]);
current.setDescription(RowData[2]);
current.setYear(Integer.valueOf(RowData[3]));
current.setPriceBottle(Integer.valueOf(RowData[4]));
current.setPriceGlass(Integer.valueOf(RowData[5]));
chrd[count] = current;
Log.i(myTag, count + " " + chrd[count]);
count++;
}
这方面的问题是:
current = new Chardonnay();
您只创建了一个对象,while的每个循环都会替换该对象中的属性,从而以最后一个对象结束
将对象的创建移动到while循环中。将“current=new Chardonnay();”移动到while循环中
在您的代码中,数组中的每一项都指向同一个霞多丽实例。根据我的经验,这就是我为ExpandableListAdapter组格式化数据的方式:
ArrayList<HashMap<String, String>> alist = new ArrayList<HashMap<String, String>>();
...
//provided there are entries in the database, iterate through them all. create a hashmap using "company" as the key and
//the company as the item and add this hashmap to the array of maps.
if (cursor.moveToFirst()) {
do {
HashMap<String, String> m = new HashMap<String, String>();
m.put("company", cursor.getString(cursor.getColumnIndex(CompanyAndProductDatabaseAdapter.company_column)));
alist.add(m);
} while (cursor.moveToNext());
}
ArrayList alist=new ArrayList();
...
//如果数据库中有条目,则遍历所有条目。使用“company”作为键和
//将公司作为项目,并将此hashmap添加到映射数组中。
if(cursor.moveToFirst()){
做{
HashMap m=新的HashMap();
m、 put(“company”,cursor.getString(cursor.getColumnIndex(CompanyAndProductDatabaseAdapter.company_column));
3.添加(m);
}while(cursor.moveToNext());
}
chrd定义是必需的。。。包含期末考试、静力学和填充。您如何格式化ExpandableListAdapter的数据。根据我的经验,必须将组格式化为列表,将子组格式化为列表。我在回答中提供了一些示例代码来说明。chrd是这样定义的:Wine[]chrd;chrd=新葡萄酒[8];嗯,似乎其他答案更合适。不管这一次那些为你工作。谢谢!你说得对。如果我那样做的话,它会起作用的。现在我正试着让我的大脑思考为什么它会第一次正确地报告(从循环内部的日志中),但一旦我离开循环,它就会切换。是不是因为我只是给数组中的current分配了一个引用?如果我想要像chrd[count].setName(RowData[0])这样的东西;等如果不对当前对象执行所有这些操作,我认为它也可以避免这个问题。第一次是在循环中,因此它打印对象的当前值,在下一个循环中,值在所有单元格中都已更改,因为它是同一个对象。
ArrayList<HashMap<String, String>> alist = new ArrayList<HashMap<String, String>>();
...
//provided there are entries in the database, iterate through them all. create a hashmap using "company" as the key and
//the company as the item and add this hashmap to the array of maps.
if (cursor.moveToFirst()) {
do {
HashMap<String, String> m = new HashMap<String, String>();
m.put("company", cursor.getString(cursor.getColumnIndex(CompanyAndProductDatabaseAdapter.company_column)));
alist.add(m);
} while (cursor.moveToNext());
}