I am trying to create an ExpandableListView inside my activity that shows shows types of wines on the top level, and individual bottles of wine on the second level. I am reading all of my data from a CSV file that I have created and filled with 8 specific bottles of wine which are all under one category for now. I am having a problem though, I am reading my data from the csv file into an Array and I can report it out to the log as I read it in and it shows correctly. But once I go to try to put it into my adapter and then into the listview the array is filled with 8 identical Wine objects which are whatever the last one in my file is.
Here is the code I am using to read the file and create an Array of Wine objects.
Edit: I changed my code to check my array write after the while loop finishes filling it and I am getting the same result. This is the newer version of the code.
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();
}
And here is the log output that running this creates:
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]
I have tried the same logical concept but with ArrayList instead of Wine[] and its having the same problem. I am stumped, I have never seen the contents of an Array just change for no apparent reason like this. Perhaps I am overlooking something relatively simple, does anyone have any idea what might be going on here?
You assign the same object (current
) to all cells of chrd
, this is why you end up with the last value. You should initialize current
inside the loop to fix this.
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++;
}
Problem is in this line:
current = new Chardonnay();
You are only creating one object, every loop of the while replaces properties in this object and thus you end with the last one.
Move the creation of the object inside the while loop.
move "current = new Chardonnay();" to while loop
in your code every item in array point to the same instance of Chardonnay
In my experience, this is how I have had to format data for ExpandableListAdapter Groups:
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());
}
精彩评论