我将 SherlockListActivity 与 ListView 一起使用。当列表中没有项目时,我想显示 no_data 字符串。我使用@android:id/empty 遵循了几个示例,但它们对我不起作用。“空”文本视图永远不可见。任何帮助表示赞赏。这是我的 layout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ListView android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no_data"/>
</LinearLayout>
这就是我的列表视图在 MainActivity Java 文件中的填充方式
private void fillData()
{
// Get cursor of name, bal, date for account list
Cursor cursor = db.getCursor();
startManagingCursor(cursor);
// THE DESIRED COLUMNS TO BE BOUND
String[] columns = new String[] { "accounts.name", "accounts.bal", "accounts.ot"};
// THE XML DEFINED VIEWS WHICH THE DATA WILL BE BOUND TO
int[] to = new int[] { R.id.tvName,R.id.tvBal, R.id.tvDate };
// CREATE THE ADAPTER USING THE CURSOR POINTING TO THE DESIRED DATA AS WELL AS THE LAYOUT INFORMATION
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.main_list_item, cursor, columns, to);
// SET THIS ADAPTER AS YOUR LISTACTIVITY'S ADAPTER
this.setListAdapter(mAdapter);
}
这就是我在单独的 Java 类中创建光标的方式
// Getting All accounts
public Cursor getCursor() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_ACCOUNTS, new String[] {
KEY_AID, // Account ID Integer
KEY_NAME, // Name of account
KEY_BAL, // Balance
KEY_ADATE, // Occurrence time
},
KEY_TYPE + "=?",
new String[] {"2"}, null, null, null);
// return account list
return cursor;