Edittext在自定义适配器

时间:2016-11-23 08:49:43

标签: android android-recyclerview android-adapter

我是初学者,我尝试将我的edittext保存在recyclerview中,但它会消失。

我想保存它以便将其发送到文件中。

我从这个论坛尝试了一些代码,但我没有成功。

这是我的代码:

TaskAdapter

    public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.MyViewHolder> {


    private List<Task> TaskList;



    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView title;
        public CheckBox checkBox;
        public EditText finalComment;

        public MyViewHolder(View view) {
            super(view);
            title = (TextView) view.findViewById(R.id.title);
            checkBox = (CheckBox) view.findViewById(R.id.checkBox);
            finalComment = (EditText) view.findViewById(R.id.FinalComment);
        }
    }



    public TaskAdapter(List<Task> TaskList) {

        this.TaskList = TaskList;
    }



    @Override
    public TaskAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.recyvlerview, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {



        Task task = TaskList.get(position);
        holder.title.setText(task.getTitle());
        holder.finalComment.setText(task.getComment());
        holder.checkBox.setOnCheckedChangeListener(null);
        holder.checkBox.setChecked(task.isState());

        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                task.setState(b);
            }
        });


        holder.finalComment.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
               task.setComment(editable.toString());
            }
        });
    }

    public Task getItem(int position){
        return TaskList.get(position);
    }

    @Override
    public int getItemCount() {
        return TaskList.size();
    }    
 }

我的回收者视图:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear_tasks"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="visible"
    android:weightSum="1">


    <TextView
        android:id="@+id/title"
        android:textColor="@color/black"
        android:textSize="16dp"
        android:textStyle="bold"
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_toLeftOf="@+id/checkBox"
        android:layout_toStartOf="@+id/checkBox" />


    <EditText
        android:id="@+id/FinalComment"
        android:hint="Commentaire..."
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/checkBox"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
         />


</RelativeLayout>

我的班级任务:

public class Task {

    String title;
    boolean state;
    String comment;
    int period;
    String type;

    public Task(String title, boolean state, String comment, String type, int period) {
        this.title = title;
        this.state = state;
        this.comment = comment;
        this.type = type;
        this.period = period;
    }

    @Override
    public String toString() {
        return "Title : "+ title +
                "\nState : " + state +
                "\nComment : " + comment +
                "\nPeriod : " + period +
                "\nType : " + type;
    }

    public String getTitle() {
        return title;
    }

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

    public void setState(boolean state) {
        this.state = state;
    }

    public boolean isState() {
        return state;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getPeriod() { return period; }

    public void setPeriod(int period) { this.period = period; }
}

非常感谢

1 个答案:

答案 0 :(得分:0)

好的,我自己更换了适配器litke:

public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.MyViewHolder> {


    private List<Task> TaskList;


    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView title;
        public CheckBox checkBox;
        public EditText finalComment;

        public MyViewHolder(View view) {
            super(view);
            title = (TextView) view.findViewById(R.id.title);
            checkBox = (CheckBox) view.findViewById(R.id.checkBox);
            finalComment = (EditText) view.findViewById(R.id.FinalComment);
            TextWatcher textWatcher = new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    int position = (int) finalComment.getTag();
                    TaskList.get(position).setComment(charSequence.toString());
                }

                @Override
                public void afterTextChanged(Editable editable) {

                }
            };
            finalComment.addTextChangedListener(textWatcher);
        }
    }


    public TaskAdapter(List<Task> TaskList) {

        this.TaskList = TaskList;
    }



    @Override
    public TaskAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.recyvlerview, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {



        Task task = TaskList.get(position);
        holder.title.setText(task.getTitle());
        holder.finalComment.setTag(position);
        holder.checkBox.setOnCheckedChangeListener(null);
        holder.checkBox.setChecked(task.isState());

        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                task.setState(b);
            }
        });

    }


    public Task getItem(int position){
        return TaskList.get(position);
    }

    @Override
    public int getItemCount() {
        return TaskList.size();
    }


}

无论如何,谢谢你。)