0

I have a Fragment with a RecyclerView. My fragment calls an AsyncTask, and after the request is completed the task returns to my Fragment which implements a processFinish method. in my processFinish I reference a global RV object and try to updated its' content

@Override
public void processFinish(String output, String id) {
    switch (id){
        case "exchange": {
            Gson gson = new Gson();
            JsonReader reader = new JsonReader(new
                    InputStreamReader( new ByteArrayInputStream(output.getBytes()) ));
            Type type = new TypeToken<ExchangeResponse>(){}.getType();
            ExchangeResponse data = gson.fromJson(reader, type);
            homeRV.refreshDrawableState();
            // This add doesn't work...
            ((DisplayableAdapter)homeRV.getAdapter()).addItem(new ExchangeDisplayable(data));
        }
    }
}

This is my addItem method.

public void addItem(Displayable d){
    this.results.add(d);
    this.notifyItemInserted(results.size() - 1);
    this.notifyDataSetChanged();
}

If I update the adapter from onViewCreated it works well. But not from processFinish. I stopped the debugger right after adding the item, and it seems like the reference is not lost (like others with a similar issue posted). The size of my dataset is changed, but visually nothing happens.

EDIT - I've added my onViewCreated where homeRV is defined, and my onPostExecute.

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    homeRV = getView().findViewById(R.id.rv_home);
    homeRV.setHasFixedSize(true);
    homeRV.addItemDecoration(new DividerItemDecoration(homeRV.getContext(), DividerItemDecoration.VERTICAL));
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this.getContext());
    homeRV.setLayoutManager(layoutManager);
    homeRV.setAdapter(new DisplayableAdapter(new ArrayList<Displayable>()));
    // This add works...
    ((DisplayableAdapter)homeRV.getAdapter()).addItem(new HomeDisplayable("Heads up", "this is where Heads Up info will be stored"));
    getExchangeRates();

}

In AsyncTask:

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);
    response.processFinish(result, identifier);

}
Alex Osheter
  • 589
  • 5
  • 22

1 Answers1

1

Remove homeRV.setHasFixedSize(true); and see if it works. It is not updating width/height of recyclerview because of this line so you can't see new items.

More information on how setHasFixedSize works can be found here.

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
Zohaib Amir
  • 3,482
  • 2
  • 11
  • 29