【问题标题】:ListView does not update in activity onCreate method or buttom onClickListener methodListView 在活动 onCreate 方法或按钮 onClickListener 方法中不更新
【发布时间】:2016-01-27 06:52:56
【问题描述】:

我有一个在 onCreate 方法中创建的 listView。如下所示,并从存储过程中读取数据。 我使用了 CallabeStatement。

protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.type_of_dairy);

    try
    {

        CallableStatement catCallable;
        ConnectionHelper connectionHelper = new ConnectionHelper();
        catCallable = connectionHelper.getConnection().prepareCall("{ CALL SpGetDairyCategory}");
        catCallable.execute();

        resultSet = catCallable.getResultSet();

        setResultSet(resultSet);

        if(getResultSet().next()) {
            do {

                String typeString = resultSet.getString("CategoryName");
                int typeId = resultSet.getInt("CategoryId");
                integerArraList.add(typeId);
                typeArray.add(typeString);
                typeAdapter.getItemViewType(R.id.listView); // adapter for first ListView
                dairyType.setAdapter(typeAdapter);//dairyType is my first List View

            }while(resultSet.next());

        }
    }
    catch (SQLException e)
    {
        Toast.makeText(getApplicationContext(), "Something is  wrong", Toast.LENGTH_LONG).show();

    }

在上面的代码中,我得到了Category Id和name的Id,并将它们传递到两个ArrayList中,分别是integerArrayList和typeArray。

我使用从上述存储过程中获得的类别 ID 生成第二个 ListView。 我在dairyType(第一个ListView)onClickListener 中执行此操作。 这是代码

dairyType.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                try {
                    flavorAapter.clear();
                    CallableStatement proCallable;
                    ConnectionHelper connectionHelper = new ConnectionHelper();
                    proCallable = connectionHelper.getConnection().prepareCall("{call SpGetCategoryProducts(?)}");
                    proCallable.setInt(1, integerArraList.get(dairyType.getCheckedItemPosition())); //integerArrayList contains the categoryId from the first Store Procedure
                    resultSet2 = proCallable.executeQuery();
//The second Stored Procedure get the Id and return some value as you see
                    if (resultSet2.next()) {
                        do {
                            int flavourId;
                            int weitgh;
                            int price;
                            weitgh = resultSet2.getInt("Weight");
                            price = resultSet2.getInt("Price");
                            String flavourString = resultSet2.getString("Name");
                            flavourId = resultSet2.getInt("Id");


                            dairyArray.add(flavourString);

                            idOfCategories.add(flavourId);
                            weightArray.add(weitgh);
                            priceArray.add(price);

                            flavorAapter.getItemViewType(R.id.listView2);
                            dairyFlavour.destroyDrawingCache();
                            dairyFlavour.setAdapter(flavorAapter);

                        } while (resultSet2.next());
                        connectionHelper.closeConnection();
                    } else {
                        flavorAapter.clear();
                        Toast.makeText(getApplicationContext(), "Nothing to show", Toast.LENGTH_LONG).show();

                    }
                } catch (SQLException e) {
                    Toast.makeText(getApplicationContext(), "Something wrong", Toast.LENGTH_LONG).show();
                    dairyArray.clear();
                }
            }
        });

好的,所以我根据需要成功生成了第二个 Listview, 问题出在插入按钮中, 现在我有名称、重量、价格和 Id 的 ArrayList。(我使用 Id 作为传递给最后一个存储过程的参数。 所以我必须调用另一个存储过程将所有这些数据插入 DB(Name,Weight,Price) 但它不会插入更改的项目值。 这意味着,我从第一个 ListView 中选择一个项目,第二个 Listview 生成并选择其中一个,但是当我在第一个 ListView 中更改我的选择时,它仍然插入第一个 ListView 的 id。 这是我通过名为 apply 的按钮插入的代码。

    apply.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            try {
                    dairyFlavour.invalidateViews();
                    dairyType.invalidateViews();
                    typeAdapter.notifyDataSetInvalidated();
                    flavorAapter.notifyDataSetChanged();
                    int weight = weightArray.get(dairyFlavour.getCheckedItemPosition()); //weitgh from previous SP
                    int price = priceArray.get(dairyFlavour.getCheckedItemPosition()); //price from previous SP
                    String count = "";
                    int c;
                    count = countOfProduct.getText().toString(); // insert the count of product via editText
                    c = Integer.parseInt(count); // cast the cound to Integer
                    we = idOfCategories.get(dairyFlavour.getCheckedItemPosition());//the id i got in previous SP

                    CallableStatement callableStatement = null;
                    ConnectionHelper connectionHelper = new ConnectionHelper();
                    callableStatement = connectionHelper.getConnection().prepareCall("{call SpInsertLoadCityProducts(?, ?, ? ,?)}");

                    callableStatement.setInt(1, we);
                    callableStatement.setInt(2, weight);
                    callableStatement.setInt(3, price);
                    callableStatement.setInt(4, c);
                    callableStatement.executeQuery();

                }catch(SQLException e){

               Toast.makeText(getApplicationContext(), "Something is wrong", Toast.LENGTH_LONG).show()


            }
            Intent i = new Intent(getApplicationContext(), FinishLoading.class);
            startActivity(i);
        }

    });

在视图中没关系,当我更改第一个 ListView 时,第二个 ListView 会根据需要生成。 但是在插入中,我只将第一个选择的值插入到数据库中,当我在第一个 ListView 中更改选择并在第二个 ListView 中选择另一个产品并单击应用按钮时,第一个数据被插入。

有什么建议吗? 如果您感到困惑,请提出问题以使自己更清楚!

【问题讨论】:

    标签: android arraylist android-listview android-arrayadapter android-adapter


    【解决方案1】:

    我找到了解决方案。 我应该在存储数据之前清除每个 ArrayList。 当第一次获取索引和数据时,我应该清除 ArrayList 并再次填写如下。

            dairyTypes.clear();
            integerArraList.clear();
            typeArray.clear();
    

    还有

                flavorAapter.clear();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 2021-05-01
      • 1970-01-01
      • 2019-02-13
      相关资源
      最近更新 更多