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

时间:2015-09-30 15:14:01

标签: android android-listview android-asynctask android-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中尝试了不同的变体,但它不起作用,就像没有代码一样。希望你理解我的英语,有人给出建议

2 个答案:

答案 0 :(得分:0)

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

在你的代码中你设置了imege

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

只需在onPostExecute

中添加此代码即可

答案 1 :(得分:0)

它的工作!我还在Async中添加了一些代码,所有工作都可以,但是图片和名称出现的时间太长了。

私有类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());
    }
}

有关详细信息:当您打开应用程序时,此列表位于第一个屏幕上 在第一个活动中,我将片段替换为我正在进行所有操作的片段。有任何想法如何加快AsyncTask中的操作?

非常感谢您的帮助!