Here's the code where I get to have the SharedPreferences
:
public class MainActivity2 extends Activity {
Button b;
EditText et;
TextView tv;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
ProgressDialog dialog = null;
String rfid, getPref;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
b = (Button)findViewById(R.id.loginnext);
et = (EditText)findViewById(R.id.rfid);
tv = (TextView)findViewById(R.id.tv);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog = ProgressDialog.show(MainActivity2.this, "",
"Validating user...", true);
new Thread(new Runnable() {
public void run() {
login();
}
}).start();
}
});
}
void login(){
try{
SharedPreferences preferences = getSharedPreferences(getPref, Context.MODE_PRIVATE);
final SharedPreferences.Editor editor = preferences.edit();
httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://usamobileapp.pe.hu/webservice/check.php");
nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username",et.getText().toString().trim()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response=httpclient.execute(httppost);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);
System.out.println("Response : " + response);
runOnUiThread(new Runnable() {
public void run() {
tv.setText("Response from PHP : " + response);
dialog.dismiss();
}
});
if(response.equalsIgnoreCase("User Found")){
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MainActivity2.this,"Login Success", Toast.LENGTH_SHORT).show();
editor.putString("rfid" ,rfid);
editor.commit();
}
});
startActivity(new Intent(MainActivity2.this, MainActivity3Activity.class));
}else{
showAlert();
}
}catch(Exception e){
dialog.dismiss();
System.out.println("Exception : " + e.getMessage());
}
}
This is where I'd like the get the Registration ID entered by the user and use it in all activities of the app.
Here is the code to check whether I get to have the SharedPreferences
correctly. It displays it through I text view. I wonder why it would not show up. Did I do the SharePreferences
correctly?
public class ViewGradesActivity extends ActionBarActivity {
TextView viewPref;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_grades);
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
String get = settings.getString("rfid", "");
viewPref = (TextView) findViewById(R.id.tv);
viewPref.setText(get);
}
}
By the way, the code above is from another class.
Thanks for the help in advance.