【问题标题】:Refresh custom cursor adapter after deleting database row删除数据库行后刷新自定义游标适配器
【发布时间】:2014-03-06 06:33:08
【问题描述】:

抱歉我的英语不好和一个愚蠢的菜鸟问题。 我有一个 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;
    }

}

}

【问题讨论】:

标签: android listview


【解决方案1】:

对于 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);
}

我以这种方式使用装载机,希望对您有所帮助。

【讨论】:

  • 谢谢,我考虑了一下,但我无法理解如何使用我的自定义适配器。我用主代码更新我的帖子,我希望你给我的代码建议。
  • 我更新了我的答案,检查它并告诉我它是否有效。
  • 是的,如果我退出我的应用程序,它会起作用(现在不必为刷新而使用 cilled 进程),但不能实时刷新。我只有一项活动,我不知道删除后如何强制刷新活动。
  • 你能给我解释一下吗?你也有 loginActivity ,不是吗?
  • public class MySimpleCursorAdapter extends SimpleCursorAdapter {.....} 放入您的mainActivity 类中,这意味着作为子类,因为您在mainActivity 中,它将以这种方式工作。这意味着public class MainActivity extends FragmentActivity implements LoaderCallbacks&lt;Cursor&gt; {............... private class MySimpleCursorAdapter extends SimpleCursorAdapter {.....}; } 这意味着在同一个java文件MainActivity.java中
【解决方案2】:

您应该重新加载您的光标并更新您的光标适配器:

// 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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-18
    相关资源
    最近更新 更多