2013-05-08 188 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方法中那样创建动态文本视图。如果你需要一个例子让我知道。