关闭对话框后如何刷新listView?

时间:2014-02-25 07:55:52

标签: android android-listview

我创建了一个打开对话框的布局,进入对话框后,用户选择确定/取消。我想刷新listView以重新查询数据

这是我打开对话框的布局:

 button.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) { 
                Intent myIntent = new Intent(SchoolActivity.this, InsertSchool.class);
                update();
                startActivity(myIntent);
                updateList();
            }
        });
        update();
        cursor.requery();
        String[] from = new String[]{Database.KEY_ID2, Database.KSCHOOL, Database.KSCHOOLCODE};
        int[] to = new int[]{R.id.rid, R.id.rt1, R.id.rt2};

        cursorAdapter =
            new SimpleCursorAdapter(this, R.layout.row_school, cursor, from, to);
        listContent.setAdapter(cursorAdapter);

单击该按钮后,我想刷新listView。这是对话框(我认为刷新将在ok& cancel按钮上完成。)

            ib.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent=new Intent(InsertSchool.this, SchoolActivity.class);
                startActivity(intent);
            }
        });
        update();
        mySQLiteAdapter = new Database(this);
        mySQLiteAdapter.openToWrite();
        cursor = mySQLiteAdapter.queueSchoolAll();

        ib1.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

                String data1 = ie1.getText().toString();
                String data2 = ie2.getText().toString();
                mySQLiteAdapter.insertSchool(data1, data2);
                updateList();

                Toast.makeText(getApplicationContext(), "Saved",
                           Toast.LENGTH_SHORT).show();

                Intent myIntent = new Intent(InsertSchool.this, SchoolActivity.class);
                update();
                startActivity(myIntent);

                mySQLiteAdapter.close();
                }
            }
        });

3 个答案:

答案 0 :(得分:2)

要刷新listView,您应该在列表的适配器上调用方法notifyDataSetChanged()。调用该方法取决于您...可能在您的onClickListener

答案 1 :(得分:1)

我只是使用这种技术:在主要活动中把这个

@Override
public void onWindowFocusChanged(boolean hasFocus) {
  super.onWindowFocusChanged(hasFocus);
  refreshData();  
}

一旦解除AlertDialog,它就会调用刷新ListView的therefreshData()方法

答案 2 :(得分:0)

您可以在对话框中使用.onCancelListener来知道您的对话框何时被取消。 然后您就可以引用您的数据