Android Arrays内容无缘无故被更改。

| 我试图在我的活动中创建一个ExpandableListView,以在顶层显示葡萄酒的类型,在第二层显示单个的葡萄酒。我正在从创建的CSV文件读取所有数据,并填充了8种特定的葡萄酒,目前这些葡萄酒都归为一类。但是,我遇到了问题,正在将csv文件中的数据读取到Array中,读入数据时可以将其报告到日志中,并且可以正确显示。但是一旦我尝试将其放入适配器中,然后放入列表视图中,则数组中将填充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();
}
这是运行此命令创建的日志输出:
04-13 15:45:09.390: INFO/One2OneWineMenu(6472): 0 Wine [name=Acre, place=Central Coast, description=Seductive apple pie crust and lemon blossom aromas introduce crisp juicy flavors enriched by a creaminess resulting from surlie barrel aging, year=2008, priceBottle=25, priceGlass=7]
04-13 15:45:09.390: INFO/One2OneWineMenu(6472): 1 Wine [name=Silver Palm, place=North Coast, description=Fermented in stainless steel* this wine\'s delicate fruit characteristics were preserved without any overbearing flavors that an oak barrel might impart, year=2009, priceBottle=30, priceGlass=10]
04-13 15:45:09.390: INFO/One2OneWineMenu(6472): 2 Wine [name=Franciscan, place=Napa Valley, description=Ripe* generous aromas of apple* pear* and honey with toasty oak. Lively* rich creamy and supple with notes of vanilla on the finish, year=2009, priceBottle=30, priceGlass=-1]
04-13 15:45:09.390: INFO/One2OneWineMenu(6472): 3 Wine [name=Sonoma Cutrer, place=Russian River, description=The 2nd most popular chardonnay in W&S Restaurant Poll* this wine is beautifully balanced with well integrated oak, year=2008, priceBottle=35, priceGlass=11]
04-13 15:45:09.390: INFO/One2OneWineMenu(6472): 4 Wine [name=Matanzas Creek, place=Sonoma, description=92 pts WE* this wine has a silky texture with flavors of lemon cream* peach and pear which feels elegant and creamy on the palate, year=2007, priceBottle=40, priceGlass=-1]
04-13 15:45:09.390: INFO/One2OneWineMenu(6472): 5 Wine [name=Silver by Mer Soleil, place=Santa Lucia Highlands, description=Combines ripe* intense peach* nectarine and tangerine fruit with touches of floral and spice, year=2007, priceBottle=40, priceGlass=-1]
04-13 15:45:09.390: INFO/One2OneWineMenu(6472): 6 Wine [name=Jordan, place=Russian River, description=Voted Best Chardonnay by respected wine journalists who attended 2010 Critics Challenge, year=2008, priceBottle=50, priceGlass=-1]
04-13 15:45:09.390: INFO/One2OneWineMenu(6472): 7 Wine [name=Ramey, place=Santa Lucia Highlands, description=94 pts RP* intense and vibrant* shows full-bodied citrus* melon* and hazelnut flavors that turn subtle and offer hints of fig/tangerine, year=2007, priceBottle=90, priceGlass=-1]
04-13 15:45:09.405: INFO/One2OneWineMenu(6472): Notify Change
04-13 15:45:09.405: INFO/One2OneWineMenu(6472): 0 Wine [name=Ramey, place=Santa Lucia Highlands, description=94 pts RP* intense and vibrant* shows full-bodied citrus* melon* and hazelnut flavors that turn subtle and offer hints of fig/tangerine, year=2007, priceBottle=90, priceGlass=-1]
04-13 15:45:09.405: INFO/One2OneWineMenu(6472): 1 Wine [name=Ramey, place=Santa Lucia Highlands, description=94 pts RP* intense and vibrant* shows full-bodied citrus* melon* and hazelnut flavors that turn subtle and offer hints of fig/tangerine, year=2007, priceBottle=90, priceGlass=-1]
04-13 15:45:09.405: INFO/One2OneWineMenu(6472): 2 Wine [name=Ramey, place=Santa Lucia Highlands, description=94 pts RP* intense and vibrant* shows full-bodied citrus* melon* and hazelnut flavors that turn subtle and offer hints of fig/tangerine, year=2007, priceBottle=90, priceGlass=-1]
04-13 15:45:09.405: INFO/One2OneWineMenu(6472): 3 Wine [name=Ramey, place=Santa Lucia Highlands, description=94 pts RP* intense and vibrant* shows full-bodied citrus* melon* and hazelnut flavors that turn subtle and offer hints of fig/tangerine, year=2007, priceBottle=90, priceGlass=-1]
04-13 15:45:09.405: INFO/One2OneWineMenu(6472): 4 Wine [name=Ramey, place=Santa Lucia Highlands, description=94 pts RP* intense and vibrant* shows full-bodied citrus* melon* and hazelnut flavors that turn subtle and offer hints of fig/tangerine, year=2007, priceBottle=90, priceGlass=-1]
04-13 15:45:09.405: INFO/One2OneWineMenu(6472): 5 Wine [name=Ramey, place=Santa Lucia Highlands, description=94 pts RP* intense and vibrant* shows full-bodied citrus* melon* and hazelnut flavors that turn subtle and offer hints of fig/tangerine, year=2007, priceBottle=90, priceGlass=-1]
04-13 15:45:09.405: INFO/One2OneWineMenu(6472): 6 Wine [name=Ramey, place=Santa Lucia Highlands, description=94 pts RP* intense and vibrant* shows full-bodied citrus* melon* and hazelnut flavors that turn subtle and offer hints of fig/tangerine, year=2007, priceBottle=90, priceGlass=-1]
04-13 15:45:09.405: INFO/One2OneWineMenu(6472): 7 Wine [name=Ramey, place=Santa Lucia Highlands, description=94 pts RP* intense and vibrant* shows full-bodied citrus* melon* and hazelnut flavors that turn subtle and offer hints of fig/tangerine, year=2007, priceBottle=90, priceGlass=-1]
我已经尝试了相同的逻辑概念,但是使用ArrayList而不是Wine [],并且存在相同的问题。我很困惑,我从未见过Array的内容只是因为没有明显的原因而改变了。也许我忽略了一个相对简单的东西,有人知道这里会发生什么吗?     
已邀请:
您将相同的对象(
current
)分配给
chrd
的所有单元格,这就是为什么最后得到最后一个值的原因。您应该在循环内部初始化“ 2”来解决此问题。
 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());
    }
    

要回复问题请先登录注册