ListView 动态添加 Item 后列表变得全都一样


我想在 ListView 里面呈现用户选择的日期与时间,但是碰到了问题,就是添加一个 Item 后整个列表都变成了刚刚选择的日期时间。界面是这样的:底部一个 Button 按钮,点击之后弹出时间选择器,选择之后添加到 ListView 里面。但是如最后一张图里面,选择之后整个列表都会变成选择的时间。不知道问题出在哪里,望高手指导。

图片描述

下面是代码,我先定义了一个数据封装类,用于封装获取到的时间与日期:


 public class Data {

    private String time;
    private String date;

    public Data(String time, String date) {
        this.time = time;
        this.date = date;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }
}

然后自定义了 Adapter 继承自 BaseAdapter,代码如下:


 public class MyAdapter extends BaseAdapter {

    private List<Data> list = new ArrayList<>();
    private Context context;

    public MyAdapter(Context context, List<Data> list) {
        this.context = context;
        this.list = list;
    }

    public void addItem(Data data) {
        list.add(data);
        notifyDataSetChanged();
    }

    public void removeItem(int position) {
        list.remove(position);
        notifyDataSetChanged();
    }

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

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.time.setText(list.get(position).getTime());
        viewHolder.date.setText(list.get(position).getDate());
        return convertView;
    }


    public class ViewHolder {

        private TextView time;
        private TextView date;

        public ViewHolder(View view) {
            time = (TextView) view.findViewById(R.id.time);
            date = (TextView) view.findViewById(R.id.date);
        }

    }
}

最后在 MainActivity 中适配 ListView,暂时只提供选择了日期没有选择时间,并且自定义了 DatePickerDialog 去掉了其中 onStop 方法中的 super(),因为不去掉的话 setDateSet 会执行两遍。代码如下:


 public class MainActivity extends AppCompatActivity {

    List<Data> list = new ArrayList<Data>();
    MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //init listview
        Button add = (Button) findViewById(R.id.add);
        ListView listView = (ListView) findViewById(R.id.list_view);
        adapter = new MyAdapter(this, list);
        listView.setAdapter(adapter);

        add.setOnClickListener(new View.OnClickListener() {
            Data data = new Data(0 + "", 0 + "");
            @Override
            public void onClick(View v) {
                Calender calener = Calender.getInstance();
                new MyDatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        data.setDate(year + "Year " + monthOfYear + "Month " + dayOfMonth + "Day");
                        data.setTime("This is time");
                        list.add(data);
                        adapter.notifyDataSetChanged();
                    }
                }, calener.get(Calendar.YEAR), calener.get(Calendar.MONTH), calener.get(Calendar.DAY_OF_MONTH)).show();
            }
        });
    }

    public class MyDatePickerDialog extends DatePickerDialog {

        public MyDatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {
            super(context, callBack, year, monthOfYear, dayOfMonth);
        }

        @Override
        protected void onStop() {
        }
    }
}

Android listview

Plics♂C 9 years, 5 months ago

Data data = new Data(0 + "", 0 + "");
写错了位置,应该写到 onClick 方法里面,不然一直是一个对象。

这是正确答案,我在 eoe 论坛也提问了,有大神指出了错误。链接: 链接描述

3rd-eye answered 9 years, 5 months ago

Your Answer