0

我的代码是..

class TweetListAdaptor extends ArrayAdapter<Tweet> {

    private ArrayList<Tweet> tweets;

    public TweetListAdaptor(Context context,
                                int textViewResourceId,
                                ArrayList<Tweet> items) {
             super(context, textViewResourceId, items);
             this.tweets = items;
    }


   // @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.list_tem, null);
            }
            Tweet o = tweets.get(position);
            TextView tt = (TextView) v.findViewById(R.id.toptext);
            TextView bt = (TextView) v.findViewById(R.id.bottomtext);
            tt.setText(o.content);
            bt.setText(o.author);

            return v;
    }

}
4

3 回答 3

4

使用此代码:

LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

而不是这个:

LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
于 2013-11-21T09:33:31.067 回答
2

它不在活动中,所以这样称呼.. this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)

和你这样的构造函数..

private Context context;
public TweetListAdaptor(Context context,
                        int textViewResourceId,
                        ArrayList<Tweet> items) {
     super(context, textViewResourceId, items);
     this.tweets = items;
     this.context=context;
}
于 2013-10-26T11:04:40.553 回答
0

试试这个代码

class TweetListAdaptor extends ArrayAdapter<Tweet> {

private ArrayList<Tweet> tweets;
 private Context context;

public TweetListAdaptor(Context context,
                            int textViewResourceId,
                            ArrayList<Tweet> items) {
         super(context, textViewResourceId, items);
         this.tweets = items;
         this.context=context;
}


 // @Override
public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi=
        (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.list_tem, null);
        }
        Tweet o = tweets.get(position);
        TextView tt = (TextView) v.findViewById(R.id.toptext);
        TextView bt = (TextView) v.findViewById(R.id.bottomtext);
        tt.setText(o.content);
        bt.setText(o.author);

        return v;
}

}

于 2013-10-26T11:22:22.013 回答