我正在尝试开发我的第一个应用程序,但我需要一些帮助。因此,要访问我的应用程序,您必须使用自己的用户名和密码登录。我的应用程序左侧有一个滑动菜单,我使用 jfeinstein10 库。现在,我希望在此菜单的第一行中显示用户的用户名,这样您就知道这是一个动态字符串。
这是我行的 .xml 代码:
<item name="list_item_municipio_id" type="slidingmenu" />
<item name="list_item_municipio_label" type="slidingmenu">"Municipio"</item>
<item name="list_item_municipio_icon " type="slidingmenu">"@drawable/iconamunicipio"</item>
现在它显示为“municipio”,带有<item name="list_item_municipio_label"
. 我读到字符串是静态的,并且可能的解决方案是使用 SharedPreferences。你可以给我一个例子吗?因为我不明白如何将用户名存储到 SharedPreferences。这是 login.java 的代码:
package com.baruckis.SlidingMenuImplementation;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Login extends Activity implements OnClickListener{
public static final String PREFS_NAME = "LoginPrefs";
private EditText user, pass;
private Button mSubmit, mRegister;
private ProgressDialog pDialog;
// Attivo una connessione JSON
JSONParser jsonParser = new JSONParser();
//Collego un file php sul mio server
private static final String LOGIN_URL = "http://provasitosaronno.dnshosting.it/php/login.php";
//JSON gestisce le risposte fornite dallo script php
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
if (settings.getString("logged", "").toString().equals("logged")) {
Intent i = new Intent(Login.this, SlidingMenuFromClassActivity.class);
finish();
startActivity(i);
}
//Imposto le due variabili user e pass
user = (EditText)findViewById(R.id.username);
pass = (EditText)findViewById(R.id.password);
//Collego i due pulsanti del login e del register
mSubmit = (Button)findViewById(R.id.login);
mRegister = (Button)findViewById(R.id.register);
//Aggiungo la funzione del clicklistener
mSubmit.setOnClickListener(this);
mRegister.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.login:
new AttemptLogin().execute();
break;
case R.id.register:
Intent i = new Intent(this, Register.class);
startActivity(i);
break;
default:
break;
}
}
class AttemptLogin extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
boolean failure = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Login.this);
pDialog.setMessage("Accesso in corso...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
int success;
String username = user.getText().toString();
String password = pass.getText().toString();
try {
//Aggiungo i parametri generati dalle variabili
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
Log.d("request!", "starting");
// Ottengo i risultati dalla connessione http
JSONObject json = jsonParser.makeHttpRequest(
LOGIN_URL, "POST", params);
//Aspetto una risposta da JSON
Log.d("Login attempt", json.toString());
//Se il tutto avviene...
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("logged", "logged");
editor.commit();
Log.d("Login Successful!", json.toString());
Intent i = new Intent(Login.this, SlidingMenuFromClassActivity.class);
finish();
startActivity(i);
return json.getString(TAG_MESSAGE);
}else{
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
//funzione dismiss
pDialog.dismiss();
if (file_url != null){
Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
}
}
}
}