CJanon
CJanon

Reputation: 101

How to refresh listView to after closing the dialog ?

I have created a layout that opens the dialog, and after entering the dialog, the user selects ok/cancel. I want to refresh the listView to requery the data

Here's my layout that would open the dialog:

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

After clicking that button, I want to refresh the listView. Here's the dialog (I think refreshing is going to be done here on the ok & cancel buttons).

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

Upvotes: 5

Views: 6383

Answers (3)

Abdurahman Popal
Abdurahman Popal

Reputation: 3019

You can use .onCancelListener in your dialog to know when ur dialog get cancel. Then u can referesh ur data

Upvotes: 0

Adam Bedoui
Adam Bedoui

Reputation: 43

I simply use this technique : in the main activity put this

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

as soon the AlertDialog is dismissed, it calls therefreshData() method that refreshs the ListView

Upvotes: 1

Luciano Rodríguez
Luciano Rodríguez

Reputation: 2309

To refresh a listView you should call the method notifyDataSetChanged() on your list's adapter. When calling that method is up to you... maybe inside your onClickListener.

Upvotes: 2

Related Questions