所有膨胀的 TableRow 都具有相同的 TextView 值?

问题描述 投票:0回答:1

在运行时,用户填充 SQLite 数据库。

当用户在其 onCreate 方法中打开某个活动时,我会填充 TableLayout。 我使用 While 循环来执行此操作。我在 TableLayout 中填充的所有行在 TextView 中都具有相同的数据。我是否必须添加到所有 TableRows 和 TextViwes 不同的 id ?

我认为如果我在创建视图时添加“false”应该可以解决我的问题,但事实并非如此。

View tr = inflater.inflate(R.layout.table_row_chronology, null,false);

我认为问题出在我的代码中。如果不是,我将不得不更好地了解当用户将数据添加到数据库时会发生什么。

这是我用于填充 TableLayout 的代码:

LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TableLayout tableChronology = (TableLayout) findViewById(R.id.tableLayout1);

    // --------------------------------------------------------------------------------
    MyDatabase myDB = new MyDatabase(this);
    try {
        myDB.createDataBase();
    } catch (IOException e) {
        e.printStackTrace();
    }
    ArrayList<HashMap<String, String>> historyList = myDB.getHistory();
    Iterator<HashMap<String, String>> iterator = historyList.iterator();
    HashMap<String,String> oneRowMap;
    // --------------------------------------------------------------------------------


// Add and inflate table rows while there is data
while(iterator.hasNext()){
    oneRowMap = (HashMap<String,String>) iterator.next();

        View tr = inflater.inflate(R.layout.table_row_chronology, null,false);

        tableChronology = (TableLayout) findViewById(R.id.mainTable);

        String productName = oneRowMap.get("ProductName");
        String calories = oneRowMap.get("Calories");
        String protein = oneRowMap.get("Protein");
        String carbohydrates = oneRowMap.get("Carbohydrates");
        String fats = oneRowMap.get("Fats");
        String grams = oneRowMap.get("Grams");
        //date = oneRowMap.get("Date");

        TextView tv_productName = (TextView) tr.findViewById(R.id.textView_name_show);
        TextView tv_calories = (TextView) tr.findViewById(R.id.textView_kcal_show);
        TextView tv_protein = (TextView) tr.findViewById(R.id.textView_protein_show);
        TextView tv_carbohydrates = (TextView) tr.findViewById(R.id.textView_carbohydrates_show);
        TextView tv_fats = (TextView) tr.findViewById(R.id.textView_fats_show);
        TextView tv_grams = (TextView) tr.findViewById(R.id.textView_grams_show);

        tv_productName.setText(productName);
        tv_calories.setText(calories);
        tv_protein.setText(protein);
        tv_carbohydrates.setText(carbohydrates);
        tv_fats.setText(fats);
        tv_grams.setText(grams);

        tableChronology.addView(tr);

        Toast.makeText(Today.this, "Беше добавен продукт", Toast.LENGTH_SHORT).show();

        myDB.close();

    }
android while-loop layout-inflater
1个回答
0
投票

回答我自己的问题:看来我上面的代码没问题,工作正常。 问题在于我的方法检索了充满

ArrayList
s:
HashMap

 ArrayList<HashMap<String, String>> historyList = myDB.getHistory();

该方法的主要代码如下:

getHistory():

    do {
        historyMap = new HashMap<String, String>();
                
        historyMap.put("NumberId", myCursor.getString(0));
        historyMap.put("ProductName", myCursor.getString(1));
        historyMap.put("Calories", myCursor.getString(2));
        historyMap.put("Protein", myCursor.getString(3));
        historyMap.put("Carbohydrates", myCursor.getString(4));
        historyMap.put("Fats", myCursor.getString(5));
        historyMap.put("Grams", myCursor.getString(6));
        // historyMap.put("Date", myCursor.getString(7));

        historyArray.add(historyMap);

    } while (myCursor.moveToNext());

看来我已将

HashMap
的创建放在循环之外,并且将相同的对象传递给
.add()
方法。

因此,我一遍又一遍地用同一个对象填充

ArrayList

historyMap = new HashMap<String, String>();
© www.soinside.com 2019 - 2024. All rights reserved.