如何从 RecyclerView EditText 中保存数据

How to save data from RecyclerView EditText

我有带有多个 EditText 的 RecyclerView。我想保存用户提供给 EditText 的数据,存储它并在再次打开应用程序后检索它。

我有一个应用程序,用户可以在其中跟踪他们重复了多少次特定锻炼,显然我希望以某种方式保存它并在以后检索,但我不知道如何使用 RecyclerView 执行此操作。通常我只是将某些 EditText 数据发送到数据库,然后我会检索它,但是在 RecyclerView 中我不知道如何访问该数据,因为它是动态创建的(?)对不起,如果我有什么不对,但我是Android 编程非常新。

下面是我的代码。


public class exercises extends AppCompatActivity {

    String data1;
    RecyclerView recyclerView;
    String[] s1,s2,s3;
    int[] images = {R.drawable.squat,R.drawable.pull_up,R.drawable.handstand,R.drawable.leg_raises,
            R.drawable.push_up,R.drawable.dips,R.drawable.horizontal_pull,R.drawable.plank};
    ImageView goBackArrow;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exercises);
        Objects.requireNonNull(getSupportActionBar()).hide();
        goBackArrow = findViewById(R.id.go_back_arrow2);
        data1 = getIntent().getStringExtra("data1");

        Log.d("IntentToExercises", "onCreate: " + data1);


        recyclerView = findViewById(R.id.exercisesRecyclerView);

        if(data1.equals("SQUATS")) {
            s1 = getResources().getStringArray(R.array.LEVEL);
            s2 = getResources().getStringArray(R.array.SQUATS);
            s3 = getResources().getStringArray(R.array.SQUATS_VIDEOURL);
            ExercisesRecyclerView exercisesRecyclerView = new ExercisesRecyclerView(this,
                    s1,s2,s3);
            recyclerView.setAdapter(exercisesRecyclerView);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
        } else if(data1.equals("PULL UPS")){
            s1 = getResources().getStringArray(R.array.LEVEL);
            s2 = getResources().getStringArray(R.array.PULL_UPS);
            s3 = getResources().getStringArray(R.array.PULL_UPS_VIDEOURL);
            ExercisesRecyclerView exercisesRecyclerView = new ExercisesRecyclerView(this,
                    s1,s2,s3);
            recyclerView.setAdapter(exercisesRecyclerView);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
        }else if(data1.equals("HANDSTAND PUSH UPS")){
            s1 = getResources().getStringArray(R.array.LEVEL);
            s2 = getResources().getStringArray(R.array.HANDSTAND);
            s3 = getResources().getStringArray(R.array.HANDSTAND_VIDEOURL);
            ExercisesRecyclerView exercisesRecyclerView = new ExercisesRecyclerView(this,
                    s1,s2,s3);
            recyclerView.setAdapter(exercisesRecyclerView);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
        }else if(data1.equals("LEG RAISES")){
            s1 = getResources().getStringArray(R.array.LEVEL);
            s2 = getResources().getStringArray(R.array.LEGRAISES);
            s3 = getResources().getStringArray(R.array.LEGRAISES_VIDEOURL);
            ExercisesRecyclerView exercisesRecyclerView = new ExercisesRecyclerView(this,
                    s1,s2,s3);
            recyclerView.setAdapter(exercisesRecyclerView);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
        }else if(data1.equals("PUSH UPS")){
            s1 = getResources().getStringArray(R.array.LEVEL);
            s2 = getResources().getStringArray(R.array.PUSH_UPS);
            s3 = getResources().getStringArray(R.array.PUSH_UPS_VIDEOURL);
            ExercisesRecyclerView exercisesRecyclerView = new ExercisesRecyclerView(this,
                    s1,s2,s3);
            recyclerView.setAdapter(exercisesRecyclerView);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
        }else if(data1.equals("DIPS")){
            s1 = getResources().getStringArray(R.array.LEVEL);
            s2 = getResources().getStringArray(R.array.DIPS);
            s3 = getResources().getStringArray(R.array.DIPS_VIDEOURL);
            ExercisesRecyclerView exercisesRecyclerView = new ExercisesRecyclerView(this,
                    s1,s2,s3);
            recyclerView.setAdapter(exercisesRecyclerView);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
        }else if(data1.equals("HORIZONTAL PULLS")){
            s1 = getResources().getStringArray(R.array.LEVEL);
            s2 = getResources().getStringArray(R.array.HORIZONTAL_PULLS);
            s3 = getResources().getStringArray(R.array.HORIZONTAL_PULLS_VIDEOURL);
            ExercisesRecyclerView exercisesRecyclerView = new ExercisesRecyclerView(this,
                    s1,s2,s3);
            recyclerView.setAdapter(exercisesRecyclerView);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
        }


适配器代码:


public class ExercisesRecyclerView extends RecyclerView.Adapter<ExercisesRecyclerView.MyViewHolder>
{
    String[] data1, data2, videoURL;
    int[] images;
    Context context;

        public ExercisesRecyclerView(Context ct, String[] s1, String[] s2, String[] videoArray) {
            context = ct;
            data1 = s1;
            data2 = s2;
            videoURL = videoArray;
        }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.exercises_row, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.level.setText(data1[position]);
        holder.description.setText(data2[position]);
        holder.playVideoButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                YouTubePlayerView youTubePlayerView;
                AlertDialog.Builder dialogBuilder;
                AlertDialog dialog;
                dialogBuilder = new AlertDialog.Builder(context);
                LayoutInflater inflater = LayoutInflater.from(context);
                View view2 = inflater.inflate(R.layout.popup,null);
                youTubePlayerView = view2.findViewById(R.id.youtube_player_view);


                youTubePlayerView.addYouTubePlayerListener(new AbstractYouTubePlayerListener() {
                    @Override
                    public void onReady(@NonNull YouTubePlayer youTubePlayer) {
                        String videoID = videoURL[position];
                        youTubePlayer.loadVideo(videoID,0);
                        super.onReady(youTubePlayer);
                    }

                });

                dialogBuilder.setView(view2);
                dialog = dialogBuilder.create();
                dialog.show();

            }
        });

    }

    @Override
    public int getItemCount() {
        return data2.length;
    }




    public class MyViewHolder extends RecyclerView.ViewHolder{

            TextView level, description;
            ImageView playVideoButton;


        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            level = itemView.findViewById(R.id.exercisesLevelTextView);
            description = itemView.findViewById(R.id.exercisesNameTextView);
            playVideoButton = itemView.findViewById(R.id.playVideoButton);

        }
    }
}



如果要存储和检索数据,可以使用共享首选项、数据存储或房间数据库。使用 EditText,您需要添加 TextWatcher 以收集文本更改并保存。

使用SharedPreferences. But, it does take a few lines of code. So, I'd prefer you to use my library保存数据并不难。使用起来更快。您可以使用 README.md 文件将其添加到您的应用中。现在,我可以在下面为您提供具有该功能的完整适配器代码:

public class ExercisesRecyclerView extends RecyclerView.Adapter<ExercisesRecyclerView.MyViewHolder>
{
    String[] data1, data2, videoURL;
    int[] images;
    Context context;
    TinyDBManager tinyDB; // added this line

        public ExercisesRecyclerView(Context ct, String[] s1, String[] s2, String[] videoArray) {
            context = ct;
            data1 = s1;
            data2 = s2;
            videoURL = videoArray;
            tinyDB = TinyDB.getInstance(context); // added this line
        }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.exercises_row, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.level.setText(data1[position]);
        holder.description.setText(data2[position]);
        holder.playVideoButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                YouTubePlayerView youTubePlayerView;
                AlertDialog.Builder dialogBuilder;
                AlertDialog dialog;
                dialogBuilder = new AlertDialog.Builder(context);
                LayoutInflater inflater = LayoutInflater.from(context);
                View view2 = inflater.inflate(R.layout.popup,null);
                youTubePlayerView = view2.findViewById(R.id.youtube_player_view);


                youTubePlayerView.addYouTubePlayerListener(new AbstractYouTubePlayerListener() {
                    @Override
                    public void onReady(@NonNull YouTubePlayer youTubePlayer) {
                        String videoID = videoURL[position];
                        youTubePlayer.loadVideo(videoID,0);
                        super.onReady(youTubePlayer);
                    }

                });

                dialogBuilder.setView(view2);
                dialog = dialogBuilder.create();
                dialog.show();

            }
        });

    }

    @Override
    public int getItemCount() {
        return data2.length;
    }




    public class MyViewHolder extends RecyclerView.ViewHolder{

            TextView level, description;
            ImageView playVideoButton;


        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            level = itemView.findViewById(R.id.exercisesLevelTextView);
            description = itemView.findViewById(R.id.exercisesNameTextView);
            playVideoButton = itemView.findViewById(R.id.playVideoButton);

        }
    }
}

数据库的初始化部分。但是,因为我在适配器的任何地方都找不到您的编辑文本,所以我没有添加那部分。但是,该编辑文本的代码将是这样的:

edittext.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        // TODO Auto-generated method stub
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        // TODO Auto-generated method stub
    }

    @Override
    public void afterTextChanged(Editable s) {

        tinyDB.putString("value", s.toString());
    }
});

但是,不要忘记根据您的要求更改那个地方的密钥。