android – 删除数据库行后刷新自定义游标适配器

android – 删除数据库行后刷新自定义游标适配器,第1张

概述对不起我的英语不好和一个愚蠢的菜鸟问题. 我有一个SimpleCursorAdapter和每个项目上的按钮ListView(数据库中的行). 我意识到删除行但我不知道如何刷新ListView. 我希望有人能用简单明了的例子来帮助我 我的适配器 public class MySimpleCursorAdapter extends SimpleCursorAdapter {Context cont 对不起我的英语不好和一个愚蠢的菜鸟问题.
我有一个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;}@OverrIDepublic 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,0);    lvForms = (ListVIEw) findVIEwByID(R.ID.lvForms);    lvForms.setAdapter(scAdapter);    registerForContextMenu(lvForms);    getSupportLoaderManager().initLoader(0,this);}public voID onbuttonClick(VIEw vIEw) {    Intent intent = new Intent(MainActivity.this,LoginActivity.class);    startActivity(intent);}protected voID onDestroy() {    super.onDestroy();    table_form.close();    // закрываем подключение при выходе}@OverrIDepublic Loader<Cursor> onCreateLoader(int ID,Bundle bndl) {    return new MyCursorLoader(this,table_form);}@OverrIDepublic voID onLoadFinished(Loader<Cursor> loader,Cursor cursor) {    scAdapter.swapCursor(cursor);}@OverrIDepublic 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;    }}

}

解决方法 对于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,this);// declare your new Custom Simple Cursor Adapter herescAdapter = new MySimpleCursorAdapter ....

并添加:

public voID onResume() {    super.onResume();    // Restart loader so that it refreshes displayed items according to database    getLoaderManager().restartLoader(0x01,this);}

我以这种方式与装载机一起工作,我希望它对你有帮助.

总结

以上是内存溢出为你收集整理的android – 删除数据库行后刷新自定义游标适配器全部内容,希望文章能够帮你解决android – 删除数据库行后刷新自定义游标适配器所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1129392.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存