Я пытаюсь создать ExpandableListView внутри своей деятельности, которая показывает виды вин на верхнем уровне и отдельные бутылки вина на втором уровне. Я читаю все свои данные из CSV файла, который я создал и заполнял 8 отдельными бутылками вина, которые сейчас находятся под одной категорией. У меня проблема, но я читаю свои данные из файла csv в массив, и я могу сообщить об этом журналу, когда я его прочитал, и он показывает правильно. Но как только я попытаюсь поместить его в свой адаптер, а затем в список, массив заполняется 8 идентичными объектами Wine, которые являются тем, что было последним в моем файле.
Вот код, который я использую, чтобы прочитать файл и создать объект Array of 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 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 [] и с той же проблемой. Я в тупике, я никогда не видел, чтобы содержимое массива просто менялось без видимых причин, подобных этому. Возможно, я пропустил что-то относительно простое, может ли кто-нибудь понять, что здесь может произойти?
Вы назначаете один и тот же объект (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.
move "current = new Chardonnay();" to while loop
в вашем коде каждый элемент массива указывает на тот же экземпляр Chardonnay
По моему опыту, именно так мне пришлось форматировать данные для групп 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());
}