0

This code has worked for more than a decade now. Today upgraded the SDK from 28 to 32 and the view does not display when it appears first time(first screen of the app). The items count is coming in as 4 as desired, the control is going into getView method. But it does not display. If I go to another screen and press back button, then the grid view appears. Is there a change in grid view w.r.t SDK 30 or above? Any help is appreciated. Rest of the app is all working fine with existing code. Showing partial code here. On pressing back button, onRestart is called and view displays properly.

public class MainActivity extends BaseActivity implements Constants {
public GridView gridView;
MainActivityAdapter adapter;

private ArrayList<String> topicNames;
private ArrayList<Integer> topicIconIds;
private ArrayList<OnTouchListener> onClickListeners;

@Override
protected void onCreation(Bundle savedInstanceState) {
    setLayout(new MainLayout());
    try {
        configureUI();
    } catch (MyException exception) {
        exception.handleMe(this);
    }
    UIUtils.handleLastCrashIfAny(this);
}


private void configureUI() throws MyException {
    ((MainLayout) getBaseLayout()).configureUI();
    prepareList();
    Log.d("MainActivity", " creating mainlayout with topics=" + topicNames.stream().count());
    adapter = new MainActivityAdapter(this, topicNames, topicIconIds);
    gridView.setAdapter(adapter);
}

class MainActivityAdapter extends BaseAdapter {
    private final ArrayList<String> adapterTopicNames;
    private final ArrayList<Integer> adapterTopicIcondIds;

    public MainActivityAdapter(Activity activity, ArrayList<String> listTopic, ArrayList<Integer> listImage) {
        super();
        this.adapterTopicNames = listTopic;
        this.adapterTopicIcondIds = listImage;
    }

    @Override
    public int getCount() {
        return adapterTopicNames.size();
    }

    @Override
    public String getItem(int position) {
        return adapterTopicNames.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder view = null;
        if (convertView == null) {
            convertView = ((MainLayout) getBaseLayout()).getView();
        }
        view = (ViewHolder) convertView.getTag();

        // set image view parameters
        ((MainLayout) getBaseLayout()).setTopicImageParameters(view);

        view.textView.setText(adapterTopicNames.get(position).replace(" and ", " & "));
        view.imageView.setId(position);

        view.imageView.setImageResource(adapterTopicIcondIds.get(position));
      view.position = position;

        convertView.setId(position);
        view.imageView.setOnTouchListener(onClickListeners.get(position));
        return convertView;
    }
}

public ArrayList<String> getTopicNames() {
    return topicNames;
}

@Override
protected void onRestart() {
    super.onRestart();
    adapter.notifyDataSetChanged();
    getBaseLayout().hideBuyFullVersionButtonIfNeeded();
    Log.d("MainActivity", "onRestart");

}

MainLayout.configureUI method is

  public void configureUI() throws MyException {

    int landscapeRowsPortraitColumns = ApplicationDetails.getLandscapeRowCountPortraitColumnCount();
    int landscapeColumsPortraitRows = ApplicationDetails.getLandscapeColumnCountPortraitRowCount();

    if (activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
        numberOfRows = landscapeColumsPortraitRows;
        numberOfColumns = landscapeRowsPortraitColumns;

    } else if (activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
        numberOfRows = landscapeRowsPortraitColumns;
        numberOfColumns = landscapeColumsPortraitRows;
    }

    gridView = new GridView(activity) {
        @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {
            super.onLayout(changed, l, t, r, b);
            if (!areCellDimensionsCalculated) {
                getCellDimensions();
            }
        }
    };

    gridView.setVerticalSpacing(1);
    gridView.setHorizontalSpacing(1);
    gridView.setOverScrollMode(GridView.OVER_SCROLL_NEVER);
    gridView.setStretchMode(GridView.NO_STRETCH);
    gridView.setSelector(android.R.color.transparent);
    gridView.setBackgroundColor(ApplicationDetails.getMainScreenDividerColor());
    RelativeLayout.LayoutParams gridViewParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);
    gridViewParams.addRule(RelativeLayout.BELOW, navigationBar.getId());
    gridViewParams.addRule(RelativeLayout.ABOVE, footerBar.getId());
    contentView.addView(gridView, gridViewParams);

    ((MainActivity) activity).gridView = gridView;

}
nirav dinmali
  • 197
  • 11

1 Answers1

0

Thanks to https://stackoverflow.com/a/14119464/8094329, found the answer. The problem was GridView's onLayout was getting called AFTER setting the adapter, so I added a listener on onLayout and within that listener set the adapter.

private void configureUI() throws WebrichException {
    ((MainLayout) getBaseLayout()).configureUI();
    prepareList();
    adapter = new MainActivityAdapter(this, topicNames, topicIconIds);

    gridView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            gridView.setAdapter(adapter);
            gridView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
    });
}
nirav dinmali
  • 197
  • 11