0

我正在尝试使用包含在类本身中的列表数据来实现 ArrayAdapter,而不是由调用活动传递。构造函数将只包含上下文,如下所示:

    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;
        };
    }

但是虽然视图是用标题绘制的,但行是空的。我究竟做错了什么?

编辑:修改代码以包含解决方案。谢谢你,@Suaj。

4

1 回答 1

1

用这个

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

代替

View rowView = inflater.inflate(R.layout.myview, null, true);
于 2018-01-06T18:44:30.127 回答