我的应用程序使用 cursorloader 将 sqlite 数据填充到 ListView 中。实际上,游标加载器应该只将数据库中的一列 (COLUM_NAME_SITE) 填充到列表视图中。我遇到的问题是,一旦在数据库中插入信息,就会创建一个列表项(我可以通过每次插入数据时显示的行来判断)但列表视图中没有显示文本,列表视图基本上是空白的。我相信一旦单击列表视图项,这也会使应用程序崩溃。
我假设我的 FROM 和 TO 数组以及光标适配器已正确创建,但我可能是错的。问题可能出在我的布局上吗?我不确定,但我希望有人能深入研究我的代码,让我知道我哪里出错了。
在单击空白列表视图项之前,我没有收到任何错误。
装载机类:
public class LoginList extends FragmentActivity implements AdapterView.OnItemClickListener, OnClickListener, LoaderManager.LoaderCallbacks<Cursor> {
private ListView loginList;
private Button webLogin;
private SimpleCursorAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_listview);
loginList = (ListView)findViewById(R.id.loginlist);
loginList.setOnItemClickListener(this);
webLogin = (Button)findViewById(R.id.button3);
webLogin.setOnClickListener(this);
//Specify fileds to display in the list
String[] from = new String[] { ListProvider.COLUMN_NAME_SITE };
//Bind fields to listview
int[] to = new int[] {R.id.loginlist };
// Create CursorAdapter and set it to display
adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, null, from, to);
loginList.setAdapter(adapter);
getSupportLoaderManager().initLoader(0, null, this);
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Selected ID :" + arg2, Toast.LENGTH_SHORT).show();
Intent updateDeleteLoginInfo = new Intent (this, UpdateDeleteLoginList.class);
Cursor clickedObject = (Cursor)loginList.getItemAtPosition(arg2);
Bundle loginBundle = new Bundle();
loginBundle.putString("clickedWebSite",((LoginDetails) clickedObject).getsName());
loginBundle.putString("clickedWebAddress",((LoginDetails) clickedObject).getwUrl());
loginBundle.putString("clickedUserName",((LoginDetails) clickedObject).getuName());
loginBundle.putString("clickedPassWord",((LoginDetails) clickedObject).getpWord());
loginBundle.putString("clickedNotes",((LoginDetails) clickedObject).getlNotes());
updateDeleteLoginInfo.putExtras(loginBundle);
startActivityForResult(updateDeleteLoginInfo, 0);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent webLoginIntent = new Intent (this, LoginPlusActivity.class);
startActivity(webLoginIntent);
}
@Override
public Loader<Cursor> onCreateLoader(int ignored, final Bundle args) {
return new CursorLoader(this, ListProvider.CONTENT_URI, null, null, null, null);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
adapter.swapCursor(cursor);
}
@Override
public void onLoaderReset (Loader<Cursor> loader) {
adapter.swapCursor(null);
}
}
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ns="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/loginlist"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/button3"
android:layout_alignParentTop="true" />
<Button
android:id="@+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="ADD" />
</RelativeLayout>
数据库:
//Database Columns
public static final String COLUMN_ROWID = "_id";
public static final String COLUMN_NAME_SITE = "sName";
public static final String COLUMN_NAME_ADDRESS = "wUrl";
public static final String COLUMN_NAME_USERNAME = "uName";
public static final String COLUMN_NAME_PASSWORD = "pWord";
public static final String COLUMN_NAME_NOTES = "lNotes";
// Database related Constants
public static final String DATABASE_NAME = "SiteLogindb";
public static final int DATABASE_VERSION = 2;
public static final String DSTORE_CREATE = "create table if not exists " +
TABLE_NAME_INFOTABLE + " ("+ COLUMN_ROWID + " integer primary key autoincrement,"
+ COLUMN_NAME_SITE + " text not null,"
+ COLUMN_NAME_ADDRESS + " text not null,"
+ COLUMN_NAME_USERNAME + " text not null,"
+ COLUMN_NAME_PASSWORD + " text not null,"
+ COLUMN_NAME_NOTES + " text not null);";