0

I've extended Application and declared a public ArrayList there that I use to hold data (I know it's not as a Singleton in android should be, but it was the only thing I could make work).

The problem : When I display my CreateNewObject dialog and press OK I get the following error.

Error:

10-30 10:20:19.069: E/AndroidRuntime(632): java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(16908298, class android.widget.ListView) with Adapter(class com.nuclear.gfr.adapter.PatientAdapter)]

My code:

public class NewPatientDialog extends DialogFragment {

    private static final String TAG = "NUKClear";

    OnNewPatientlistener listener;

    private NewPatientDialog() {

    }

    public interface OnNewPatientlistener {
        public void OnNewPatient(Patient newPatient);
    }

    public void addPatientAction(Patient patient) {
        OnNewPatientlistener activity = (OnNewPatientlistener) getActivity();
        activity.OnNewPatient(patient);
    }

    public static NewPatientDialog newInstance(String title) {
        NewPatientDialog frag = new NewPatientDialog();
        Bundle args = new Bundle();
        args.putString("title", title);
        frag.setArguments(args);
        return frag;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        String title = getArguments().getString("title");

        LayoutInflater inflater = (LayoutInflater) getActivity()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        final View newPatient = inflater.inflate(R.layout.new_patient_dialog,
                null);
        Log.d(TAG, "Creating dialog for new patientinput...");
        return new AlertDialog.Builder(getActivity())
                .setIcon(R.drawable.alert_dialog_icon)
                .setTitle(title)
                .setView(newPatient)
                .setPositiveButton(R.string.alert_dialog_ok,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {
                                final EditText name = (EditText) newPatient
                                        .findViewById(R.id.name_edit);
                                final EditText ssn = (EditText) newPatient
                                        .findViewById(R.id.ssn_edit);
                                final EditText accnum = (EditText) newPatient
                                        .findViewById(R.id.accnr_edit);

                                final Patient patient = new Patient(ssn.getText()
                                        .toString(), name.getText().toString(),
                                        new Study(SimpleDateFormat
                                                .getDateInstance().toString(),
                                                accnum.getText().toString()));

                                addPatientAction(patient); 

                                DatabaseHelper db = new DatabaseHelper(
                                        getActivity());
                                db.addPatientStudy(patient);

                                /*DataManager dm = new DataManager(getActivity()
                                        .getBaseContext());
                                dm.addPatient(patient);*/

                                /*
                                 * PatientManager pm = new
                                 * PatientManager(getActivity
                                 * ().getBaseContext()); pm.add(patient);
                                 */

                                // Interfacecallback
                                /*
                                 * OnNewPatientlistener activity =
                                 * (OnNewPatientlistener) getActivity();
                                 * activity.OnNewPatient(patient);
                                 */

                            }
                        })
                .setNegativeButton(R.string.alert_dialog_cancel,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {
                                dismiss();
                            }
                        }).create();

    }
}

Can anyone explain why this happens and how to circumvent this?

5
  • try making the arraylist synchronized. Commented Oct 30, 2012 at 9:49
  • I get the error: Illegal modifier for the field Patients; only public, protected, private, static, final, transient & volatile are permitted Commented Oct 30, 2012 at 9:51
  • stackoverflow.com/questions/1431681/… see this Commented Oct 30, 2012 at 9:53
  • Not sure how this explains my problem, could you elaborate. Commented Oct 30, 2012 at 10:34
  • Found the solution. Had to add: 'runOnUiThread(new Runnable() { public void run() { GFRApplication.dPatients.add(newPatient); } });' Commented Oct 30, 2012 at 10:42

1 Answer 1

0

Adding...

runOnUiThread(new Runnable() {
              public void run() {
                  GFRApplication.dPatients.add(newPatient);
              }
            });

...did the trick.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.