Issue
I have RecyclerView with multiple EditText. I want to save data that user provided to the EditText, store it and retrieve it after opening an app again.
I have an app where user tracks how many repetitions of certain exercise they did and obviously I want it to be saved somehow and to be retrieved later, but I don't know how to do this with RecyclerView. Usually I would just send certain EditText data to the database and then I would retrieve it, but in RecyclerView I don't know how to access that data as it is created dynamically(?) Sorry if I am wrong with something, but I am very new in Android programming.
Below is my code.
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));
}
Adapter code:
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);
}
}
}
Solution
It is not so hard to save data with SharedPreferences
. But, it does take a few lines of code. So, I'd prefer you to use my library. It is even faster to use. You can add it to your app using the README.md
file. Now, I can give you the entire adapter code with that functionality below:
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);
}
}
}
That does the init part for the db. But, because I don't find your edit text anywhere in the adapter, I have not added that part. But, the code for that edit text will be like this:
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());
}
});
But, dont forget to change the key in that place according your requirements.
Answered By - Sambhav Khandelwal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.