0

I am using shared preferences after someone on this site told me to use it for storing a value of a textfield. However, after I implemented it, I am still not able to store data in an EditText field.

My main java activity(relevant bit of code) is:

package com.example.fahadsaleem.xyz;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.content.SharedPreferences;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {
    public String str = " ";
    public static final String sub_name = "Subject Key: ";




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



    TextView calc_monday = (TextView) findViewById(R.id.monday_calc);


final SharedPreferences sharedPreferences = getSharedPreferences("AllData", Context.MODE_PRIVATE);





    calc_monday.setOnClickListener(
            new Button.OnClickListener(){
                public void onClick(View v){

                    CustomDialogClass cdd = new CustomDialogClass(MainActivity.this);
                    cdd.show();
                    TextView text1 = (TextView) cdd.findViewById(R.id.Subject_ID);
                    text1.setText(str);
                    TextView text2 = (TextView) cdd.findViewById(R.id.Room_ID);
                    text2.setText("6 (SEECS)");
                    TextView text3 = (TextView) cdd.findViewById(R.id.Time_ID);
                    text3.setText("09:00am  09:50am");
                }
            }
    );

    calc_monday.setOnLongClickListener(
            new Button.OnLongClickListener() {
                public boolean onLongClick(View v) {
                    kj monday_calc = new kj(MainActivity.this);
                    monday_calc.show();

                    EditText set_monday_calc = (EditText) monday_calc.findViewById(R.id.set_Subject_ID);
                    str = set_monday_calc.getText().toString();

                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString(sub_name, str);
                    editor.commit();

                    return true;
                }
            }

    );

Basically, my app has an interface of a timetable. The timetable contains names of various subjects at different times. When I single click (tap) on a subject name, a dialog box appears showing Subject Name, Time, And Room no. of the subject.

I want to be able to change these 3 things however I want. So I created another dialog which should appear on a long click. The text entered in this dialog box(which for now is just the subject name) should be stored in a string str and then this str should be displayed again on a single click.

But this code is not working.

When I enter calculus like this on a long click, the following screen is shown:

enter image description here

But then when I click to go back and then single click on the subject name again, nothing is being shown!

enter image description here

what's the issue? Am I implementing shared preferences in a wrong way?

EDIT: I added the code of storing as suggested by some users, but still the code is not working. Still calculus is not being stored on the field. When I single click, the EditText field is still blank. Here's my new mainactivity onLongClickListener code after editing:

enter image description here

EDIT-2: I changed my code in onClickListener function and it now looks like this:

enter image description here

but even now the result is same. Also, the following error is shown when I click go back after typing calculus on the dialog which sets the name:

enter image description here

Fahad Saleem
  • 413
  • 2
  • 13

4 Answers4

0

First, change sub_namevalue to "subjectKey". Use camel-case format.

Next, you're storing value:

                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString(sub_name, str);
                editor.commit();

but there's no code to retrieve a saved value.

Visit this page: How to use SharedPreferences in Android to store, fetch and edit values

EDIT: This part would be responsible for retrieving a saved data:

 String text = sharedPreferences.getString(sub_name, "");
        if(text != ""){                       //you need to check if value is empty 
            set_monday_calc.setText(text);   //otherwise, you may get NPE 
        }
        str = set_monday_calc.getText().toString(); //now you can use `str` variable 
                                                   //it has now saved value

Hope it help

Community
  • 1
  • 1
piotrek1543
  • 19,130
  • 7
  • 81
  • 94
  • On that link, there are many codes written to retrieve data. Which code is suitable for my application. Please keep in mind I want to show the data I store in the calc_monday.setOnClickListener(); – Fahad Saleem Dec 26 '15 at 15:18
  • still not working. see my original question, I added the edit at the end – Fahad Saleem Dec 26 '15 at 15:52
0

If you are storing the value in the SharedPreferences then why you are using the Global variable str. Just for a suggestion, in the View#OnClickListener get the value for key SUBJECT_ID and display it in TextView

Mohammed Rampurawala
  • 3,033
  • 2
  • 22
  • 32
0

You are only storing the data. You have to retrieve it and load the text into EditText like this,

String text = sharedPreferences.getString(sub_name, "");
if(text != ""){
    set_monday_calc.setText(text);
}

EDIT: If I understand correctly, you have to set the value of TextView with ID R.id.Subject_ID. Try to do it in onClickListener(),

calc_monday.setOnClickListener(
    new Button.OnClickListener(){
    public void onClick(View v){

        CustomDialogClass cdd = new CustomDialogClass(MainActivity.this);
        cdd.show();
        TextView text1 = (TextView) cdd.findViewById(R.id.Subject_ID);

        String text = sharedPreferences.getString(sub_name, "");
        if(text != ""){
            text1.setText(text);
        }

        TextView text2 = (TextView) cdd.findViewById(R.id.Room_ID);
        text2.setText("6 (SEECS)");
        TextView text3 = (TextView) cdd.findViewById(R.id.Time_ID);
        text3.setText("09:00am  09:50am");
        }
    }
);
Msp
  • 2,493
  • 2
  • 20
  • 34
0

You have to retrieve the data you stored by using the shared preferences:

String value = sharedPreferences.getString(sub_name, null);

Otherwise everything looks alright... except for the formatting of the code (use camel-case)

Andrew V
  • 522
  • 10
  • 24