如何从对话框窗口与ListView进行同步?

时间:2018-02-13 08:24:13

标签: java android listview dialog synchronization

这是我的代码 -

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, final int position, final long id) {

        builder1 = new AlertDialog.Builder(context);
        alert1 = builder1.create();
        layoutInflater1 = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        layout1 = layoutInflater1.inflate(R.layout.alert_one, null);
        alert1.setView(layout1);

        buttonAlert_oneDelete = (Button) layout1.findViewById(R.id.buttonAlert_oneDelete);
        buttonAlert_oneUpdate = (Button) layout1.findViewById(R.id.buttonAlert_oneUpdate);
        buttonAlert_oneCancel = (Button) layout1.findViewById(R.id.buttonAlert_oneCancel);

        buttonAlert_oneDelete.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
                funDelete(id);
                simpleCursorAdapter.notifyDataSetChanged();
                alert1.cancel();
            }
        });

        buttonAlert_oneCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                alert1.cancel();
            }
        });     
        alert1.show();
    }
});

funDelete 方法

private void funDelete(long id) {
  db = abcDbHelper.getReadableDatabase(); 
  db.delete(AbcDbContract.TableWords.TABLE_NAME,AbcDbContract.TableWords.COLUMN_NAME_ID + " = " + String.valueOf(id), null); 
}

嗨!从数据库中删除行后, notifyDataSetChanged()方法不会立即 ListView 。怎么做的?非常感谢你。

1 个答案:

答案 0 :(得分:0)

SELECT a.key, a.transaction_id,(SELECT GROUP_CONCAT(lo.issue) FROM keys.key_list lo WHERE version_id = 2 AND lo.issue >= '2017-01-01' AND lo.key=a.key) as issue_dates FROM public.orders a WHERE a.type IN (2, 4) 不了解您的数据库表。适配器只知道其AdapterList的数据源。因此,在这种情况下,您必须从数据库重新加载数据并更新适配器。

Array

如果您使用CursorLoader,那么您不需要手动查询。 CursorLoader会自动注册 buttonAlert_oneDelete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { funDelete(id); // reload datafrom Database // update datasource of the simpleCursorAdapter simpleCursorAdapter.notifyDataSetChanged(); alert1.cancel(); } }); 以在数据更改时触发重新加载。您只需在任何更新/插入/删除操作后调用ContentObserver

检查this如何实施它。