包含数据的

时间:2018-01-06 18:29:22

标签: android listview android-arrayadapter custom-arrayadapter

我正在尝试实现一个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;
        };
    }

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

编辑:修改了包含解决方案的代码。谢谢@Suraj。

1 个答案:

答案 0 :(得分:1)

使用此

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

而不是

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