Hunt
Hunt

Reputation: 8435

prevent data being loaded everytime in DialogFragment

I have created a custom AlergDialog using DialogFragment with the help of Android Compatibility Support Package

In my custom dialog i have a listview inside a dialog and the content of the listview are being loaded from Sqlite Database in Android. This dialog gets popped up from a listview which is inside a fragment only.

Now t, whenever i click on listview (which is inside a fragment) a dialog appears whit the data into listview successfully , but each time i press on listitem ( which is inside a fragment) every time , the data gets load from the database in a dialog box's listview bcoz onCreate is being called every time so wht i want is data should be loaded at once for all listitem of listview which is inside of fragment.

Code

Whenever a listitem in a fragment gets clicked , i make a call to custom DialogFragment

DalogFragment newFragment = TemplateToContact.newInstance("hi");
newFragment.show(getFragmentManager(), "dialog");

OnCreate of DialogFragment

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        contactDB = new ContactDB(getActivity().getApplicationContext());
    contactDataList = contactDB.getAllContacts();
        templateContactAdapter = new TemplateContactAdapter();

    }

in OnCreateDialog i create a custom alertdialog box , set the adapger and return it

@Override
 public Dialog onCreateDialog(Bundle savedInstanceState) {

   LayoutInflater factory = LayoutInflater.from(getActivity());         
   View v = factory.inflate(R.layout.cdialog, null);
   builder = new AlertDialog.Builder(getActivity());

   builder.setView(v);

   templateContactDlg = builder.create();                

   templateContactList = (ListView)v.findViewById(R.id.contactDlgList);            

   templateContactList.setAdapter(templateContactAdapter);

   return templateContactDlg;

  }

Upvotes: 0

Views: 1731

Answers (1)

Christine
Christine

Reputation: 5575

You say you use DialogFragment but your code says AlertDialog. Here is an example of how to use DialogFragment. The Google Android team recommends you use DialogFragment, not AlertDialog.

added:


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        if (savedInstanceState != null) {
            aboutFile = savedInstanceState.getString(FILE_NAME);
        }
    }



    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putString(FILE_NAME, aboutFile);
    }

For injecting helper classes, read this, for example.

Upvotes: 0

Related Questions