0

I am trying to implement an ArrayAdapter with the list data contained inside the class itself instead of being passed by the calling activity. The constructor will only include the context, like so:

    public class customadapter extends ArrayAdapter<String> {
        private Context mContext;
        private final String[] itemName = {"a", "b", "c"};
        private final String[] itemQty = {"1", "2", "3"};

        public customadapter(Context c) {
            super(c, R.layout.myview);
            mContext = c;
        }

        @Override
        public int getCount() {
          return itemname.length;
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            View rowView = inflater.inflate(R.layout.myview, parent, false);

            TextView fieldName = (TextView) rowView.findViewById(R.id.fieldName);
            TextView fieldQty = (TextView) rowView.findViewById(R.id.fieldQty);
            fieldName.setText(itemName[position]);
            fieldQty.setText(itemQty[position]);
            return rowView;
        };
    }

But although the view is drawn with the headers, the rows are empty. What am I doing wrong?

EDIT: Amended the code to include the solution. Thank you, @Suraj.

iSofia
  • 1,412
  • 2
  • 19
  • 36

1 Answers1

1

use this

View rowView = inflater.inflate(R.layout.myview, parent, false);

instead of

View rowView = inflater.inflate(R.layout.myview, null, true);
Suraj Vaishnav
  • 7,777
  • 4
  • 43
  • 46
  • complete row not visible or textview' data? – Suraj Vaishnav Jan 06 '18 at 18:54
  • The layout includes two headers lines and column headers as well. They appear correctly, but not a single row is displayed. The list fits in a _wrap_content_ view, and it's not expanded beyond the headers. Even after manually increasing the view height, still no rows are shown. The example I'm following has the string array in the calling activity, which is passed to the adapter via the constructor. That works, but not in this way. – iSofia Jan 06 '18 at 18:59
  • The requirement is for ArrayAdapter, and for the data arrays to be within the class (eg: MyList) and not passed from the calling activity. – iSofia Jan 06 '18 at 19:16
  • Your very first answer solved my problem; to use parent and false in the inflater. The reason it did not work earlier was because I had missed the getCount() method in the class. I'm not using the BaseAdapter answer, so I won't mark it as as such. If you could re-post your original answer separately, I'd be happy to mark that. In any case, thank you very much for your help. – iSofia Jan 06 '18 at 20:12