listClass.add(new ListClass(json.getString("ProductId"), json
.getString("ProductPrice")));
}
list.setAdapter(new MyListAdapter(Fragment1.this, listClass));
但是在Class:MyListAdapter中我收到一个错误:
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = LayoutInflater.from(fragment).inflate(
R.layout.pricelist, parent, false);
fragment1类扩展片段。 而在rowView = layoutinflater.from(//我只能在这里扩展上下文 而不是片段)。 我该怎么办?
更新mylistadapter类:
public class MyListAdapter extends BaseAdapter {
private Fragment fragment;
private ArrayList<ListClass> listClass = new ArrayList<ListClass>();
public MyListAdapter(Fragment1 fragment1, ArrayList<ListClass> listClass1) {
this.fragment = fragment1;
this.listclass = listClass1;
}
@Override
public int getCount() {
return listClass.size();
}
@Override
public Object getItem(int position) {
return ListClass.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = LayoutInflater.from(fragment).inflate(
R.layout.pricelist, parent, false);
TextView text1 = (TextView) rowView.findViewById(R.id.title);
TextView text2 = (TextView) rowView.findViewById(R.id.details);
text1.setText(BeanClass.get(position).phoneName);
text2.setText(BeanClass.get(position).phonePrice);
return rowView;
}
}
答案 0 :(得分:0)
要获取活动的上下文,请使用getActvitiy()
。
http://developer.android.com/reference/android/app/Fragment.html#getActivity()
public final Activity getActivity ()
返回此片段当前与之关联的活动。
list.setAdapter(new MyListAdapter(getActivity(), listClass));
然后在你的MyListAdapter构造函数
中 Context mContext; // you can remove this
LayoutInflater mInflater;
private ArrayList<ListClass> listClass = new ArrayList<ListClass>();
public MyListAdapter(Context context, ArrayList<ListClass> listClass1)
{
mContext = context; // you can remove this
mInflater = LayoutInflater.from(mContext); // get rid of this statement
mInflater = LayoutInflater.from(context); // you can directly use context
this.listclass = listClass1;
}
然后在getView
View rowView = mInflater.inflate(
R.layout.pricelist, parent, false);