自定义BaseAdapter执行错误更新

时间:2016-06-17 15:06:16

标签: android listview android-edittext

我已实施自定义BaseAdapter以显示TodoTodo包含不同的属性。现在的问题是,当我向其添加新Todo并向下滚动(以便无法再看到ListView条目时)现有Todo的文本将重置为测试

感谢您的任何帮助,谢谢!

自定义BaseAdapter

public class TodosArrayAdapter extends BaseAdapter {
    private List<Todo> todos;
    private LayoutInflater inflater;

    public TodosArrayAdapter(List<Todo> todos, LayoutInflater inflater) {
        if (todos == null || inflater == null) {
            throw new IllegalArgumentException();
        }

        this.todos = todos;
        this.inflater = inflater;
    }

    public void addTodo(Todo todo) {
        this.todos.add(todo);

        notifyDataSetChanged();
    }

    public void removeTodo(Todo todo) {
        this.todos.remove(todo);

        notifyDataSetChanged();
    }

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

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

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final TodoHolder holder;
        final Todo todo = (Todo) getItem(position);
            if (convertView == null) {
                holder = new TodoHolder();
                holder.ref = todo;

                convertView = inflater.inflate(R.layout.todolistitem_layout, null);
                holder.tvTitle = (EditText) convertView.findViewById(R.id.tvTitle);
                holder.tvTitle.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }

                @Override
                public void afterTextChanged(Editable s) {
                    holder.ref.setTitle(s.toString());
                }
            });
                convertView.setTag(holder);
            } else {
                holder = (TodoHolder) convertView.getTag();
            }

            holder.tvTitle.setText(todo.getTitle());

        return convertView;
    }

    static class TodoHolder {
        Todo ref;
        EditText tvTitle;
    }
}

Todo

public class Todo {

    private String title;
    private String description;
    private Date date;

    public Todo(String title, String description, Date date) {
        this.title = title;
        this.description = description;
        this.date = date;
    }

    public String getTitle() {
        return title;
    }

    public String getDescription() {
        return description;
    }

    public Date getDate() {
        return date;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

初始化适配器:

 ArrayList<Todo> todos = new ArrayList<>();
 todos.add(new Todo("first", "first description", new Date()));
 todos.add(new Todo("second", "second description", new Date()));
 todos.add(new Todo("third", "third description", new Date()));

 todosAdapter = new TodosArrayAdapter(todos, this.getLayoutInflater());
 listView = (ListView) findViewById(R.id.todos);
 listView.setAdapter(todosAdapter);

从按钮添加新Todo

todosAdapter.addTodo(new Todo("test", "test description", new Date()));

0 个答案:

没有答案