这是我的getView()方法,我试图在从凌空中取出后设置距离值。这里距离计算是正确的。
public View getView(final int position, View convertView, ViewGroup parent)
{
listrowposition = position;
if (convertView == null)
{
LayoutInflater inflater = getActivity().getLayoutInflater();
convertView = inflater.inflate(R.layout.singlerowallassigendloction, null);
holder = new ViewHolder();
holder.distance = (TextView) convertView.findViewById(R.id.distance);
holder.lati = (TextView) convertView.findViewById(R.id.lati);
holder.longi = (TextView) convertView.findViewById(R.id.longi);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.lati.setText(salesmanlocationArrayList.get(listrowposition).getLati());
holder.longi.setText(salesmanlocationArrayList.get(listrowposition).getLongi());
double lat1= Double.parseDouble(holder.lati.getText().toString());
double lng1= Double.parseDouble(holder.longi.getText().toString());
vollyRequest_Fetch_distance(lat1,lng1,lat,lng);
Log.d("distance_ll=","tex="+text+" "+value+" "+lat1);
double d= Double.parseDouble(value)/1000;
holder.distance.setText(""+new DecimalFormat("##.##").format(d)+" KM");
return convertView;
}
这是我的截击请求代码
public void vollyRequest_Fetch_distance(double lat11, double lon11, double lat22, double lon22)
{
String url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins="+lat11+","+lon11+"&"+"destinations="+lat22+","+lon22;
Log.d("RESPOetchlocation..>>> ", url + "<<<");
RequestQueue queue = Volley.newRequestQueue(getActivity());
StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>()
{
@Override
public void onResponse(String response)
{
Log.d("RESPONFetchlocation>>> ", response + "<<<");
// progressDialog.dismiss();
Jsonresponse_Distance(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("RESPONSE:Error>>> ", error.toString() + "<<<");
// progressDialog.dismiss();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("key", "Arz5SyA5UFy-pTsr5cIdwxghhnV6BoH-pCJBARg");
return params;
}
};
queue.add(request);
}
public void Jsonresponse_Distance(String str)
{
JSONObject jsonObject = null;
JSONArray jsonArray = null;
JSONArray jsonArray_elements = null;
JSONObject jsonObject_elements = null;
JSONObject jobj;
String error = null;
String msg = null;
try
{
jsonObject = new JSONObject(str);
Log.d("jsonObject==", jsonObject.toString());
msg = jsonObject.getString("status");
if (msg.equals("OK"))
{
jsonArray = jsonObject.getJSONArray("rows");
Log.d("jsonArray.length()=",""+jsonArray.length());
for (int i = 0; i < jsonArray.length(); i++)
{
jobj= jsonArray.getJSONObject(i);
jsonArray_elements= jobj.getJSONArray("elements");
Log.d("jsonArray_ets.length()=",""+jsonArray_elements.length());
for (int j = 0; j < jsonArray_elements.length(); j++)
{
jsonObject_elements= jsonArray_elements.getJSONObject(0);
Log.d("jsonObject_elements=",jsonObject_elements.toString());
JSONObject job= jsonObject_elements.getJSONObject("distance");
Log.d("job=",job.toString());
text= job.getString("text");
value= job.getString("value");
Log.d("job=",""+text+" "+value);
}
}
}
else
{
}
}
catch (JSONException e)
{
e.printStackTrace();
}
}
我得到了值(距离)的正确结果,但在getview
方法中获取null请帮助我。
//这里我试图在获取截击请求后设置我的适配器
public void Jsonresponse_Viewlocation(String str)
{
JSONObject jsonObject = null;
JSONArray jsonArray = null;
JSONObject jobj;
String error = null;
String msg = null;
salesmanlocationArrayList.clear();
try
{
jsonObject = new JSONObject(str);
Log.d("jsonObject==", jsonObject.toString());
msg = jsonObject.getString("status");
if (msg.equals("true")) {
jsonArray = jsonObject.getJSONArray("response");
for (int i = 0; i < jsonArray.length(); i++)
{
salesmanlocation = new Salesmanlocation();
jobj = jsonArray.getJSONObject(i);
address = jobj.getString("address");
salesmanlocation.setAddress(address); salesmanlocation.setAddress(jobj.getString("address"));
String latlong_string=getLocationFromAddress(address);
String latlong[]=latlong_string.split(",");
String lat1=latlong[0];
String lng1=latlong[1];
Log.d("latlng==",""+lat1+" "+lng1);
double latt= Double.parseDouble(lat1);
double lng1g= Double.parseDouble(lng1);
salesmanlocation.setLati(String.valueOf(lat1));
salesmanlocation.setLongi(String.valueOf(lng1));
salesmanlocationArrayList.add(salesmanlocation);
}
}
else
{
}
}
catch (JSONException e)
{
e.printStackTrace();
}
adapter = new Baseddapter_Allassignloc();
alllist.setAdapter(adapter);
}