对于TweetListAdaptor类型,未定义getSystemService(String)方法

时间:2013-10-26 11:02:52

标签: android twitter android-listview adapter

我的代码是..

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

}

3 个答案:

答案 0 :(得分:4)

使用此代码:

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

而不是:

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

答案 1 :(得分: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;
}

答案 2 :(得分: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;
}

}