0

I have a list which rows are created dynamically. Each row contains a EditText and when the user clicks on this view I want a TimePickerDialog to show up. I'm using the same technique somewhere else in my code, but not in a ListView, and it works perfectly, but somehow at this point I always get

ERROR   AndroidRuntime      java.lang.IllegalStateException: Fragment already added: TimePickerDialogFragment{42f06710 #0 0c005225-d6a4-47f9-b3ee-ac90a70d3962}
ERROR   AndroidRuntime      at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1175)
ERROR   AndroidRuntime      at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:616)

My ListAdapter is:

private class SetAdapter extends BaseAdapter {

        private LayoutInflater inflater = null;        

        public SetAdapter() {
            inflater = LayoutInflater.from(TrainingDetailActivity.this);
        }

        public View getView(final int position, View convertView, ViewGroup parent) {
            convertView = inflater.inflate(R.layout.training_detail_row, parent, false);
            EditText col1 = (EditText) convertView.findViewById(R.id.trainingDetailCol1);
            setTimePickerTo(col1, exercise.getSets().get(position), UUID.randomUUID().toString());
        }
}

and

private void setTimePickerTo(EditText editText, final Set set, final String tag) {
        int hours = exercise.getDuration() / 60;
        int minutes = exercise.getDuration() % 60;
        editText.setText(String.format("%02d:%02d", hours, minutes));
        TimePickerDialog.OnTimeSetListener timePickerDurationListener
            = new TimePickerDialog.OnTimeSetListener() {

                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    int val = hourOfDay * 60 + minute;
                    exercise.setDuration(val);
                    notifyDataSetChanged();
                }
            };
        final TimePickerDialogFragment timePickerDuration
                = new TimePickerDialogFragment(exercise, timePickerDurationListener);
        editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    timePickerDuration.show(getSupportFragmentManager(), tag);
                }
                else {

                }
            }
        });

    }

So as the TimePickerDialogFragment works somewhere else I guess the problem comes from the ListView. If I add a

timePickerDuration.dismiss()

to the else-case on onFocusChange(), the dialog shows and dismisses several times and logcat gets spammed with

WARN    IInputConnectionWrapper getExtractedText on inactive InputConnection
WARN    IInputConnectionWrapper getTextBeforeCursor on inactive InputConnection
WARN    IInputConnectionWrapper getExtractedText on inactive InputConnection

Any ideas what I can do?

Syex
  • 1,320
  • 1
  • 12
  • 23

1 Answers1

0

Try this: In the adapter class, in getView(), put an if (convertView == null) condition surrounding the convertView = inflater.inflate(R.layout.training_detail_row, parent, false);

Gak2
  • 2,661
  • 1
  • 16
  • 28
  • Try putting `final TimePickerDialogFragment timePickerDuration = new TimePickerDialogFragment(exercise, timePickerDurationListener);` in the onFocusChange() method. Right now you are creating a fragment for each row unnecessarily when it should only be created when you need it. – Gak2 Mar 08 '14 at 00:17
  • That seems to help, although I don't get what's the big difference. However, the dialog shows up twice, now, what I fixed by simply using an OnTouchListener, so thanks for your help. – Syex Mar 08 '14 at 11:15