3

I have a custom horizontal grid implementation using

Android-DraggableGridViewPager

My flow is as follows

1.I set an initial adapter with an empty list.

2.I make a network call and update the same list and call notifyDataSetChanged() on that adapter

Fragment onCreateView code:

    public class DashboardFragment extends Fragment {
    private DashboardAppsFragmentBinding dashboardFragBindedContainerObj;
    private List<App> appsList;
    private DashboardAppGridAdapter adapterObj;    

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        dashboardFragBindedContainerObj = DataBindingUtil.inflate(
                inflater, R.layout.dashboard_apps_fragment, container, false);
        View view = dashboardFragBindedContainerObj.getRoot();

        appsList = new ArrayList<>();
        adapterObj = new DashboardAppGridAdapter(getActivity(), 0, appsList);

        dashboardFragBindedContainerObj.appGridview.setColCount(3);
        dashboardFragBindedContainerObj.appGridview.setRowCount(3);
        dashboardFragBindedContainerObj.appGridview.setAdapter(adapterObj);

        return view;
    }

AdapterCode:

public class DashboardAppGridAdapter extends ArrayAdapter<App> {
    private Context ctx;
    private List<App> appList;

    public DashboardAppGridAdapter(Context ctx, int resource, List<App> appList) {
        super(ctx, 0, appList);
        this.ctx = ctx;
        this.appList = appList;
    }


    @NonNull
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        AppListingRowItemBinding appListingBindedContainerObj;
        appListingBindedContainerObj =
                DataBindingUtil.inflate(
                        LayoutInflater.from(getContext()),
                        R.layout.app_listing_row_item, parent, false);
        convertView = appListingBindedContainerObj.getRoot();

        if (appList != null && appList.size() > 0) {
            appListingBindedContainerObj.lblAppTitle.setText(getItem(position).getDisplayName());
            appListingBindedContainerObj.lblAppDesc.setText(getItem(position).getDisplayDescription());
        }

        return convertView;
    }

    /**
     * Method to update adapter with new data
     *
     * @param appList
     */
    public void refresh(List<App> appList) {
       this.appList.clear();
       this.appList.addAll(appList);       
       this.notifyDataSetChanged();
    }

}

In my fragment when i get a network response, i call the refresh method in my adapter like so:

public void setupAppsGrid(List<App> appsToBeDisplayed) {
            if (appsToBeDisplayed != null && appsToBeDisplayed.size() > 0) {  
                appsList.clear();           
                appsList.addAll(appsToBeDisplayed);
                adapterObj.refresh(appsList);
                dashboardFragBindedContainerObj.executePendingBindings();   
                }        
            }

When i debug i see the response coming in, the list containing the values but the view doesn't update.

If i reset the adapter, my grid is populated.

i.e

 dashboardFragBindedContainerObj.appGridview.setAdapter(new DashboardAppGridAdapter(getActivity(), 0, appsToBeDisplayed));

every time, i have to update.

usr30911
  • 2,731
  • 7
  • 26
  • 56
  • You don't need to implement a custom `refresh` method - there are public methods : `public void clear()` & `public void addAll(@NonNull Collection extends T> collection)` in the `ArrayAdapter` class (along with other convenience methods) - this automatically does everything you need. – Mark Feb 22 '18 at 23:48
  • 1
    Its a duplication, if you notice my method in the fragment setupAppsGrid() im doing a clear and addAll, and then again inside my adapter im doing the same, i know its redundant – usr30911 Feb 22 '18 at 23:51
  • `i know its redundant` - So what I just said then? – Mark Feb 22 '18 at 23:53
  • 1
    your saying me using custom refresh is the cause for my grid not updating? – usr30911 Feb 22 '18 at 23:54
  • 1
    No, that would be an answer ... I have commented that you can make your code better by using the convenience methods provided by the class -`ArrayAdapter` - you have subclassed. Benefits include thread safety. – Mark Feb 22 '18 at 23:56
  • ok, noted, thankyou! – usr30911 Feb 22 '18 at 23:57
  • Where are you setting your adapter ? eg mDraggableGridViewPager.setAdapter(mAdapter); – nimi0112 Feb 23 '18 at 00:07
  • @nimi0112 - its done here - `dashboardFragBindedContainerObj.appGridview.setAdapter(adapterObj);` - OP is using databinding – Mark Feb 23 '18 at 00:16

0 Answers0