简体   繁体   中英

Refresh listview after SQLite update - Requery with spinner

I have a custom listview that displays my content extracted from a SQLite database. I also have the ability to edit and update a single value of any given row. I have successfully implemented the update.

However, upon updating the row, the listview does NOT show the change. Should i change activity and go back the change has been implemented. From my understanding I need to requery the cursor to sync the data change.

My problem is that my listview contents can be changed dependent on the value of spinner (All, Catergory1, Catatergory2, etc...) therefore dependent on the spinner value, i would require a different cursor requery to be required followed by notifyDataSetChanged() on the adapter.

Eg

Assuming I have a database of foods:

I can edit and update a row value when all rows are displayed (NO category selected). But also I can edit and update a row value when category selected is fruit.


Therefore dependent on spinner category value I would have to run a particular cursor requery.

I have attempted to call my original class the runs through a series of 'if statements' to determine spinner value then select the correct cursor to be run however this requires the method to be static which isn't allowed when returning the spinner value.

Please advise.

I have a code that may help you, I used this to refresh a listview of cities depending on the state selected on a previous spinner

final stateAdapter stateadapter = new stateAdapter(this, StateList);
//customized adapter of state, it´s just an integer and a String

    StateSpinner = (Spinner) findViewById(R.id.spinner1);
    StateSpinner.setAdapter(stateadapter);

    StateSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
            State state = (State) StateSpinner.getSelectedItem();
                            //Custom State Class, you may use your custom food class
            ArrayList<City> CitiesList = new ArrayList<City>();

            try {
                            //your new SQLite query to populate the CitiesList

                }
            } catch (Exception ex) {

            }

            cityadapter = new cityAdapter(MyActivity.this, CitiesList);
                            //customized adapter for cities
            CitySpinner = (Spinner) findViewById(R.id.spinner2);
            CitySpinner.setAdapter(cityadapter);

        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            // your code here
        }
    });

我找到了重新查询的替代方法,并在使用给定值更改数据库后手动简单地更新了数组/适配器,然后运行notifyDataSetChanged()命令刷新列表视图。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM