我编写了一个程序来使用SimpleCursorAdapter
更新我的列表视图。
我有EditText
输入手机号码,当我点击添加时,手机号码会存储在我的sqlite数据库中,同时在listview中显示。但问题是listview没有刷新,我尝试使用notifyDataSetChanged()
但没有工作。
Home_Page.java :这是我更新listview的活动
try{
dbListHelper = new DriverSqliteHelper(getBaseContext());
dbListHelper.open(getBaseContext());
}catch (Exception e){
e.printStackTrace();
}
columns = new String[]{DriverSqliteHelper.DbListHelper.DRIVER_USER_ID};
to = new int[]{R.id.DriverId};
getLoaderManager().initLoader(0, null, this);
columns = new String[]{DriverSqliteHelper.DbListHelper.DRIVER_USER_ID};
to = new int[]{R.id.DriverId};
driverStatusAdapter = new DriverStatusAdapter(getBaseContext(),
R.layout.view_userid_item,null,columns,to,0);
listDriverId.setAdapter(driverStatusAdapter);
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
return new CustomCursorLoader(getBaseContext());
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
if(driverStatusAdapter!=null && cursor!=null) {
driverStatusAdapter.swapCursor(cursor);
driverStatusAdapter.notifyDataSetChanged();
}else {
Log.v("Adapter null","OnLoadFinished: mAdapter is null");
}
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
if(driverStatusAdapter!=null) {
driverStatusAdapter.swapCursor(null);
} else {
Log.v("Adapter null", "OnLoadReset: mAdapter is null");
}
}
@Override
protected void onResume() {
super.onResume();
getLoaderManager().restartLoader(0x01, null, this);
}
我使用CustomCursorLoader类来查询结果。
public class CustomCursorLoader extends CursorLoader{
Context context;
DriverSqliteHelper driverSqliteHelper;
Cursor cursor;
public CustomCursorLoader(Context context) {
super(context);
try {
driverSqliteHelper = new DriverSqliteHelper(context);
driverSqliteHelper.open(context);
}catch (Exception e){
e.printStackTrace();
}
}
public Cursor loadInBackground(){
cursor = driverSqliteHelper.getDriverStatus();
return cursor;
}
}
当我重新启动我的应用时,列表视图会更新,但在我点击同一活动的添加按钮时不会自动更新。
我已经坚持了很长时间,而且每当我点击添加时我都不知道该怎么做才能刷新listview。
感谢任何帮助。