How to Save Two Different Texts under one editText with different Positions


So basically I am a beginner towards coding in Android Studio, and so far I am making an app
that uses a Recycler View that would display different images under the main_activity that when click would direct you to their respective details activity. So in the details activity I put an editable text that would allow the user to type their thoughts regarding the show displayed in the picture then they can save the text they inputted via a button, and when they close the app they can just use the retrieve button to retrieve what they’ve typed, I used sharedPreferences for that. I also used an Intent and Bundle so that the app knows what position it’s supposed direct you.

So my dilemma is that the editable text found on the details activity depends on what picture was selected on the main_activity. Therefore it shouldn’t be the same, what was type and save on show A must not be carried unto show B. Any suggestions is highly appreciated.

This is the code of my main_activity

RecyclerView rv;
RecyclerAdapter ra;
Intent i1;
Bundle b1;

protected void onCreate(Bundle savedInstanceState) {

    //code below for recyclerView and setOnclickListener
    b1 = new Bundle();

    rv = findViewById(;
    rv.setLayoutManager(new GridLayoutManager(this, 2));
    ra = new RecyclerAdapter(this);
    ra.setOnClickListener(new AnimeClickListener() {
        public void OnClick(View view, int position) {
            i1 = new Intent(getApplicationContext(),anime_details.class);
            b1.putInt("position", position);
            startActivityForResult(i1, 1);

And this is the code of my details_activity

Intent i2;
Bundle b2;

ImageView poster;
TextView animeName;
int position = 0;

EditText contentText;
SharedPreferences sp;
SharedPreferences.Editor spe;

protected void onCreate(Bundle savedInstanceState) {
    b2 = new Bundle();

    i2 = getIntent();
    b2 = i2.getExtras();
    position = b2.getInt("position",0);

    animeName = findViewById(;
    poster = findViewById(;
    contentText = findViewById(;


    sp = getSharedPreferences("myPref", MODE_PRIVATE);

public void saveText(View v){
    spe = sp.edit();
    spe.putString("content", contentText.getEditableText().toString());

public void retrieveText(View v){

Here are is what the app looks like for reference


Try using these lines instead:

spe.putString("content" + position, contentText.getEditableText().toString());


contentText.setText(sp.getString("content" + position,""));

That way, you store a different pref depending on which position was clicked.

Note that the ideal solution involves using a database. I’m only showing you this way because you’re a beginner.

Answered By – Gavin Wright

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published