包含数据的 ArrayAdapter
ArrayAdapter with contained data
我正在尝试使用 class 本身包含的列表数据实现 ArrayAdapter,而不是通过调用 activity 传递。构造函数将只包含上下文,如下所示:
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;
};
}
但是虽然视图是用 headers 绘制的,但行是空的。我做错了什么?
编辑: 修改了代码以包含解决方案。谢谢@Suraj。
使用这个
View rowView = inflater.inflate(R.layout.myview, parent, false);
而不是
View rowView = inflater.inflate(R.layout.myview, null, true);
我正在尝试使用 class 本身包含的列表数据实现 ArrayAdapter,而不是通过调用 activity 传递。构造函数将只包含上下文,如下所示:
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;
};
}
但是虽然视图是用 headers 绘制的,但行是空的。我做错了什么?
编辑: 修改了代码以包含解决方案。谢谢@Suraj。
使用这个
View rowView = inflater.inflate(R.layout.myview, parent, false);
而不是
View rowView = inflater.inflate(R.layout.myview, null, true);