2013-05-08 139 views
3

我有一個應用程序,從數據庫中取出字符串,並將其放入ListView中。 這是從數據庫中獲取我的字符串代碼:TextView是ListView沒有正確顯示

public void setLogView(String date){ 


    ArrayAdapter<TextView> adapter = new ArrayAdapter<TextView>(this, android.R.layout.simple_list_item_1); 
    listView.setAdapter(adapter); 

    DataBaseMain dataBase = new DataBaseMain(this); 
    dataBase.open(); 
    String[][] all = dataBase.dayLog(date); 
    dataBase.close(); 

    if(all == null) 
     return; 

String temporay = ""; 

    for(int j = 0; j < all[0].length; j++){ 
      temporay = ""; 
     for (int i = 0; i < all.length; i++){ 
      TextView text = new TextView(this); 
      temporay = temporay + " " + all[i][j]; 
      text.setText(temporay); 
      adapter.add((TextView)text); 
      } 
     } 
} 

它似乎,我在我的ListView獲得新的TextView但文字搞砸了。 我檢查了我的temporay字符串,並沒有問題。 在某處把他放在ListView中。 logcat或異常中沒有錯誤。 這裏是我在我的應用程序的ListView insted的我想要的文字(我想把picutrue,但我沒有足夠的repetion:

enter image description here

+0

你能分享一個截圖嗎你在等什麼你在看什麼 – CRUSADER 2013-05-08 06:10:39

+0

我給你添加了聲望,所以你可以添加一張圖片 – Alex 2013-05-08 06:10:43

+0

請添加截圖。 – 2013-05-08 06:31:03

回答

1

在那裏,它成爲與您提供

嘗試圖像清晰,這個..

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1); 
    listView.setAdapter(adapter); 

你的適配器附加TextView的對象,而不是字符串您提供。

再加入temporay而不是TextView的裏面你loop..like

adapter.add(temporay); 

這肯定會解決你的問題。

+0

其工作:) 非常感謝:) – tomer 2013-05-08 10:48:35

0

更改您的適配器ArrayAdapter<String>,並添加temporay代替整個列表視圖。

否則,您可以延長一個ArrayAdapter和覆蓋getView()

+0

謝謝,它爲我工作:) – tomer 2013-05-08 10:49:02

0

假設,您嘗試使用不同的layout.xml其中只包含textvie顯示在列表視圖定製文本w在裏面。

檢查下面給我的例子,這是我的表現如何實現這一點:

首先取指你要顯示並將其存儲在一個ArrayList中的數據。這裏,al_rec_id,al_rec_name分別是IntegerString類型的陣列列表。

cursor = database.query("records_data", new String[]{"rec_id", "rec_name"}, "cat_id=?", new String[]{cat_id+""}, null, null, null); 
      if(cursor.getCount() == 0) 
       Toast.makeText(getBaseContext(), "No records found.", Toast.LENGTH_SHORT).show(); 
      else 
      { 
       while(cursor.moveToNext()) 
       { 
        al_rec_id.add(cursor.getInt(0)); 
        al_rec_name.add(cursor.getString(1)); 
       } 
       cursor.close(); 
      } 

之後,結合此ArrayList與ArrayAdapter,然後將該適配器到如下的ListView。在這裏,array_adapter_all_records是String類型

array_adapter_all_records = new ArrayAdapter<String>(this, R.layout.single_row_home, R.id.textViewSingleRowHome, al_rec_name); 
     listview_all_records.setAdapter(array_adapter_all_records); 

的ArrayAdapter這是我single_row_home.xml代碼:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:padding="15dp" 
    android:background="#ffffff" > 

    <TextView 
     android:id="@+id/textViewSingleRowHome" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     style="@style/listview_only_textview" 
     android:text="TextView" /> 

</RelativeLayout> 

完蛋了。你完成了...... !!!

+0

謝謝你,它爲我工作:) – tomer 2013-05-08 10:49:43

0

創建一個類定製適配器,它將擴展您的ArrayList適配器您可以使用包含您的textview的不同xml充氣,也可以像在自定義適配器中的getView方法中那樣創建動態文本視圖。如果你需要一個例子讓我知道。