Skip to content

Unconditional layout inflation from view adapter warning

I am using GridView in my alert dialogue with below codes.its giving me error called in title. My codes and more required information is like below.

  private void showGotoPageDialog() {

        final Dialog mDialog = new Dialog(getActivity());
        mDialog.setContentView(R.layout.grid_dialogue);
        mDialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        GridView mGridView = (GridView) mDialog.findViewById(R.id.grid_dialog);
        ArrayList<String> tmp = new ArrayList<>(mPageOptions.length);
        for(int i = 0; i < mPageOptions.length; i++)
        {
            tmp.add(mPageOptions[i].split(" ")[1]);
        }
        CustomAdapter adapter = new CustomAdapter(getActivity(), tmp, mPageIndx);
        //ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, tmp);
        mGridView.setAdapter(adapter);
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int item, long l) {
                mDialog.dismiss();
                mPageIndx = item + 1;
                updateQuotesListServer();
                updatePageInfo();
            }
        });
        mDialog.show();
        TextView dismiss = (TextView) mDialog.findViewById(R.id.dialog_dismiss);
        dismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mDialog.dismiss();
            }
        });
    }

and Adapter like this

 private class CustomAdapter extends BaseAdapter
    {
        private ArrayList<String> list;
        private LayoutInflater inflater;
        private int currentPage;

        CustomAdapter(Activity activity, ArrayList<String> list, int currentPage)
        {
            this.list = new ArrayList<>();
            this.list.addAll(list);
            inflater = (LayoutInflater) activity.getSystemService(AppCompatActivity.LAYOUT_INFLATER_SERVICE);
            this.currentPage = currentPage;
        }

        public void setCurrentPage(int currentPage)
        {
            this.currentPage = currentPage;
        }

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

        @Override
        public Object getItem(int i) {
            return null;
        }

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

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = View.inflate(getActivity(),R.layout.dialogue_item, null);
            TextView txt = (TextView) view.findViewById(R.id.textview_dialogue);
            txt.setText(list.get(i));
            if(i == currentPage-1)
            {
                txt.setTextColor(Color.BLACK);
            }

            return view;
        }
    }

its giving me warning on this line

view = View.inflate(getActivity(),R.layout.dialogue_item, null);

warning is like below

Unconditional layout inflation from view adapter: Should use View Holder pattern (use recycled view passed into this method as the second parameter) for smoother scrolling.

My code is working fine as expected and I am not facing any issue. I want know that What changes require for fix this and what is benefit of it ? Let me know if someone can help me for come out from this and solve my doubt.

Thanks

Answer

View adapter is built so that instead of creating a new view for each item, views that scroll off screen can be reused (they are passed in via the 2nd argument of getView). You are being warned that you should try to reuse these views instead of always inflating new ones. Change the line to

if (view == null) view = View.inflate(getActivity(),R.layout.dialogue_item, null);