【发布时间】:2014-05-29 04:54:53
【问题描述】:
我正在构建我的第一个 AndroidApp,但无法让 ListView 填充数据库中的数据。数据肯定在那里,因为我可以循环遍历它和 Toast() 它,然后数据就会显示出来——由于某种原因,我只是无法将它放入 ListView。下面是代码:
ListSmsRecipients.java
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class ListSmsRecipients extends Base_Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.smslist);
Button addSmsRecipient = (Button) findViewById(R.id.btn_listSms_addSms);
DbHelper dbhelper = new DbHelper(getApplicationContext());
SQLiteDatabase db = dbhelper.getWritableDatabase();
String[] from = new String[] { DbHelper.SMS_NAME, DbHelper.SMS_PHONE, DbHelper.SMS_MESSAGE };
String[] column = new String[] {DbHelper.SMS_ID, DbHelper.SMS_NAME, DbHelper.SMS_PHONE, DbHelper.SMS_MESSAGE };
int[] toViewIDs = new int[] { R.id.temp_smslist_item_name, R.id.temp_smslist_item_phone, R.id.temp_smslist_item_message };
Cursor cursor = db.query(DbHelper.SMS_TABLE_NAME, column, null, null, null, null, null);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.smslist_item, cursor, from, toViewIDs);
ListView list = (ListView)findViewById(R.id.listview_smsList);
list.setAdapter(adapter);
// This works
cursor.moveToFirst();
while (cursor.moveToNext())
{
String name = cursor.getString(cursor.getColumnIndex(DbHelper.SMS_NAME));
String phone = cursor.getString(cursor.getColumnIndex(DbHelper.SMS_PHONE));
String message = cursor.getString(cursor.getColumnIndex(DbHelper.SMS_MESSAGE));
Toast.makeText(getApplicationContext(), "Name = " + name + "\nPhone = " + phone + "\nMessage = " + message, Toast.LENGTH_SHORT).show();
}
cursor.close();
}
}
BaseActivity.java
import android.support.v4.app.FragmentActivity;
public class Base_Activity extends FragmentActivity
{
}
smslist.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="@+id/listview_smsList"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
smslist_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/temp_smslist_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text" />
<TextView
android:id="@+id/temp_smslist_item_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text" />
<TextView
android:id="@+id/temp_smslist_item_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
【问题讨论】:
-
你的光标有数据吗?你的适配器是 cursorAdapter 吗?
-
试试 SimpleCursorAdapter 适配器 = new SimpleCursorAdapter(getApplicationContext(), R.layout.smslist_item, cursor, column, toViewIDs);
-
不要在 oncreate() 中关闭光标;
-
光标未填充时出现的问题
-
在创建时关闭光标不正确。并且也代替 FragmentActivity 使用 Activity 本身。 FragmentActivity 用于加载 Fragment。
标签: android listview android-fragmentactivity