我從我的(測試)數據庫中獲取客戶信息列表,並且想要顯示它。客戶由Customer
類別代表,name
,info
和note
成員。其toString
方法只返回name
。我已經創建了一個只使用simple_list_item_1
佈局DemoDatabaseMainActivity
,從而只顯示客戶的name
- 這樣的:如何構建並顯示simple_list_item_2中的信息?
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
?應該實施什麼而不是Customer.toString()
,以及使用什麼適配器或什麼?根據帕特里克的評論https://stackoverflow.com/a/16062742/1346705
更新 - 對於這一點,我也希望這樣的修改的方案:
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);
}
...
}
所以,我已經加了我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_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;
}
}
但它沒有工作(顯然是因爲我的一些錯誤)。請注意0構造函數代碼調用中的simple_list_item_2
。運行時,代碼顯示如下錯誤日誌消息:
E/ArrayAdapter(27961): You must supply a resource ID for a TextView
我想問問你問題出在哪裏。試圖找出原因,我已經修改了TwolineAdapter
以相同的方式工作的原始與simple_list_item_1
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;
}
}
可以肯定的重寫getView
作品,我已經顯示了客戶信息的不同部分。它工作得很好。換句話說,不是一般的toString
方法,而是顯示了我的具體getInfo
的結果。無論如何,我需要使用simple_list_item_2
。我應該做什麼? (我的結論是我做錯了什麼在構造函數中我說的對問題出在哪裏。?)
我明白了。它在'ArrayAdapter'文檔中是正確的(http://developer.android。COM /參考/機器人/插件/ ArrayAdapter.html)。如果我理解得很好,我應該創建自己的類來擴展'ArrayAdapter'。它的'getView'將會是你建議的覆蓋,對嗎? –
pepr
@pepr你說得對! – Patrick
謝謝帕特里克的提示。請看看最新的問題。 – pepr