我知道如何获得两点之间的距离。我通常使用 AsyncTask 来完成工作。但是,现在我必须将代码放入适配器中,我很困惑。
在适配器中使用 AsyncTask 会减慢列表视图的加载速度吗?如果会,有没有更好的方法来解决这个问题?
该列表将包含很多项目(超过 50 个)。
全班代码。其中一些没有意义。工作正在进行中。
public class CompanyTileAdapter extends BaseAdapter
{
private ArrayList<CompanyTile> companyList;
private LayoutInflater mInflater;
public ImageLoader imageLoader;
public CompanyTileAdapter(Context context, ArrayList<CompanyTile> results)
{
companyList = results;
mInflater = LayoutInflater.from(context);
imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(context.getApplicationContext()));
}
@Override public int getCount()
{
return companyList.size();
}
@Override public Object getItem(int arg0)
{
return companyList.get(arg0);
}
@Override public long getItemId(int position)
{
return position;
}
@Override public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.tile_company, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.companyname);
holder.type = (TextView) convertView.findViewById(R.id.companytype);
holder.logo = (ImageView) convertView.findViewById(R.id.logo);
holder.category = (ImageView) convertView.findViewById(R.id.category);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText("Discount offered: " + companyList.get(position).getCompanyDiscout() + "%");
holder.type.setText(companyList.get(position).getCompanyName());
imageLoader.displayImage(companyList.get(position).getCompanyLogo(), holder.logo);
imageLoader.displayImage(getCategoryImage(companyList.get(position).getCompanyType()), holder.category);
return convertView;
}
static class ViewHolder
{
TextView name;
TextView type;
ImageView logo;
ImageView category;
}
private String getCategoryImage(String name)
{
String url = "";
String cat = "";
if (name.contains(" "))
{
String[] arr = name.split("\\s");
cat = arr[0];
}
else
{
cat = name;
}
url = "http://***/imgTypeLogos/" + cat + ".png";
return url;
}
private class GetDistanceTime extends AsyncTask<String, Void, String>
{
@Override protected String doInBackground(String... params)
{
String sDistance = "";
String sTime = "";
try
{
URL googleDMatrix = new URL("http://maps.googleapis.com/maps/api/distancematrix/json?origins=" + params[0]
+ "&destinations=" + params[1] + "&language=en-UK&sensor=true");
URLConnection tc = googleDMatrix.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream()));
String line;
StringBuffer sb = new StringBuffer();
// take Google's legible JSON and turn it into one big string.
while ((line = in.readLine()) != null)
{
sb.append(line);
}
// turn that string into a JSON object
JSONObject main = new JSONObject(sb.toString());
// now get the JSON array that's inside that object
JSONArray rows_array = new JSONArray(main.getString("rows"));
JSONObject elements_object = rows_array.getJSONObject(0);
JSONArray elements_array = elements_object.getJSONArray("elements");
JSONObject distance_object = elements_array.getJSONObject(0);
JSONObject distance = distance_object.getJSONObject("distance");
sDistance = distance.getString("text");
JSONObject time = distance_object.getJSONObject("duration");
sTime = distance.getString("text");
}
catch (Exception e)
{
sDistance = "N/A";
sTime = "N/A";
e.printStackTrace();
}
return sDistance + ":" + sTime;
}
@Override protected void onPostExecute(String result)
{
String[] arr = result.split(":");
}
}
private void setDistanceTimeValues(TextView dist, TextView time)
{
}