CursorLoader没有填充ListView [HELP]

时间:2013-05-01 18:06:55

标签: sqlite android-layout listview simplecursoradapter android-fragmentactivity

我的应用程序使用游标加载器将sqlite数据填充到ListView中。实际上,cursorloader应该只将一个列(COLUNM_NAME_SITE)从DB填充到listview中。我遇到的问题是一旦信息被插入到数据库中,就会创建一个列表项(我可以通过每次插入数据时显示的行线来判断)但列表视图中没有显示文本,列表视图基本上是空白的。我相信这也会在点击列表视图项后使应用程序崩溃。

我假设我的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);";

1 个答案:

答案 0 :(得分:2)

您可以通过不同的方式扩展ListActivity并实现LoaderManager.LoaderCallbacks。

public class LoginActivity extends ListActivity implements LoaderManager.LoaderCallbacks<Cursor> {...

Android Design Patterns博客上有一个很好的4部分教程。

相关问题