AsyncTask 在自定义适配器中不起作用

AsyncTask doesn`t work in custom adapter

我正在使用自定义适配器从 SQLite 创建销售点列表

    dbHelper = new YourVoiceDatabaseHelper(getActivity());

    pointsList = new ArrayList<Point>();
    List<Point> pl = new ArrayList<Point>();
    PointsListArrayAdapter adapter;

    adapter =  new PointsListArrayAdapter(getActivity().getApplicationContext(),
            pointsList,
            getActivity().getFilesDir(),
            getResources().getString(R.string.Local_Resorces_Folder),
            dbHelper);

    pointsListView.setAdapter(adapter);

    pl =  dbHelper.getAllPoints();

    if(myLocation != null) {
        for (Point el : pl) {
            elLocation.setLatitude(Double.parseDouble(el.getLatitude()));
            elLocation.setLongitude(Double.parseDouble(el.getLongtitude()));

            if (myLocation.distanceTo(elLocation) <= 999) {
                pointsList.add(el);
            }
        }
    } else {
        Toast.makeText(getActivity(), "Не удалось определить ваше местоположение, проверте ваши разрешения и настройки",
                Toast.LENGTH_LONG).show();
    } adapter.notifyDataSetChanged();

我向自定义适配器发送了使用它的数据库广告实例,这减慢了我的主要进程。我正在尝试在适配器中使用 AsyncTask 从数据库中获取图像路径并设置图像,但是 Async 什么都不做

    package ua.com.it_artel.tvoy_golos.service;

public class PointsListArrayAdapter 扩展了 BaseAdapter {

Context context;
private List<Point> pointsList;
private File appFolder;
private String resoursesFolder;
private YourVoiceDatabaseHelper dbHelper;
private String imagePath;

public PointsListArrayAdapter(Context context, List<Point> pointsList, File appFolder, String resoursesFolder, YourVoiceDatabaseHelper dbHelper) {
    this.context = context;
    this.pointsList = pointsList;
    this.appFolder = appFolder;
    this.resoursesFolder = resoursesFolder;
    this.dbHelper = dbHelper;
}

@Override
public int getCount() {
    return pointsList.size();
}

@Override
public Object getItem(int position) {
    return pointsList.get(position);
}

@Override
public long getItemId(int position) {
    return pointsList.get(position).getId();
}

static class ViewHolder {
    ImageView image;
    TextView adress;
    TextView name;
    TextView distance;
    Location elLocation;
    Point point;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder viewHolder;
    View rowView = convertView;
    if(convertView == null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = inflater.inflate(R.layout.list_element_layout_type_1, parent, false);

        viewHolder = new ViewHolder();
        viewHolder.elLocation = new Location("");

        viewHolder.image = (ImageView) rowView.findViewById(R.id.item_image);

        viewHolder.adress = (TextView) rowView.findViewById(R.id.item_text);

        viewHolder.name = (TextView) rowView.findViewById(R.id.item_name);

        viewHolder.distance = (TextView) rowView.findViewById(R.id.item_distance);

        rowView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) rowView.getTag();
    }

    viewHolder.elLocation.setLatitude(Double.parseDouble(pointsList.get(position).getLatitude()));
    viewHolder.elLocation.setLongitude(Double.parseDouble(pointsList.get(position).getLongtitude()));

    viewHolder.point = pointsList.get(position);
    new AdapterImageSetter().execute(viewHolder);

    viewHolder.adress.setText(pointsList.get(position).getAddress());

    viewHolder.name.setText(dbHelper.getPartnerByUID(pointsList.get(position).getPartnerUid()).getName());

    viewHolder.distance.setText(String.valueOf(Math.round(MyLocationListener.myLocation.distanceTo(viewHolder.elLocation))) + " м");

    return rowView;

}

private class AdapterImageSetter extends AsyncTask<ViewHolder, Void, ViewHolder> {

    @Override
    protected ViewHolder doInBackground(ViewHolder... params) {
        ViewHolder viewHolder = params[0];

        imagePath = dbHelper.getPointsPartnerImage(viewHolder.point.getPartnerUid());

        if (imagePath.equals("")) {
            viewHolder.image.setImageResource(R.drawable.no_image);
        } else {
            Drawable dr = Drawable.createFromPath(appFolder.getPath() + "/" + resoursesFolder + "/" + imagePath);
            viewHolder.image.setImageDrawable(dr);
        }
        return viewHolder;
    }
}

}

我在 AsyncTask 和 Runnable 中尝试了不同的变体,但它不起作用,就像没有代码一样。希望你能理解我的英语,并有人给予建议

你不能在doInBackground方法中执行ui相关操作ui相关操作只使用onpostexcecute方法 更多关于异步任务 http://developer.android.com/reference/android/os/AsyncTask.html

在你的代码中你设置了图像

if (imagePath.equals("")) {
            viewHolder.image.setImageResource(R.drawable.no_image);
        } else {
            Drawable dr = Drawable.createFromPath(appFolder.getPath() + "/" + resoursesFolder + "/" + imagePath);
            viewHolder.image.setImageDrawable(dr);
        }

只需在 onPostExecute

中添加此代码

成功了!我还在 Async 中添加了一些代码,一切正常,但是出现图像和名称需要太多时间。

私有 class AdapterImageSetter 扩展 AsyncTask {

    @Override
    protected ViewHolder doInBackground(ViewHolder... params) {
        ViewHolder viewHolder = params[0];

        viewHolder.imagePath = dbHelper.getPointsPartnerImage(viewHolder.point.getPartnerUid());

        viewHolder.pointName = dbHelper.getPartnerByUID(viewHolder.point.getPartnerUid()).getName();
        return viewHolder;
    }

    @Override
    protected void onPostExecute(ViewHolder viewHolder) {

        if (viewHolder.imagePath.equals("")) {
            viewHolder.image.setImageResource(R.drawable.no_image);
        } else {
            Drawable dr = Drawable.createFromPath(appFolder.getPath() + "/" + resoursesFolder + "/" + viewHolder.imagePath);
            viewHolder.image.setImageDrawable(dr);
        }

        viewHolder.name.setText(viewHolder.pointName);

        Log.i("IMAGE PATH", viewHolder.imagePath);
        Log.i("PARTNER UID", viewHolder.point.getPartnerUid());
    }
}

更多信息:当您打开应用程序时,此列表会出现在第一个屏幕上。
在第一个 activity 中,我替换了 fragment 和 шт 我正在执行所有操作的片段。对如何加快 AsyncTask 中的操作有任何想法吗?

非常感谢您的帮助!