2012-07-09 101 views
0

在我的應用程序中,我試圖在listview中顯示來自數據庫的值。我無法將數據添加到cursoradapter。我提到了簡單適配器示例中的代碼,但是我遇到了一個問題(活動中沒有顯示任何內容)。Android中的Cursoradapter listview

這裏是我的代碼:

super.onCreate(savedInstanceState); 
setContentView(R.layout.report); 
db.open(); 
Cursor c = db.getAllTitles(); 
startManagingCursor(c); 
String[] from = new String[] { db.KEY_INCOME,db.KEY_DESC}; 
int[] to = new int[] {R.id.text1 ,R.id.text2}; 
SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.columnview, c, from, to); 
//System.out.println("notes="+notes.getCount()); 
setListAdapter(notes);  

我mainXml代碼:

<?xml version="1.0" encoding="utf-8"?> 
<ListView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@id/android:list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#bfffdf" 
    android:drawSelectorOnTop="false"> 
</ListView> 

我的專欄XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:paddingTop="4dip" 
    android:paddingBottom="6dip" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
    <TextView android:id="@+id/text1" 
     android:textColor="#00ff00" 
     android:textSize="18sp" 
     android:layout_width="30dip" 
     android:layout_height="wrap_content"/> 
    <TextView android:id="@+id/text2" 
     android:textColor="#ff0000" 
     android:textSize="18sp" 
     android:layout_width="110dip" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 

</LinearLayout> 
+0

您必須在Logcat中獲得一些例外。請檢查並粘貼到此處。 – 2012-07-09 07:34:45

+0

我沒有得到任何異常there.I認爲問題是在setListAdapter(註釋); - >這可能只是簡單的adapter.But我使用遊標適配器。 – Prakash 2012-07-09 07:36:52

+0

檢查c.getCount();正在返回使用logs.maybe您的光標不包含任何記錄。 – 2012-07-09 07:39:43

回答

0

在這裏你去。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:paddingBottom="6dip" 
    android:paddingTop="4dip" > 

    <TextView 
     android:id="@+id/text1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="10dp" 
     android:layout_weight="1" 
     android:textColor="#00ff00" 
     android:textSize="18sp" /> 

    <TextView 
     android:id="@+id/text2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:textColor="#ff0000" 
     android:textSize="18sp" /> 

</LinearLayout> 
相關問題