0

我有一个动态创建行的列表。每行都包含一个 EditText,当用户单击此视图时,我希望显示一个 TimePickerDialog。我在我的代码中的其他地方使用了相同的技术,但不是在 ListView 中,它工作得很好,但不知何故,在这一点上我总是得到

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)

我的 ListAdapter 是:

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

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 {

                }
            }
        });

    }

因此,当 TimePickerDialogFragment 在其他地方工作时,我猜问题出在 ListView 上。如果我添加一个

timePickerDuration.dismiss()

对于 onFocusChange() 上的 else-case,对话框会显示并关闭几次,并且 logcat 会收到垃圾邮件

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

有什么想法我能做什么?

4

1 回答 1

0

试试这个:在适配器类中,在 getView() 中,放置一个if (convertView == null)围绕 convertView = inflater.inflate(R.layout.training_detail_row, parent, false);

于 2014-03-07T23:13:59.777 回答