我正在尝试从SharedPreferences
获取ArrayList<HashMap<String, String>>
的数据。当我尝试在AsynTask
中获取数据时,它会给我正确的价值形式ArrayList<HashMap<String, String>>
,但当我尝试从同一个ArrayList<HashMap<String, String>>
获取价值时,它会给我不同的值。
在{ {1}} AsynTask
值为24和25,但在booking_id
方法中,它会给出Vendor_accept_info()
25和25的值。我不知道如何将24更改为25。有些人可以说出我在这里犯的错误。
booking id
Logcat输出:
package com.example.ease_my_life;
public class Clinet_notification extends Service {
private SharedPreferences user_info,sh_pref;
private String client_id,flag="b";
private Editor to_edit;
int size;
private String booking_id, name, mobile_no, photo,id_proof;
private HashMap<String, String> app_detail = new HashMap<String, String>();
private ArrayList<HashMap<String, String>> applist = new ArrayList<HashMap<String,String>>();
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
user_info();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "ping pong" , Toast.LENGTH_SHORT).show();
new Async_notification().execute();
return START_STICKY;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
private class Async_notification extends AsyncTask<String, String, String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
HttpURLConnection connection;
OutputStreamWriter request = null;
URL url = null;
String response = null;
String parameters = "client_id="+client_id;
System.out.println("PARAMETER Client notification.. "+parameters);
try
{
url = new URL(Params.URL+"send_client_pushNotification");
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestMethod("POST");
request = new OutputStreamWriter(connection.getOutputStream());
request.write(parameters);
request.flush();
request.close();
String line = "";
InputStreamReader isr = new InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
response = sb.toString();
System.out.println("RESPONSE push Notification.>>>..>>>>>> "+response);
isr.close();
reader.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return response;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// pDialog.dismiss();
System.out.println("Result====>"+result);
try{
JSONObject obj = new JSONObject(result);
String errCode = obj.getString("errCode");
String response = obj.getString("response");
System.out.println("Response in background--->"+response);
if(errCode.equalsIgnoreCase("-1")){
flag = "a";
String listing = obj.getString("listing");
JSONArray jArry = new JSONArray(listing);
if(jArry.length()>1){
for (int i = 0 ; i<jArry.length();i++){
System.out.println("your are under first part of if-array lenght--"+jArry.length());
JSONObject jsobj = jArry.getJSONObject(i);
booking_id = jsobj.getString("booking_id");
name = jsobj.getString("name");
id_proof = jsobj.getString("id_proof");
mobile_no = jsobj.getString("mobile");
photo = jsobj.getString("photo");
app_detail.put("booking_id", booking_id);
app_detail.put("name", name);
app_detail.put("id_proof", id_proof);
app_detail.put("mobile_no", mobile_no);
app_detail.put("photo", photo);
applist.add(app_detail);
size = applist.size();
System.out.println("Size--***"+size);
System.out.println("Booking_id--->"+booking_id);
System.out.println("name--------->"+name);
System.out.println("mobile_no---->"+mobile_no);
System.out.println("photo-------->"+photo);
System.out.println("Hash map values ----->"+applist.get(i));
}
}else{
JSONObject jsobj = jArry.getJSONObject(0);
System.out.println("you are under second part of if");
booking_id = jsobj.getString("booking_id");
name = jsobj.getString("name");
id_proof = jsobj.getString("id_proof");
mobile_no = jsobj.getString("mobile");
photo = jsobj.getString("photo");
app_detail.put("booking_id", booking_id);
app_detail.put("name", name);
app_detail.put("id_proof", id_proof);
app_detail.put("mobile_no", mobile_no);
app_detail.put("photo", photo);
applist.add(app_detail);
size = applist.size();
}
System.out.println("Vendor_accept_info called");
Vendor_accept_info();
notrify();
}
//new Async_location().execute();
}catch(Exception e){
e.printStackTrace();
}
}
}
private void user_info(){
user_info = getSharedPreferences("user_info", MODE_PRIVATE);
client_id = user_info.getString("id", null);
}
private void notrify() {
int notificationid = 001;
NotificationManager mNotifyManager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent resultintent = new Intent(getBaseContext(),VendorAcceptNotification.class);
resultintent.putExtra("a", "a");
System.out.println("Your are under intent");
resultintent.setAction(Intent.ACTION_VIEW);
resultintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
// resultintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this,1,resultintent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.emllogo)
.setContentTitle("Vendor Available")
.setContentText("Vendor accepted your request");
// .setAutoCancel(true);
mBuilder.setContentIntent(resultPendingIntent);
mBuilder.setOngoing(true);
SharedPreferences.Editor editor = getSharedPreferences("noti", MODE_PRIVATE).edit();
editor.putString("name", "Elena");
editor.putInt("idName", 12);
editor.commit();
mNotifyManager.notify(notificationid,mBuilder.build());
}
protected void Vendor_accept_info(){
sh_pref = getSharedPreferences("vendor_accept_info", MODE_PRIVATE);
to_edit = sh_pref.edit();
to_edit.putString("flag", flag);
to_edit.putInt("size", size);
System.out.println("Value of size in vnd accpt info --->"+size);
for(int i =0 ; i<=(size-1);i++){
System.out.println("value of i-->"+i+" vaue of size--->"+size);
HashMap<String, String> a = applist.get(i);
System.out.println("Hash map values ----->"+applist.get(i));
to_edit.putString("booking_id"+i, a.get("booking_id"));
to_edit.putString("name"+i, a.get("name"));
to_edit.putString("id_proof"+i,a.get("id_proof"));
to_edit.putString("mobile_no"+i,a.get("mobile_no"));
to_edit.putString("photo"+i,a.get("photo"));
System.out.println("in vendor accept info--->"+a.get("booking_id")+"--"+a.get("name")+"---"+a.get("mobile_no")+"--"+a.get("photo"));
}
to_edit.commit();
}
}
Logcat out for System.out(11611): RESPONSE push Notification.>>>..>>>>>> {"errCode":"-1","response":"success","listing":[{"booking_id":"24","name":"Gaj","mobile":"9650322111","id_proof":"voterid_VOTER34343","photo":"http:\/\/sabnetworks.com\/easemylife\/files\/1440671319-Jellyfish.jpg"},{"booking_id":"25","name":"Gaj","mobile":"9650322111","id_proof":"voterid_VOTER34343","photo":"http:\/\/sabnetworks.com\/easemylife\/files\/1440671319-Jellyfish.jpg"}]}
09-03 13:06:09.985: I/System.out(11611): Result====>{"errCode":"-1","response":"success","listing":[{"booking_id":"24","name":"Gaj","mobile":"9650322111","id_proof":"voterid_VOTER34343","photo":"http:\/\/sabnetworks.com\/easemylife\/files\/1440671319-Jellyfish.jpg"},{"booking_id":"25","name":"Gaj","mobile":"9650322111","id_proof":"voterid_VOTER34343","photo":"http:\/\/sabnetworks.com\/easemylife\/files\/1440671319-Jellyfish.jpg"}]}
09-03 13:06:09.987: I/System.out(11611): Response in background--->success
09-03 13:06:09.993: I/System.out(11611): your are under first part of if-array lenght--2
09-03 13:06:09.993: I/System.out(11611): Size--***1
09-03 13:06:09.995: I/System.out(11611): Booking_id--->24
09-03 13:06:09.995: I/System.out(11611): name--------->Gaj
09-03 13:06:09.996: I/System.out(11611): mobile_no---->9650322111
09-03 13:06:09.996: I/System.out(11611): photo-------->http://sabnetworks.com/easemylife/files/1440671319-Jellyfish.jpg
09-03 13:06:09.997: I/System.out(11611): Hash map values ----->{id_proof=voterid_VOTER34343, booking_id=24, photo=http://sabnetworks.com/easemylife/files/1440671319-Jellyfish.jpg, mobile_no=9650322111, name=Gaj}
09-03 13:06:09.997: I/System.out(11611): your are under first part of if-array lenght--2
09-03 13:06:09.997: I/System.out(11611): Size--***2
09-03 13:06:10.000: I/System.out(11611): Booking_id--->25
09-03 13:06:10.000: I/System.out(11611): name--------->Gaj
09-03 13:06:10.001: I/System.out(11611): mobile_no---->9650322111
09-03 13:06:10.001: I/System.out(11611): photo-------->http://sabnetworks.com/easemylife/files/1440671319-Jellyfish.jpg
09-03 13:06:10.001: I/System.out(11611): Hash map values ----->{id_proof=voterid_VOTER34343, booking_id=25, photo=http://sabnetworks.com/easemylife/files/1440671319-Jellyfish.jpg, mobile_no=9650322111, name=Gaj}
09-03 13:06:10.002: I/System.out(11611): Vendor_accept_info called
方法:
Vendor_accept_info()
那么为什么我在System.out(11611): Value of size in vnd accpt info --->2
09-03 13:06:10.093: I/System.out(11611): value of i-->0 vaue of size--->2
09-03 13:06:10.094: I/System.out(11611): Hash map values ----->{id_proof=voterid_VOTER34343, booking_id=25, photo=http://sabnetworks.com/easemylife/files/1440671319-Jellyfish.jpg, mobile_no=9650322111, name=Gaj}
09-03 13:06:10.094: I/System.out(11611): in vendor accept info--->25--Gaj---9650322111--http://sabnetworks.com/easemylife/files/1440671319-Jellyfish.jpg
09-03 13:06:10.095: I/System.out(11611): value of i-->1 vaue of size--->2
09-03 13:06:10.095: I/System.out(11611): Hash map values ----->{id_proof=voterid_VOTER34343, booking_id=25, photo=http://sabnetworks.com/easemylife/files/1440671319-Jellyfish.jpg, mobile_no=9650322111, name=Gaj}
09-03 13:06:10.095: I/System.out(11611): in vendor accept info--->25--Gaj---9650322111--http://sabnetworks.com/easemylife/files/1440671319-Jellyfish.jpg
09-03 13:06:10.096: I/System.out(11611): Your are under intent
方法的第一次for循环中没有得到booking_id
值。?
答案 0 :(得分:1)
太简单了就把你的
HashMap<String, String> app_detail = new HashMap<String, String>();
不要声明它是全局的。在
中声明它在for loop
内,
for (int i = 0 ; i<jArry.length();i++){
HashMap<String, String> app_detail = new HashMap<String, String>();
// your code
applist.add(app_detail);
}
绝对可以解决您的问题。
您正在使用全局HashMap
这就是为什么您的HashMap
值会被覆盖,并且您在for loop