经过大量谷歌搜索后,我终于完成了我的任务。我通过异步任务尝试了它..
这是我的代码..
public View getView(int position, View convertView, ViewGroup Parent)
{
final ViewHolder holder=new ViewHolder();
View view=convertView;
if (view==null) {
convertView= inflater.inflate(R.layout.layout_row, null);
}
holder.textdistance=(TextView) convertView.findViewById(R.id.textView_Distance);
holder.position = position;
if(data.size()<=0)
{
Toast.makeText(activity, "No data", Toast.LENGTH_LONG).show();
}
else
{
**new ThumbnailTask(position, holder)
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null);**
}
return convertView;
}
//Thumbnail Class
private static class ThumbnailTask extends AsyncTask {
private int mPosition;
private ViewHolder mHolder;
public ThumbnailTask(int position, ViewHolder holder) {
mPosition = position;
mHolder = holder;
}
/* (non-Javadoc)
* @see android.os.AsyncTask#doInBackground(Params[])
*/
@Override
protected Object doInBackground(Object... params) {
// TODO Auto-generated method stub
String Distance="3";
String uri="https://maps.googleapis.com/maps/api/distancematrix/json? origins=(Your source Addres like XYZ,South Delhi-110064),&mode=deriving&sensor=false&key=(Your API Key)";
String result= GET(uri);
Log.d("result","res="+result);
try {
//jArray = new JSONObject(result);
JSONObject object = (JSONObject) new JSONTokener(result).nextValue();
JSONArray array = object.getJSONArray("rows");
// Log.d("JSON","array: "+array.toString());
//Routes is a combination of objects and arrays
JSONObject rows = array.getJSONObject(0);
//Log.d("JSON","routes: "+routes.toString());
JSONArray elements = rows.getJSONArray("elements");
// Log.d("JSON","legs: "+legs.toString());
JSONObject steps = elements.getJSONObject(0);
//Log.d("JSON","steps: "+steps.toString());
JSONObject distance = steps.getJSONObject("distance");
Log.d("JSON","distance: "+distance.toString());
Distance = distance.getString("text");
Log.d("final value","tot dis="+Distance);
JSONObject duration=steps.getJSONObject("duration");
// MyDuration=duration.getString("text");
}
catch(JSONException e)
{
Log.d("JSONEXCeption","my exce"+e.getMessage());
}
return Distance;
}
@Override
protected void onPostExecute(Object result) {
// TODO Auto-generated method stub
mHolder.textdistance.setText(result.toString());
}
}
//method to execute Url
private static String GET(String url)
{
InputStream inputStream = null;
String result = "";
try {
// create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// make GET request to the given URL
HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
// receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// convert inputstream to string
if(inputStream != null)
result = convertInputStreamToString(inputStream);
// Log.i("Result",result);
else
result = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", "hello"+e);
}
return result;
}
private static String convertInputStreamToString(InputStream inputStream) throws IOException{
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
}
参考链接是-
http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
https://developers.google.com/maps/documentation/distancematrix/
我不善于解释,但我正在尽力而为。希望它对像我这样的其他人有所帮助,因为我陷入了这项任务。