0

好的,我知道这个问题在这里被问了很多次,我已经尝试了所有的解决方案,但似乎都没有奏效。我正在关注学习 android 书,我正在尝试使用 SimpleCursorAdapter 以 ListView 的形式显示数据库中的数据以将其插入。应该显示数据的屏幕除了标题外不显示任何内容。

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>

我在上面粘贴了我的代码。谁能告诉我是什么弄乱了显示?非常感谢帮助

谢谢!!

4

1 回答 1

2

游标需要在创建 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);
于 2013-06-11T22:17:13.813 回答