抱歉我的英语不好,还有一个愚蠢的菜鸟问题。 我有一个SimpleCursorAdapter和每个项目上的按钮ListView(数据库中的行) 我意识到删除行但我不知道如何刷新ListView 我希望有人能用简单明了的例子来帮助我
我的适配器
public class MySimpleCursorAdapter extends SimpleCursorAdapter {
Context context;
public MySimpleCursorAdapter(Context contxt, int layout, Cursor c, String[] from, int[] to, int flags) {
super(contxt, layout, c, from, to, flags);
context=contxt;
}
public View newView(Context _context, Cursor _cursor, ViewGroup parent){
LayoutInflater inflater = (LayoutInflater) _context.getSystemService(_context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.listform_item, parent, false);
return view;
}
@Override
public void bindView(View view, Context Context, Cursor cursor) {
String name = cursor.getString(cursor.getColumnIndex(DBHelper.FORM_NAME));
String title = cursor.getString(cursor.getColumnIndex(DBHelper.FORM_TITLE));
TextView formname = (TextView)view.findViewById(R.id.tvFormName);
formname.setText(name);
TextView formtitle=(TextView)view.findViewById(R.id.tvFormTitle);
formtitle.setText(title);
ImageButton yourButton = (ImageButton)view.findViewById(R.id.ibtnDelete);
yourButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(view != null) {
Object obj = view.getTag();
//if(obj != null && obj instanceof Integer) {
dbForm form=new dbForm(context);
form.open();
String st=obj.toString();
form.deleteForm(Long.valueOf(st).longValue());
Toast.makeText(context, "Delete row with id = " + st, Toast.LENGTH_LONG).show();
}
}
});
Object obj = cursor.getString(cursor.getColumnIndex(DBHelper.FORM_ID));
yourButton.setTag(obj);
}
}
我还在Main中使用CursorLoader来访问数据库
UPD:我使用游标加载器而不是如何在我的自定义适配器中调用他的重置,希望得到帮助。
public class MainActivity extends FragmentActivity implements LoaderCallbacks<Cursor> {
ListView lvForms;
dbForm table_form;
SimpleCursorAdapter scAdapter;
/**
* Called when the activity is first created.
*/
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
table_form = new dbForm(this);
table_form.open();
String[] from = new String[]{DBHelper.FORM_NAME, DBHelper.FORM_TITLE};
int[] to = new int[]{R.id.tvFormName, R.id.tvFormTitle};
scAdapter = new MySimpleCursorAdapter(this, R.layout.listform_item, null, from, to, 0);
lvForms = (ListView) findViewById(R.id.lvForms);
lvForms.setAdapter(scAdapter);
registerForContextMenu(lvForms);
getSupportLoaderManager().initLoader(0, null, this);
}
public void onButtonClick(View view) {
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
}
protected void onDestroy() {
super.onDestroy();
table_form.close();
// закрываем подключение при выходе
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle bndl) {
return new MyCursorLoader(this, table_form);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
scAdapter.swapCursor(cursor);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
scAdapter.swapCursor(null);
}
static class MyCursorLoader extends CursorLoader {
dbForm table_form;
public MyCursorLoader(Context context, dbForm table_form) {
super(context);
this.table_form = table_form;
}
@Override
public Cursor loadInBackground() {
Cursor cursor = table_form.getAllData();
return cursor;
}
}
}
答案 0 :(得分:4)
对于SimpleCursorAdapter,最好使用像这样的CursorLoader:
public Loader<Cursor> onCreateLoader(int id,Bundle arg) {
return new SimpleCursorLoader(this) {
public Cursor loadInBackground() {
// This field reserved to what you want to load in your cursor adater and you return it (for example database query result)
// return Cursor ;
}
};
}
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
adapter.swapCursor(cursor);
}
public void onLoaderReset(Loader<Cursor> loader){
adapter.swapCursor(null);
}
在您的MainActivity中或您将实现适配器的位置,就在添加之前:
getLoaderManager().initLoader(0x01, null, this);
// declare your new Custom Simple Cursor Adapter here
scAdapter = new MySimpleCursorAdapter ....
并添加:
public void onResume() {
super.onResume();
// Restart loader so that it refreshes displayed items according to database
getLoaderManager().restartLoader(0x01, null, this);
}
我以这种方式与装载机一起工作,我希望它可以帮到你。
答案 1 :(得分:1)
您应该重新加载光标并更新光标适配器:
// Reload your cursor here
Cursor newCursor = ….;
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD){
adapter.swapCursor(newCurosr);
} else {
adapter.changeCursor(newCursor);
}
然后,通知列表视图有关数据更改的信息:
adapter.notifyDataSetChanged();