求解:ListView有数据不显示问题
经跟踪测试,cursor已取得数据,并已用别的方法证实,问题是用listview显示不出cursor中的数据。
1.ListView的布局(文件名activity_logiin.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.ldl.gunxueqiu.app.LoginActivity">
<ListView
android:id="@+id/login_listView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
2.Item的布局文件(文件名listitem_priciple.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<textview android:id="@+id/list_Principle"
android:textColor="#ffff00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<textview android:id="@+id/list_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
3.代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
DatabaseHelper dbHelper = new DatabaseHelper(this);
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM Principle WHERE PrincipleClass ='91' ORDER BY ShowOrder", null);
SimpleCursorAdapter listAdapter = new SimpleCursorAdapter (this,R.layout.listitem_principle, cursor,
new String[]{ "PrincipleContent","_id" },
new int[]{R.id.list_Principle,R.id.list_id},
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
ListView lv = (ListView)findViewById(R.id.login_listView);
lv.setAdapter(listAdapter);
cursor.close();
}
4.库表结构
CREATE TABLE Principle ("_id" integer NOT NULL PRIMARY KEY,PrincipleClass text NOT NULL DEFAULT 00,PrincipleContent text,UpdateTime text,ShowOrder integer DEFAULT 0)