Home > Back-end >  How to Save Two Different Texts under one editText with different Positions
How to Save Two Different Texts under one editText with different Positions

Time:06-11

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;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

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

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;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    b2 = new Bundle();
    setContentView(R.layout.activity_anime_details);

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

    animeName = findViewById(R.id.nameAnime);
    poster = findViewById(R.id.animePoster);
    contentText = findViewById(R.id.edit_textContents);

    poster.setImageDrawable(getResources().obtainTypedArray(R.array.atImages).getDrawable(position));
    animeName.setText(getResources().getStringArray(R.array.animeTitles)[position]);

    sp = getSharedPreferences("myPref", MODE_PRIVATE);
}

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

public void retrieveText(View v){
    contentText.setText(sp.getString("content",""));
}

Here are is what the app looks like for reference

CodePudding user response:

Try using these lines instead:

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

and

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.

  • Related