我有一个关于自定义Android ListView项目的问题:
我正在学习本教程:http://codehenge.net/blog/2011/05/customizing-android-listview-item-layout/
第一部分,正常的R.android.simple_text_item_1对我来说很好,所以我在每一行都得到了带有字符串的ListView。
然后我继续尝试修改ListView的项目,如下所述:
我为列表项定义了这个XML数据。它是一个只包含两个TextViews的LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
我定义了一个数据类UserRecord,它简单地给了我创建包含名称和电子邮件地址的对象的能力
public class UserRecord {
public String username;
public String email;
public UserRecord(String username, String email) {
this.username = username;
this.email = email;
}
}
我复制了整个适配器类,但我收到一个错误,我评论的行
public class UserItemAdapter extends ArrayAdapter<UserRecord> {
private ArrayList<UserRecord> users;
public UserItemAdapter(Context context, int textViewResourceId, ArrayList<UserRecord> users) {
super(context, textViewResourceId, users);
this.users = users;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
//In the following line I get the error: "The method getSystemService is undefined
//for type UserItemAdapter
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_item, null);
}
UserRecord user = users.get(position);
if (user != null) {
TextView username = (TextView) v.findViewById(R.id.username);
TextView email = (TextView) v.findViewById(R.id.email);
if (username != null) {
username.setText(user.username);
}
if(email != null) {
email.setText("Email: " + user.email );
}
}
return v;
}
}
我在MainActivity中改变了我的onCreate方法,如下所示,也许这就是错误。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<UserRecord> users = new ArrayList<UserRecord>();
UserRecord a = new UserRecord("a","b");
users.add(a);
ListView listView = (ListView) findViewById(R.id.listView1);
//The following adapter worked fine when using a String array
//listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
//This adapter doesn't work I guess :(
listView.setAdapter(new UserItemAdapter(this, R.layout.list_item, users));
}
谁能告诉我,我的错在哪里?如果我尝试启动我的应用程序,它甚至没有启动,我的模拟器说“不幸的是关闭了blabla”。 -.-
答案 0 :(得分:0)
LayoutInflater是一个systemService,用于扩展布局,例如列表项的布局。为了访问任何系统服务,你需要一些上下文时间。然后尝试这个。
public class UserItemAdapter extends ArrayAdapter<UserRecord> {
private ArrayList<UserRecord> users;
private Context context
public UserItemAdapter(Context context, int textViewResourceId, ArrayList<UserRecord> users) {
super(context, textViewResourceId, users);
this.context=context;
this.users = users;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
//In the following line I get the error: "The method getSystemService is undefined
//for type UserItemAdapter
LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_item, null);
}
UserRecord user = users.get(position);
if (user != null) {
TextView username = (TextView) v.findViewById(R.id.username);
TextView email = (TextView) v.findViewById(R.id.email);
if (username != null) {
username.setText(user.username);
}
if(email != null) {
email.setText("Email: " + user.email );
}
}
return v;
}
}