How to invalidate a listview from another class

320 Views Asked by At

When I call a Dialog from another Class which is not an Activity, I want to Invalidate the Listview from its calling Activity and NotifyDatasetChange the Adapter after dismissing the Dialog.

public class SingleAudioBook extends Activity {
    private ListView lv;
    public void onCreate(Bundle savedInstanceState) {
        lv = (ListView) findViewById(R.id.lvSingleSong);
 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    MyDialog.callLoginDialog(SingleAudioBook.this);
}}}

and my dialog is:

public class MyDialog {
private static Dialog myDialog;
 public static void callLoginDialog(final Context c){
...
}
1

There are 1 best solutions below

2
On

You can use static reference of listView and Adapter. Replace your declaration with following-

Your_Activity -

   public static BaseAdapter your_adapter_name;
    public static ListView your_lv_name;

Use in another class-

// refresh listview
if( Your_Activity.your_lv_name!=null && Your_Activity.your_adapter_name!=null )
{
        Your_Activity.your_adapter_name.notifyDataSetChanged();
        Your_Activity.your_lv_name.refreshDrawableState();
        Your_Activity.your_lv_name.invalidateViews();
}