在对话框后从数据库刷新列表视图

时间:2014-08-01 19:33:39

标签: android sql listview

我有一个从SQL数据库生成的列表视图。

用户可以单击其中一个列表项,然后会弹出一个对话框,其中包含选项"编辑"或"删除"。用户还可以点击底部的按钮,然后添加"一个项目到数据库。所有这些功能都能正常工作,但为了刷新列表视图,用户需要返回活动然后再转发。如何在对话框关闭后自动刷新列表视图?

这是代码:

    final LevelTextDatabase ltb = new LevelTextDatabase(this);
    final List<LevelText> texts = ltb.getLevelTexts(level);

    final BaseAdapter theAdapter = new ArrayAdapter<LevelText>(this, android.R.layout.simple_list_item_1,
            texts);

    final ListView theListView = (ListView) findViewById(R.id.levelTextList);

    theListView.setAdapter(theAdapter);
    theListView.setFastScrollAlwaysVisible(true);

    theListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int position, long id)
        {
            CharSequence options[] = new CharSequence[] {"edit", "delete"};

            AlertDialog.Builder builder = new AlertDialog.Builder(EditLevelText.this);
            builder.setTitle("What would you like to do?");
            builder.setItems(options, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //user clicks edit
                    if (which == 0)
                    {
                        AlertDialog.Builder alert = new AlertDialog.Builder(EditLevelText.this);

                        alert.setTitle("Edit Criteria");
                        alert.setMessage("Edit: ");
                        final EditText input = new EditText(EditLevelText.this);
                        alert.setView(input);
                        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                ltb.editText(texts.get(position).getId(),
                                        String.valueOf(input.getText()));

                                //insert refresh command here????

                            }
                        });
                        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {

                            }
                        });
                        alert.show();

                    }
                    if (which == 1) {
                        ltb.deleteText(texts.get(position).getId());

                        //this refresh works

                        texts.remove(position);
                        theAdapter.notifyDataSetChanged();

                    }

                }
            });
            builder.show();

        }
    });

}

//method for add button
public void addLevelText(View view) {

    Intent prev = getIntent();
    final int level = prev.getExtras().getInt("level");

    final LevelTextDatabase ltb = new LevelTextDatabase(this);

    AlertDialog.Builder alert2 = new AlertDialog.Builder(EditLevelText.this);

    alert2.setTitle("Add Criteria");
    alert2.setMessage("Add: ");
    final EditText input = new EditText(EditLevelText.this);
    alert2.setView(input);
    alert2.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            ltb.addLevelText(new LevelText(level, String.valueOf(input.getText())));

            //insert refresh here????
        }
    });
    alert2.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {

        }
    });
    alert2.show();

}

2 个答案:

答案 0 :(得分:0)

您可以致电onDismiss并刷新该方法中的活动。

public abstract void onDismiss (DialogInterface dialog) {
  finish();
  startActivity(getIntent());
}

答案 1 :(得分:0)

制作最终的List文本和最终的BaseAdapter是MainActivity的Adapter成员并调用 每当文本发生变化时,都会发出notifyDatasetChanged。