将项添加到ListView

时间:2014-08-26 17:11:10

标签: android listview android-listview android-adapter

我正在尝试通过点击按钮将项目添加到我的列表中。每个列表项都包含一个'TextView',它应该被设置为从EditText获得的值。第一项正确添加但是,当我向列表中添加另一项时,前一个列表项的TextView被设置为新值。因此,结果是有两个(或多个你添加)相同的列表项。

我可以看到为什么会在我的代码中发生这种情况,但我不知道如何更改它以使其正常工作。

我正在使用ListView的自定义适配器,称为“TimetableListAdapter”。要添加到每个列表项的数据来自“DialogFragment”(此处未显示,因为数据传递/传输工作正常)

    public class ScreenSlidePagerFragment extends Fragment implements View.OnClickListener, AddLessonDialog.onSubmitListener {

        ViewGroup rootView;
        Button btn_addLesson;
        ListView lv_viewDay;
        TimetableListAdapter la_listAdapt;
        String[] lessonData;

        public ScreenSlidePagerFragment() {
            // Required empty public constructor
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            rootView = (ViewGroup) inflater.inflate(
                    R.layout.fragment_timetable_page, container, false);

            btn_addLesson = (Button) rootView.findViewById(R.id.btn_addLesson);
            btn_addLesson.setOnClickListener(this);

            lv_viewDay = (ListView) rootView.findViewById(R.id.lv_viewDay);
            la_listAdapt = new TimetableListAdapter(getActivity());
            lv_viewDay.setAdapter(la_listAdapt);

            return rootView;
        }


        @Override
        public void onClick(View v) {

            switch (v.getId()) {

                case R.id.btn_addLesson:

                  // Shows DialogFragment where data is input 

                    break;
            }
        }

        public class TimetableListAdapter extends BaseAdapter {

            private ArrayList<String[]> mDayList;

            Context mContext;

            public TimetableListAdapter(Context context) {

                mContext = context;
                mDayList = new ArrayList<String[]>();

            }


            @Override
            public int getCount() {
                return mDayList.size();
            }

            @Override
            public Object getItem(int position) {

                return mDayList.get(position);
            }

            @Override
            public long getItemId(int position) {

                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {

                if (null == convertView) {

                    convertView = LayoutInflater.from(mContext).inflate(
                            R.layout.lesson_item, null);

                }

                    TextView tv_subjectField = (TextView) convertView.findViewById(R.id.tv_subjectField);
                    tv_subjectField.setText(lessonData[0]);

                    return convertView;
                }

            public void addLesson(int position, String subject, String teacher, String room, String period) {

                lessonData = new String[4]; 
                lessonData[0] = subject;

                mDayList.add(position, lessonData);
                notifyDataSetChanged();
            }

        }


        @Override
        public void setOnSubmitListener(View v, String subject, String teacher, String room, String period, int position) {

            switch (v.getId()) {


                case R.id.btn_setLesson: // This is a button on the DialogFragment

// The data is passed through from the DialogFragment fine
                    la_listAdapt.addLesson(position, subject, teacher, room, period);

                    break;

            }
            }

        }

从我的代码中可以看出,我正在尝试将列表项中的数据设置为从DialogFragment传递的数据,该数据存储在String[] addLesson()处。

1 个答案:

答案 0 :(得分:0)

在适配器的getView方法中,替换:

tv_subjectField.setText(lessonData[0]);

使用:

tv_subjectField.setText(mDayList.get(position));