好的,我知道这个问题已经在这里被问了很多次,我尝试了所有的解决方案,但似乎都没有。我正在学习安卓学习书,我试图以ListView的形式显示数据库中的数据,使用SimpleCursorAdapter将其插入。应该显示数据的屏幕除标题外不显示任何内容。< / p>
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>
我已粘贴上面的代码。谁能告诉我什么弄乱了显示器?真的很感激帮助
谢谢!
答案 0 :(得分: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);