2013-06-11 104 views
0

好吧,我知道這個問題已經在這裏問了很多次,我試過所有的解決方案,但似乎沒有工作。我正在學習Android書,我試圖使用SimpleCursorAdapter以ListView的形式顯示數據庫中的數據以將其插入。應該顯示數據的屏幕除了標題外不顯示任何內容。ListView + SimpleCursorAdapter不顯示數據

package com.example.yamba; 

import android.app.Activity; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.widget.ListView; 
import android.widget.SimpleCursorAdapter; 

public class TimelineActivity extends Activity { 
    static final String[] FROM = { StatusData.C_USER,StatusData.C_TEXT, StatusData.C_CREATED_AT}; 
    static final int[]TO = {R.id.text_user,R.id.text_text,R.id.text_createdAt}; 

    ListView list; 
    Cursor cursor; 
    SimpleCursorAdapter adapter; 

    @SuppressWarnings("deprecation") 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.timeline); 
     list = (ListView) findViewById(R.id.list); 


     adapter = new SimpleCursorAdapter(this, R.layout.row, cursor, FROM, TO); 
     list.setAdapter(adapter); 

     cursor = ((YambaApp) getApplication()).statusData.query(); 
//  while (cursor.moveToNext()) { 
//   String user = cursor.getString(cursor.getColumnIndex(StatusData.C_USER)); 
//   String text = cursor.getString(cursor.getColumnIndex(StatusData.C_TEXT)); 
//   list.append(String.format("\n%s: %s", user, text)); 
//  } 
    } 
} 



***Timeline.xml*** 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/my_blue" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/timeline" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="@string/Timeline" /> 

    <ListView 
     android:id="@+id/list" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

    </ListView> 
    <!-- 
<ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
    <TextView 
     android:id="@+id/timelineView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
</ScrollView> 
    --> 

</LinearLayout> 



***row.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="match_parent" > 

    <TextView 
     android:id="@+id/text_user" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:text="Slashdot" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/text_text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/text_createdAt" 
     android:text="A really long example tweet for just test purposes" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <TextView 
     android:id="@+id/text_createdAt" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/text_user" 
     android:layout_alignBottom="@+id/text_user" 
     android:layout_alignParentRight="true" 
     android:text="10 minutes ago" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

</RelativeLayout> 

我粘貼了上面的代碼。誰能告訴我什麼在搞亂顯示器?真的很感謝幫助

謝謝!!

回答

1

遊標需要在創建SimpleCursorAdapter之前創建,否則你傳遞給構造函數的是什麼。這是一個重寫:

super.onCreate(savedInstanceState); 

setContentView(R.layout.timeline); 
list = (ListView) findViewById(R.id.list); 


cursor = ((YambaApp) getApplication()).statusData.query(); 
adapter = new SimpleCursorAdapter(this, R.layout.row, cursor, FROM, TO); 
list.setAdapter(adapter); 
+0

哦哇。這讓它工作。非常感謝Lionel Port。我花了將近一個小時來嘗試調試它。 – user1660568

+0

沒問題,有時需要一雙新鮮的眼睛。 –