Question

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?

Was it helpful?

Solution

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);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top