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

}
4

1 回答 1

0

要获取活动的上下文,请使用getActvitiy().

http://developer.android.com/reference/android/app/Fragment.html#getActivity()

public final Activity getActivity ()

返回此片段当前关联的 Activity。

  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); 
于 2013-06-23T07:12:34.500 回答