2013-04-17 18 views
3

我從我的(測試)數據庫中獲取客戶信息列表,並且想要顯示它。客戶由Customer類別代表,name,infonote成員。其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。我應該做什麼? (我的結論是我做錯了什麼在構造函數中我說的對問題出在哪裏。?)

回答

17

您可以覆蓋ArrayAdaptergetView方法:

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; 
    } 
    }) 
+0

我明白了。它在'ArrayAdapter'文檔中是正確的(http://developer.android。COM /參考/機器人/插件/ ArrayAdapter.html)。如果我理解得很好,我應該創建自己的類來擴展'ArrayAdapter '。它的'getView'將會是你建議的覆蓋,對嗎? – pepr

+1

@pepr你說得對! – Patrick

+0

謝謝帕特里克的提示。請看看最新的問題。 – pepr

2

退房this教程如何創建一個自定義listView。作爲初學者,它會從新的項目階段開始,我認爲它會幫助你。至於你應該用什麼來代替你的toString()方法,只需在你的Customer類中創建一些getter來檢索數據。事情是這樣的:

public String getName(){ 
return this.name; 
} 

而在你的代碼中使用這樣的:

Customer x = new Customer(); 
//return the customer name into a string, obviously you will return it in a list that you will pass it to the listview 
String customerName = x.getName(); 
+0

謝謝,Cosmin,提示。 (1) – pepr