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:
But then when I click to go back and then single click on the subject name again, nothing is being shown!
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:
EDIT-2: I changed my code in onClickListener function and it now looks like this:
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: