我想在我的 Preferences 类中设置一个自定义列表视图适配器,其中一个适配器位于我的主类中。当我尝试:
MyActivity.listView.setAdapter(new MyActivity.UserItemAdapter2(Prefs.this, R.layout.listitem, MyActivity.tweets));
我收到一条错误消息,指出“无法访问 MyActivity 类型的封闭实例。” 我不能使我的适配器类静态,因为它调用getSystemService
,这是一个非静态方法。如何解决这个问题?任何帮助是极大的赞赏。这是我的适配器类:
public class UserItemAdapter2 extends ArrayAdapter<Tweet> {
private ArrayList<Tweet> tweets;
public UserItemAdapter2(Context context, int textViewResourceId, ArrayList<Tweet> tweets) {
super(context, textViewResourceId, tweets);
this.tweets = tweets;
}
@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.listitem, null);
}
Tweet tweet = tweets.get(position);
if (tweet != null) {
TextView username = (TextView) v.findViewById(R.id.username);
TextView message = (TextView) v.findViewById(R.id.message);
ImageView image = (ImageView) v.findViewById(R.id.avatar);
if (username != null) {
username.setText(tweet.username);
}
if(message != null) {
message.setText(tweet.message);
}
if(image != null) {
//image.setImageBitmap(getBitmap(tweet.image_url));
tango.DisplayImage(tweet.image_url, image);
}
}
return v;
}
}