-->

Android Gridview is not refreshed while pressing b

2020-07-24 06:13发布

问题:

I have one grid view in one activity.I have set Adapter using array-list. Gridview is displaying the list of the items stored in the database.Let say it is some products. When i click on any product in the grid-view it will navigate me to product detail screen. From product detail screen i can delete product.If i delete product and press back button it will navigate me to product list screen.

Expected: Product list screen will come without deleted item.

Actual: Product list screen is coming with deleted item. When touch this item it will do nothing.

Supporting my question,please see below picture

回答1:

You have get gridview items from db in OnResume method of activity. And when u delete any item and then back press, it will call OnResume method of previous activity where u get new list, and call notifyDataSetChanged() on the grid, in order to recreate the view



回答2:

Try Some like:

if (homeGridViewDatas.size() > 0) {
        homeGridViewAdapter = new HomeGridViewAdapter(activity, context,
                homeGridViewDatas);
        gvHome.setAdapter(homeGridViewAdapter);
        homeGridViewAdapter.notifyDataSetChanged();

    }


回答3:

You should notify the gridadapter which something is changed by callingnotifyDataSetChanged on it. In onResumeshould be ok. Or when you update it too.



回答4:

When i click on any product in the grid-view it will navigate me to product detail screen.

When you go from gridView activity (product list screen) to detail activity(product detail screen.), the detail activity is launched on top of your gridView activity in Stack.

If i delete product and press back button it will navigate me to product list screen.

And when you delete a particular product in detail screen and press back button, the detail activity is removed from the stack and hence the gridview activity which was below detail activity comes to foreground and becomes visible.

Product list screen is coming with deleted item. When touch this item it will do nothing.

However your gridView activity still has the previous data.Hence it displays that old data and nothing happens on touch as actual data is different than the one in adapter.

Solution :

You need to notify the adapter for gridView in that activity, that the data has been changed and the view must be updated accordingly.

As onResume() is called everytime activity comes to foreground and is visible, it is the better place to call notifyDataSetChanged() on your gridadapter.