Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
                }
            }
        });
like image 306
CJanon Avatar asked Oct 20 '22 13:10

CJanon


2 Answers

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.

like image 135
Luciano Rodríguez Avatar answered Oct 23 '22 05:10

Luciano Rodríguez


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

like image 35
Adam Bedoui Avatar answered Oct 23 '22 05:10

Adam Bedoui