0

2点間の距離を取得する方法を知っています。私は通常、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)
    {

    }
4

0 に答える 0