androidlistviewandroid-arrayadaptercustom-arrayadapter

ArrayAdapter with contained data


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.


Solution

  • use this

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

    instead of

    View rowView = inflater.inflate(R.layout.myview, null, true);