SharedPreference giving me unknown LOG

Viewed 102

I want to know what the log that shows means when the Buttom Navigation Bar is clicked, The text information inside this item is stored in the prefrence.

this is the code for navigation item when clicked :

case R.id.navigation_gift:
    View view = View.inflate(getApplicationContext(), R.layout.section_fragment_69, null);
    TextView txtEmtiaz = (TextView) view.findViewById(R.id.txtEmtiaz);
    SharedPreferences sp = getSharedPreferences("entire", 0);
    txtEmtiaz.setText(sp.getString("tik", ""));
    Log.e("POPI", "txt in main2 :" + String.valueOf(txtEmtiaz));

This is the Log information that is displayed:

/POPI: txt in main2 :android.widget.TextView{1e42d479 V.ED.... ......ID 0,0-0,0 #7f09016d app:id/txtEmtiaz}
3 Answers

You need to get text from txtEmtiaz and then set it to sp shared preference.

Problem is at below line:

txtEmtiaz.setText();

Change to:

sp.putString("tik", txtEmtiaz.getText());

Problem in log:

  Log.e("POPI", "txt in main2 :" + String.valueOf(txtEmtiaz));

You are trying to print txtEmtiaz i.e. txtEmtiaz object which of course you can not.

You should use like below to print value (text) of txtEmtiaz TextView:

  Log.e("POPI", "txt in main2 :" + txtEmtiaz.getText());

Try this

You are using SharedPreference with no name.

Try this SharedPreference with name:

Setting values in Preference

SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
 editor.putString("name", "HD");

 editor.apply();

Retrieve data from preference:

    SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
    String restoredText = prefs.getString("text", null);
    if (restoredText != null) {
      String name = prefs.getString("name", "HD");
     txtEmtiaz.setText(name);

    }

You're trying to print a TextView. You want to print the content of the TextView instead, use:

txtEmtiaz.getText().toString()

Related