2012-03-02 72 views
0

我試圖顯示有關對象,具體是客戶的信息。我的意思是姓名,姓氏,電話號碼,地址,....這是我的數據庫查詢的結果。將包含在對象中的信息顯示到ListView中

我想在ListView上顯示它,所以如果我的活動繼承了ListActivity,因爲ListView默認是自包含的,所以更好。我有一個名爲Customer的類,通過查詢(Cursor)保存所有信息,只返回一個記錄(客戶),我不知道如何傳遞這個對象,我沒有找到最合適的適配器。

我可以這樣做,或者我必須將此對象轉換爲包含所有信息的ArrayList,事實上,我的適配器使用ArrayAdapter?

任何人都知道如何做到這一點?

謝謝。

+0

我的第一個想法是存儲記錄的信息,該信息將光標返回到對​​象Customer中。我想我應該把它轉換成包含所有信息的ArrayList 。 – danigonlinea 2012-03-02 13:28:07

+0

ArrayList 您可以使用此.......... – 2012-03-02 13:29:44

+0

我想顯示一個客戶的信息,它只是一個客戶。我的意思是,在屏幕上,名稱:name_custormer,lastname:lastname_customer,.... etc ...在ListView的每一行上執行此操作,但我不知道如何執行此操作...對不起,我是新秀。 – danigonlinea 2012-03-02 13:48:10

回答

2

一旦你的光標查詢,你應該做這樣的事情

String[] from = new String[]{"FirstName"}; 
int[] to = new int[]{R.id.row}; 

SimpleCursorAdapter sca = new SimpleCursorAdapter(this, R.layout.row_item, cur, from, to); 
setListAdapter(sca); 

檢查這裏SimpleCursorAdapter的文檔。 如果您需要做更復雜的東西與你的意見,你應該實現自己的自定義光標適配器:

public class ExampleCursorAdapter extends CursorAdapter { 
    public ExampleCursorAdapter(Context context, Cursor c) { 
     super(context, c); 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
     TextView summary = (TextView)view.findViewById(R.id.summary); 
     summary.setText(cursor.getString(
       cursor.getColumnIndex(ExampleDB.KEY_EXAMPLE_SUMMARY))); 
    } 

    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     LayoutInflater inflater = LayoutInflater.from(context); 
     View v = inflater.inflate(R.layout.item, parent, false); 
     bindView(v, context, cursor); 
     return v; 
    } 
} 
+0

感謝您的回答。它可以是我的解決方案。但我會問一個問題。我通過這種方式來顯示屏幕.... 1行:名字:佩佩 - 2行:姓:馬丁內斯 - 3行電話:6000005555 ...並以這種方式顯示數據....你怎麼推薦我,我可以做? – danigonlinea 2012-03-02 13:33:03

0

最後,我可以找到解決辦法的那樣。我要告訴你我是怎麼做的。

我希望通過這種方式來顯示詳細信息的客戶:

  1. 行1>名稱:NAME_VALUE
  2. 行2->姓:lastname_value
  3. 排3->電話:phone_value 。 ..

N.ň行 - > n_field:n_value

字段描述什麼v Alue將顯示,我的意思是「姓名:」,「姓氏:」,「電話:」,... 我把所有這些值放在字符串數組。我通過一個字符串[]來捕獲該字符。

值,顯示正確的字段後的正確值。我把所有的ArrayList一個內部 和明年只需要編寫正確的適配器:

這個活動繼承ListActivity的ListView默認。

SimpleCursorAdapter是不妥當的,因爲我們必須通過光標作爲該適配器的說法,但我的光標只返回結果只有一個,只有一個記錄

And CursorAdapter由於相同的原因而被拒絕。接下來我可以做什麼?ArrayAdapter

public class DetalleClienteAdapter extends ArrayAdapter<String> 
{ 
    private String[] campos_cliente; 
    private ArrayList<String> detalles_cliente; 


    public DetalleClienteAdapter(Context contexto, int layout, ArrayList<String> detalles) 
    { 
     super(contexto,layout,detalles); 
     //mInflater = LayoutInflater.from(contexto); 

     // LOS VALORES DE LOS CAMPOS: 
     detalles_cliente = detalles; 

     // CAMPOS DE DETALLES: nombre, apellidos, etc... 
     android.content.res.Resources res = getResources(); 
     campos_cliente = res.getStringArray(R.array.array_detalle_cliente); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     // TODO Auto-generated method stub 
     //return super.getView(position, convertView, parent); 
     LayoutInflater inflater= getLayoutInflater(); 
     View fila = inflater.inflate(R.layout.detalles_cliente, parent, false); 

     TextView campo =(TextView)fila.findViewById(R.id.detalles_campo); 
     TextView valor =(TextView)fila.findViewById(R.id.detalles_valor); 

     campo.setText(campos_cliente[position]); 
     valor.setText(detalles_cliente.get(position)); 

     return fila; 

    } 

} 

現在,我會寫一個更有效的適配器,帶ViewHolder。你對此有何看法?

謝謝。

相關問題