Android 无法从ArrayList获取正确的值<;HashMap<;字符串,字符串>&燃气轮机;
我正在尝试从Android 无法从ArrayList获取正确的值<;HashMap<;字符串,字符串>&燃气轮机;,android,arraylist,hashmap,Android,Arraylist,Hashmap,我正在尝试从ArrayList获取我的SharedReferences的数据。当我试图在AsynTask中获取数据时,它以正确的值形式给出ArrayList但当我试图从相同的ArrayList中获取值时,它会给出不同的值。在AsynTask预订id中,值是24和25,但在供应商接受信息()方法它给出预订id25和25的值。我不知道24岁怎么变成25岁命名约定。 HashMap<String, String> app_detail = new HashMap<String, S
ArrayList
获取我的SharedReferences
的数据。当我试图在AsynTask
中获取数据时,它以正确的值形式给出ArrayList
但当我试图从相同的ArrayList
中获取值时,它会给出不同的值。在
AsynTask
预订id中,值是24和25,但在供应商接受信息()
方法它给出预订id
25和25的值。我不知道24岁怎么变成25岁<有人能告诉我我在这里犯了什么错误吗
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();
}
}
Vendor\u accept\u info()的Logcat out方法:
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
那么为什么我在vendor\u accept\u info()
HashMap<String, String> app_detail = new HashMap<String, String>();
这肯定能解决你的问题
您正在使用全局HashMap
这就是为什么您的HashMap
值得到覆盖,并且在for循环中得到最后一个值2次的原因。。。您的代码是一场噩梦,有与方法同名的变量。。。太难读了!请清理您的代码(现代IDE将为您提供提示和工具),并向我们展示问题的最低但足够的版本,而不是所有代码。请使用公共类Clinet_通知-->命名约定。
HashMap<String, String> app_detail = new HashMap<String, String>();
for (int i = 0 ; i<jArry.length();i++){
HashMap<String, String> app_detail = new HashMap<String, String>();
// your code
applist.add(app_detail);
}