我从我的(测试)数据库中得到客户信息列表,我想显示它。客户由具有Customer
、name
和info
成员的note
类表示。它的toString
方法只返回name
。我创建了只使用DemoDatabaseMainActivity
布局的simple_list_item_1
,因此只显示客户的name
,如下所示:
public class DemoDatabaseMainActivity extends ListActivity {
private CustomerDataSource datasource;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
datasource = new CustomerDataSource(this);
datasource.open();
List<Customer> values = datasource.getAllCustomers();
ArrayAdapter<Customer> adapter = new ArrayAdapter<Customer>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
...
}
它工作得很好;但是,我想学习下一步…
我想修改代码,以便在第一行使用
android.R.layout.simple_list_item_2
,在第二行使用name
+info
?应该实现什么而不是note
,应该使用什么适配器或什么?根据patric的评论进行更新——关于这一点,我希望修改后的解决方案是这样的:
public class DemoDatabaseMainActivity extends ListActivity {
private CustomerDataSource datasource;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
datasource = new CustomerDataSource(this);
datasource.open();
List<Customer> values = datasource.getAllCustomers();
TwolineAdapter adapter = new TwolineAdapter(this, values); // here the difference
setListAdapter(adapter);
}
...
}
所以,我已经这样添加了我的
Customer.toString()
类:public class TwolineAdapter extends ArrayAdapter<Customer> {
private List<Customer> objects;
public TwolineAdapter(Context context, List<Customer> objects) {
super(context, android.R.layout.simple_list_item_2, objects);
this.objects = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text1 = (TextView) view.findViewById(android.R.id.text1);
TextView text2 = (TextView) view.findViewById(android.R.id.text2);
text1.setText(objects.get(position).getName());
text2.setText(objects.get(position).getInfo()
+ " (" + objects.get(position).getNote() + ")");
return view;
}
}
但它不起作用(显然是因为我的一些错误)。注意
TwolineAdapter
构造函数代码调用中的simple_list_item_2
。运行时,代码显示错误日志消息,如:E/ArrayAdapter(27961): You must supply a resource ID for a TextView
我想问你问题出在哪里。为了找到原因,我对
super
进行了修改,使其工作方式与原来的TwolineAdapter
相同。public class TwolineAdapter extends ArrayAdapter<Customer> {
private List<Customer> objects;
public TwolineAdapter(Context context, List<Customer> objects) {
super(context, android.R.layout.simple_list_item_1, objects);
this.objects = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView view = (TextView)super.getView(position, convertView, parent);
view.setText(objects.get(position).getInfo()); // displaying a different info
return view;
}
}
为了确保覆盖的
simple_list_item_1
有效,我显示了客户信息的另一部分。而且效果很好。换句话说,不是一般的getView
方法,而是显示特定toString
的结果。无论如何,我需要使用
getInfo
。接下来我该怎么办?(我的结论是我在构造函数中做了一些错误的事情。我说的对吗?问题在哪里?) 最佳答案
您可以重写getView
的ArrayAdapter
方法:
new ArrayAdapter (context, android.R.layout.simple_list_item_2, android.R.id.text1, list)
{
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text1 = (TextView) view.findViewById(android.R.id.text1);
TextView text2 = (TextView) view.findViewById(android.R.id.text2);
text1.setText("1");
text2.setText("2");
return view;
}
})