简体   繁体   中英

Update Listview of an Activity from a Dialog

I have an activity for entering master data (Order Data) and after saving the order for entering Items in the order the user clicks a button and a Alertdialog pops up where the user enters Item and other details related to the item and press save. The user continues this process till all the Items are entered. I want after each save button press a listview at the parent activity to get refreshed and Items added in the order is displayed in the list one after another as the user adds item through the alert dialog. Please tell me how to do that. Thanks in advance.
EDITED
I am a fool (I should have thought this before putting the question) - as the Activity is calling the Alertdialog ... all variables are available to the dialog. So from the dialog I can retrieve all the items for that order and populate the listview available on the Activity. Thanks for all the suggestions.

Maybe...

  • Put your update data code in onResume() of your activity
  • Or, send a Broadcast event to your activity

Check in the official documentation how to send back data to your activity:

http://developer.android.com/guide/topics/ui/dialogs.html#PassingEvents

You have to implement a listener interface. In the callback method you can update your listview.

You can make a function to update your listdataitem, and call this function just after deleting a file from sdcard. It will referesh your listItem. You may call the same function for first time to setdata list.

ActivityClass.java

ListView listview;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listview = (ListView) findViewById(android.R.id.list); 
    refreshData();
listview .setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
                new AlertDialog.Builder(ActivityName.this)
                //.setIcon(R.drawable.folder)
                //.setTitle("title")
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                    public void onClick(DialogInterface dialog, int which) {
                        saveData();
                        refreshData();

                    }
                }).show();


            }
        });

    }
    void saveData(){
        //write code here fo save data
    }

    public void refreshData(){


        ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list);
         listview.setAdapter(adapter);

    }   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM