【问题标题】:Listview with images saved on the sdcard gets so slow带有保存在 SD 卡上的图像的 Listview 变得如此缓慢
【发布时间】:2017-02-18 09:10:30
【问题描述】:

我有一个应用程序从相机中获取图像,然后将它们存储在一个文件夹中,路径存储在 SQLite 数据库中,随后显示在 Listview 中,但是当我滚动时,ListView 移动非常慢。

Android 监视器向我显示以下消息

应用程序可能在您的主线程上做太多的工作。

我读到做一个 AsyncTask 问题就解决了,我想知道如何使用 AsyncTask 获取图像的路径。

这是我的代码

public class PropiedadCursorAdapter extends CursorAdapter{

private PropiedadDbAdapter dbAdapter =null;

public PropiedadCursorAdapter (Context context,  Cursor c){
    super(context, c);
    dbAdapter = new PropiedadDbAdapter(context);
    dbAdapter.abrir();
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {

    View view =((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.layout_lista_propiedades,parent,false);
    ViewHolder holder = new ViewHolder();
    holder.ivCasa = (ImageView) view.findViewById(R.id.ivCasa);
    holder.tvTitulo= (TextView) view.findViewById(R.id.tv_titulo);
    holder.tvUbicacion= (TextView) view.findViewById(R.id.tv_ubicacion);

    view.setTag(holder);
    return view;
}

static class ViewHolder {
    ImageView ivCasa;
    TextView tvTitulo;
    TextView tvUbicacion;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {

    ViewHolder holder = (ViewHolder) view.getTag();
    String cadena = cursor.getString(cursor.getColumnIndex(PropiedadDbAdapter.C_COLUMNA_IMAGENES));
    Picasso.with(context).load(cadena).into(holder.ivCasa);

    holder.tvUbicacion.setText(cursor.getString(cursor.getColumnIndex(PropiedadDbAdapter.C_COLUMNA_DESCRIPCION)));
    holder.tvTitulo.setText(cursor.getString(cursor.getColumnIndex(PropiedadDbAdapter.C_COLUMNA_TITULO)));
}}

这是我的异步任务

public class getImageTask extends AsyncTask<PropiedadCursorAdapter.ViewHolder, Void, Bitmap> {

private PropiedadCursorAdapter.ViewHolder v;


@Override
protected Bitmap doInBackground(PropiedadCursorAdapter.ViewHolder... params) {
    v = params[0];
    return null;
}

@Override
protected void onPostExecute(Bitmap bitmap) {
    super.onPostExecute(bitmap);
    String cadena = PropiedadDbAdapter.C_COLUMNA_IMAGENES; 

} }

谢谢

【问题讨论】:

  • 没有路径怎么获得?
  • ViewHolder holder = (ViewHolder) view.getTag(); String cadena = cursor.getString(cursor.getColumnIndex(PropiedadDbAdapter.C_COLUMNA_IMAGENES)); Picasso.with(context).load(cadena).into(holder.ivCasa);
  • 我不知道该怎么做。
  • I would like to know how to get the path of the images using AsyncTask. ????如果您可以在没有异步任务的情况下获得路径,那么如果您使用异步任务有什么问题?之前有人问过你。
  • 我不知道如何在 AsyncTask 中实现它,我应该在 onPostExecute() 方法中放入什么?

标签: android sqlite listview android-asynctask imageview


【解决方案1】:

在bindView数据获取中不要进入Adapter,而应该请求完整的数据初始化,然后存放在一个Bean的包中,然后设置成Adapter,这样速度会快很多。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多