Edittext数据显示在recyclelerview

时间:2017-01-06 15:30:53

标签: java android android-edittext android-recyclerview adapter

我是一个新手编码巫婆android工作室,我尝试将文件保存在recyclerview内的editText上,但数据在另一个editText中消失或重复。

有人能帮帮我吗?

这是我的代码:

TaskActivity.java

    package com.example.davidsimon.appexploit.activities;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.opengl.EGLDisplay;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.davidsimon.appexploit.R;
import com.example.davidsimon.appexploit.classes.ReturnResponse;
import com.example.davidsimon.appexploit.classes.Task;
import com.example.davidsimon.appexploit.classes.TaskAdapter;
import com.example.davidsimon.appexploit.classes.TaskToSend;
import com.example.davidsimon.appexploit.classes.User;
import com.example.davidsimon.appexploit.services.JsonService;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static android.R.attr.button;
import static android.R.attr.title;

public class TasksActivity extends AppCompatActivity {

    private List<Task> taskList = new ArrayList<>();
    private RecyclerView recyclerView;
    public TaskAdapter mAdapter;
    Button GoPhoto;
    String user;

    JsonService service;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_tasks);
        Button uploadButton = (Button) findViewById(R.id.butUpload);
        GoPhoto = (Button) findViewById(R.id.butt_photo);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(getString(R.string.base_url))
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        service = retrofit.create(JsonService.class);
        Button takePictureButton = (Button) findViewById(R.id.butt_photo);
        user = getIntent().getStringExtra("user");
        //imageView = (ImageView) findViewById(R.id.imageview);

        uploadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                TaskToSend task;
                List<TaskToSend> listTask = new ArrayList<>();
                for (int i = 0; i < mAdapter.getItemCount(); i++) {

                    task = new TaskToSend(mAdapter.getItem(i).getTitle(), String.valueOf(mAdapter.getItem(i).isState()), mAdapter.getItem(i).getComment(), user);

                    if (task.getState() == "true") {
                        task.setState("Fait");
                    } else {
                        task.setState("Non fait");
                    }

                    listTask.add(task);

                }

                service.saveTasks(listTask).enqueue(new Callback<ReturnResponse>() {

                    @Override
                    public void onResponse(Call<ReturnResponse> call, Response<ReturnResponse> response) {
                        Log.wtf("Fichier", "Réponse");
                    }

                    @Override
                    public void onFailure(Call<ReturnResponse> call, Throwable t) {
                        Log.wtf("Fichier", "Pas de réponse");
                    }
                });
                Intent TakstoUpload = new Intent(TasksActivity.this, UploadActivity.class);
                startActivity(TakstoUpload);
            }
        });

        takePictureButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, 0);
           }
        });




        service.getTasks().enqueue(new Callback<List<Task>>() {
            @Override
            public void onResponse(Call<List<Task>> call, Response<List<Task>> response) {

                List<Task> listeTaches = response.body();
                Log.wtf("Nombre de taches", "Nombre de taches : " + listeTaches.size());
                Calendar c = Calendar.getInstance();
                int DayOfWeek = c.get(Calendar.DAY_OF_WEEK);
                for (Task t : listeTaches) {
                     if (t.getPeriod()== DayOfWeek || t.getPeriod()== 0) {
                        taskList.add(t);
                    }
                }
                displayRecyclerView();
            }


            @Override
            public void onFailure(Call<List<Task>> call, Throwable t) {
                Log.wtf("GetTasks Failure", t.toString());
            }
        });


        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }


    public void displayRecyclerView() {
        recyclerView = (RecyclerView) findViewById(R.id.tasks_recycler_view);
        mAdapter = new TaskAdapter(taskList);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(mAdapter);

    }


    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    public Action getIndexApiAction() {
        Thing object = new Thing.Builder()
                .setName("Tasks Page") // TODO: Define a title for the content shown.
                // TODO: Make sure this auto-generated URL is correct.
                .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
                .build();
        return new Action.Builder(Action.TYPE_VIEW)
                .setObject(object)
                .setActionStatus(Action.STATUS_TYPE_COMPLETED)
                .build();
    }

    @Override
    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client.connect();
        AppIndex.AppIndexApi.start(client, getIndexApiAction());
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        AppIndex.AppIndexApi.end(client, getIndexApiAction());
        client.disconnect();
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            Bitmap mImageBitmap = (Bitmap) extras.get("data");

        }

    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}

这是我的适配器TaskAdapter:

package com.example.davidsimon.appexploit.classes;

import android.content.Context;
import android.graphics.Movie;
import android.support.v7.widget.ListViewCompat;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.example.davidsimon.appexploit.R;
import com.example.davidsimon.appexploit.activities.TasksActivity;

import java.util.ArrayList;
import java.util.List;

import static android.R.attr.data;
import static android.R.attr.value;
import static com.example.davidsimon.appexploit.R.id.checkBox;

/**
 * Created by David SIMON on 22/08/2016.
 */
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();
    }


}

我的Task.java

package com.example.davidsimon.appexploit.classes;

/**
 * Created by David SIMON on 05/08/2016.
 */
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; }
}

我只想在EditText&#34; finalcomment&#34;中保存数据。我已经搜索了一些类似的问题,但我认为我的水平太低了。

提前致谢

3 个答案:

答案 0 :(得分:0)

RecyclerView将重复使用您的视图。因此,无论何时滚动onBindViewHolder都会被调用。在该方法中,您需要使用正确的数据设置视图。您需要在onBindViewHolder中设置EditText,这可能需要您保存已输入的tex。以下是一个没有保存逻辑的方法的例子:

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



    Task task = TaskList.get(position);
    holder.title.setText(task.getTitle());
    holder.finalComment.setTag(position);

    // You need to save the entered text and then set it in onBindViewHolder
    holder.finalComment.setText(THE TEXT FOR THE EDIT TEXT);

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

}

答案 1 :(得分:0)

我认为您在回收视图时遇到问题。 您的RecyclerView重用从屏幕退出的其他对象的视图,以节省时间和内存。

所以你需要的是始终将所有需要的数据设置为onBindViewHolder方法。否则,如果视图从可见屏幕退出并重新使用,它将与旧数据一起重复使用。

将EditText用于RecyclerView不是一个好的决定,因为您将用户输入与演示数据混合在一起。 如果要保留此方法,则必须找到一种方法来保存与每个RecyclerView项目关联的数据,然后在它再次可见时始终能够将它们设置为正确的回收视图。

显然,您还需要保留空视图的信息。

希望我已经澄清了

答案 2 :(得分:0)

我看到了一个问题,如果我的解决方案不起作用,请告诉我! 在TaskAdapter课程内,onBindViewHolder()方法:

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

    Task task = TaskList.get(position);
    holder.title.setText(task.getTitle());
    //this one is wrong
    //holder.finalComment.setTag(position);
    //while doing this you will get the text of final comment in your 
    //edit text of final comment
    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);
        }
    });

}