我正在使用 sharedpreferences 将值发送到数据中,一切正常,当我从一个类(ListView)中获取的一个共享首选项接收值时,但是当我想从两个从不同类中获取的共享首选项接收值时,我遇到了问题,因为详细,这是我的代码
login.java 中的共享首选项:
SharedPreferences values = PreferenceManager.getDefaultSharedPreferences(LoginActivity.this); SharedPreferences.Editor editor = values.edit(); editor.putString("etUser", strUser); editor.putString("key",KEY); editor.commit();
在 EpolicyListPolis.java 中,我从登录类接收值并发送新值:
SharedPreferences text = PreferenceManager.getDefaultSharedPreferences(EpolicyListPolis.this);
SharedPreferences.Editor editor = text.edit();
editor.putString("POLIS", POLIS);
editor.commit();
在 PPViewerEpolicy.java 中,当我想从登录类和 EpolicyListPolis 接收值时,我遇到了问题,因为我收到的值什么都没有,这就是我接收它的方式:
SharedPreferences values = PreferenceManager.getDefaultSharedPreferences(this);
strUser = values.getString("etUser", "null");
KEY = values.getString("key", "null");
SharedPreferences text = PreferenceManager.getDefaultSharedPreferences(this);
POLIS= text.getString("POLIS", "null");
我从两个 sharedPreferences 接收价值的方法是错误的..?如果是,我希望有人能告诉我我的错在哪里。
在这里更新我将我的代码包含在 EpolicyListPolis.java 中
public class EpolicyListPolis extends ListActivity {
static String PEMEGANG="PEMEGANG";
static String POLIS="POLIS";
static String STATUS="STATUS";
static String TERTANGGUNG="TERTANGGUNG";
String KEY, strUser;
List NasabahList= new ArrayList();
private ProgressDialog Dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(R.string.Nomor_Polis);
setContentView(R.layout.epolicy_list_polis);
SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
strUser= settings.getString("etUser", null);
KEY = settings.getString("key", null);
new NasabahAsyncTask().execute();
}
public class NasabahAsyncTask extends AsyncTask<Void, Void, String>{
String url = ("http://www.example.com"+strUser+"&k="+KEY);
public NasabahAsyncTask() {
this.url=url;
// TODO Auto-generated constructor stub
}
protected void onPreExecute() {
super.onPreExecute();
Dialog = ProgressDialog.show(EpolicyListPolis.this, "", "Loading");
}
@Override
protected String doInBackground(Void... params) {
String result="";
try {
result=Connection.get(url);
}catch (Exception e){
result=" ";
Log.d("test viewer",e.getMessage());
}
return result;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
fetchResponse(result.replace("\n", "").trim());
ListAdapter adapter = new SimpleAdapter(
EpolicyListPolis.this, NasabahList, R.layout.epolicy_polis_listitem,
new String[] {POLIS, PEMEGANG, TERTANGGUNG, STATUS}, new int [] {R.id.polis, R.id.pemegang, R.id.tertanggung, R.id.status});
setListAdapter(adapter);
Dialog.dismiss();
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("POLIS", POLIS);
editor.commit();
Intent in = new Intent(EpolicyListPolis.this, KategoriNomorPolis.class);
startActivity(in);
}
});}
}
private void fetchResponse(String result) {
// TODO Auto-generated method stub
if (!result.equals("")) {
try {
JSONArray jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
HashMap<String, String> map = new HashMap<String, String>();
if (jsonObject.has("PEMEGANG"))
map.put("PEMEGANG", jsonObject.get("PEMEGANG").toString());
if (jsonObject.has("POLIS"))
map.put("POLIS", jsonObject.get("POLIS").toString());
if (jsonObject.has("STATUS"))
map.put("STATUS", jsonObject.get("STATUS").toString());
if (jsonObject.has("TERTANGGUNG"))
map.put("TERTANGGUNG", jsonObject.get("TERTANGGUNG").toString());
NasabahList.add(map);
System.out.println("json oke");
}
}catch (JSONException e) {
e.printStackTrace();
}
}
}
这是 PPViewerEpolicy.java:
public class PPViewerEpolicy extends ListActivity {
String strUser,KEY,POLIS;
static final String[] PP= new String[] {
"Nama Lengkap",
"gelar",
"Warga Negara",
"Usia",
"Status",
"Jenis Kelamin",
"Agama"};
private ProgressDialog dialog;
private ArrayList<PPVariabel> listPP;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
strUser = settings.getString("etUser", null);
KEY= settings.getString("key", null);
POLIS= settings.getString("POLIS", null);
super.onCreate(savedInstanceState);
setContentView(R.layout.pplist);
listPP= new ArrayList<PPVariabel>();
new PemegangPolis().execute();
}
public class PemegangPolis extends AsyncTask<Void, Void, String>{
String url="http://www.example.com"+strUser+"&k="+KEY+"&p"+POLIS;
public PemegangPolis (){
this.url=url;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog = ProgressDialog.show(PPViewerEpolicy.this, "", "Please wait");
}
@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
String result = "";
try {
result = Connection.get(url);
} catch (Exception e) {
// TODO: handle exception
result = "";
}
return result;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
fetchResponse(result.replace("\n", "").trim());
dialog.dismiss();
}
}
private void fetchResponse(String result) {
// TODO Auto-generated method stub
if (!result.equals("")) {
try {
JSONObject jsonObject = new JSONObject(result);
PPVariabel varpp = null;
JSONObject pp = jsonObject.getJSONObject("pp");
for (int i=0; i<pp.length();i++){
varpp= new PPVariabel(pp.optString("nama_pmg"),
pp.optString("pendidikan_pmg"),
pp.optString("warga_pmg"),
pp.optString("usia_pmg"),
pp.optString("status_marital_pmg"),
pp.optString("kelamin_pmg"),
pp.optString("agama_pmg"),
pp.optString("tujuan_pmg"),
pp.optString("tujuana_pmg"),
pp.optString("penghasilan_pmg"),
pp.optString("pendanaan_pmg"),
pp.optString("pendanaana_pmg"),
pp.optString("bidang_industria_pmg"),
pp.optString("hubungan_pmg_ttg"));
listPP.add(varpp);
System.out.println("json parser done cynnn");
list=(ListView)findViewById(android.R.id.list);
setListAdapter(new PPViewerAdapter(this, listPP,PP));
}
}
catch (Exception e) {
Log.d("TEST CHART", e.getMessage());
}
我还是不明白,为什么当我想运行 PPViewerEpolicy 时,没有从 url 加载的数据,我在结构代码或其他任何内容上错了..?