How to update listView before closing a dialog box?

I created a layout that opens a dialog box, and after entering the dialog box, the user selects ok / cancel. I want to update listView to request data

Here is my layout that will 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 this button, I want to update the listView. Here is the dialogue (I think the update will be done here on the ok and 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();
                }
            }
        });
+4
source share
2 answers

To update listView, you must call the method notifyDataSetChanged()on your list. When you call this method, it is up to you ... perhaps within yours onClickListener.

+2
source

I just use this technique: put it in the main activity

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

AlertDialog , therefreshData(), ListView

+1

All Articles