在我的应用中,我有一个 ListView ,可显示照片,标题和说明。 但是当我滚动ListView时,即使关闭应用程序也会变慢。每次滚动时ListView都会加载元素,这就是应用程序崩溃的原因。
有人可以帮我做一个有效的ListView吗?
这是我的代码:
public class PropiedadCursorAdapter extends CursorAdapter{
private LayoutInflater mLayoutInflater;
private Context ctx;
private Cursor miCursor;
private int layout;
private int casaIndex;
private int tituloIndex;
private int ubicacionIndex;
private PropiedadDbAdapter dbAdapter =null;
private class ViewHolder {
ImageView ivCasa;
TextView tvTitulo;
TextView tvUbicacion;
ViewHolder(View v) {
tvTitulo = (TextView) v.findViewById(R.id.tv_titulo);
tvUbicacion = (TextView) v.findViewById(R.id.tv_ubicacion);
ivCasa = (ImageView) v.findViewById(R.id.ivCasa);
}
}
public PropiedadCursorAdapter(Context context, int layout, Cursor c){
super(context, c);
this.ctx = context;
this.miCursor = c;
mLayoutInflater = LayoutInflater.from(context);
}
@Override
public void bindView(View v, Context context, Cursor c) {
int icol_ivCasa = c.getColumnIndex(C_COLUMNA_IMAGENES);
int icol_tvTitulo = c.getColumnIndex(PropiedadDbAdapter.C_COLUMNA_TITULO);
int icol_tvUbicacion = c.getColumnIndex(PropiedadDbAdapter.C_COLUMNA_DIRECCION);
String stxtCasa = c.getString(icol_ivCasa);
String stxtTitulo = c.getString(icol_tvTitulo);
String stxUbicacion = c.getString(icol_tvUbicacion);
ViewHolder vh = (ViewHolder)v.getTag();
Picasso.with(context).load(stxtCasa).into(vh.ivCasa);
vh.tvTitulo.setText(stxtTitulo);
vh.tvUbicacion.setText(stxUbicacion);
Log.d("PropiedadCursor", stxtCasa);
}
@Override
public View newView(Context ctx, Cursor cursor, ViewGroup parent) {
View vView = mLayoutInflater.inflate(R.layout.layout_lista_propiedades, parent, false);
vView.setTag( new ViewHolder(vView) );
return vView;
}
}
这是我的dbAdapter
public class PropiedadDbAdapter {
public static final String C_TABLAp = "PROPIEDADES";
/**
* TABLE PROPIEDADES
*/
public static final String C_COLUMNA_ID = "_id";
public static final String C_COLUMNA_TITULO = "titulo";
public static final String C_COLUMNA_PROPIEDAD = "propiedad";
public static final String C_COLUMNA_CATEGORIA = "categoria";
public static final String C_COLUMNA_COLONIA = "colonia";
public static final String C_COLUMNA_DIRECCION = "direccion";
public static final String C_COLUMNA_COORDENADAS = "coordenadas";
public static final String C_COLUMNA_SUPERFICIE = "superficie";
public static final String C_COLUMNA_RECAMARAS = "recamaras";
public static final String C_COLUMNA_IMAGENES = "imagenes";
public static final String C_COLUMNA_PRECIO = "precio";
public static final String C_COLUMNA_ANTIGUEDAD = "antiguedad";
public static final String C_COLUMNA_FECHA = "fecha";
public static final String C_COLUMNA_ACTUALIZACION= "actualizacion";
public static final String C_COLUMNA_EDO_SYNC= "edo_sync";
public static final String C_COLUMNA_DESCRIPCION = "descripcion";
public static final String C_COLUMNA_USR_PROP_ID= "prop_id";
private Context contexto;
private BienesRaDbHeper mDbHelper;
private SQLiteDatabase mDb;
private String[] columnasP = new String[]{C_COLUMNA_ID, C_COLUMNA_TITULO, C_COLUMNA_PROPIEDAD, C_COLUMNA_CATEGORIA, C_COLUMNA_COLONIA, C_COLUMNA_DIRECCION, C_COLUMNA_COORDENADAS, C_COLUMNA_SUPERFICIE, C_COLUMNA_RECAMARAS, C_COLUMNA_IMAGENES, C_COLUMNA_PRECIO, C_COLUMNA_ANTIGUEDAD, C_COLUMNA_FECHA, C_COLUMNA_ACTUALIZACION, C_COLUMNA_EDO_SYNC, C_COLUMNA_DESCRIPCION, C_COLUMNA_USR_PROP_ID};
private String[] columnaUserPropID = new String[]{C_COLUMNA_USR_PROP_ID};
public PropiedadDbAdapter(Context context)
{
this.contexto = context;
}
public PropiedadDbAdapter abrir() throws SQLException
{
mDbHelper = new BienesRaDbHeper(contexto);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void cerrar()
{
mDbHelper.close();
}
public Cursor getCursor() throws SQLException
{
Cursor cursor = mDb.query(true, C_TABLAp, columnasP, null, null, null, null, null, null);
return cursor;
}
public Cursor getRegistro(long id) throws SQLException
{
Cursor c = mDb.query(true, C_TABLAp, columnasP, C_COLUMNA_ID + "=" + id, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
public long insertPropiedad(ContentValues reg)
{
if (mDb == null)
abrir();
return mDb.insert(C_TABLAp, null, reg);
}
public long update(ContentValues reg)
{
long result = 0;
if (mDb == null)
abrir();
if (reg.containsKey(C_COLUMNA_ID))
{
long id = reg.getAsLong(C_COLUMNA_ID);
reg.remove(C_COLUMNA_ID);
result = mDb.update(C_TABLAp, reg, "_id=" + id, null);
}
return result;
}
public Cursor getCursor(String filtro) throws SQLException
{
Cursor c = mDb.query(true, C_TABLAp, columnasP, filtro, null, null, null, null, null);
return c; }
public Cursor getIdProp(long id) throws SQLException {
Cursor c = mDb.query(true, C_TABLAp, columnasP, C_COLUMNA_USR_PROP_ID + "=" + id, null, null,null,null,null );
if (c != null) {
c.moveToFirst();
Log.d("PROPIEDAD", String.valueOf(c));
}
return c;
}
public long delete(long id)
{
if (mDb == null)
abrir();
return mDb.delete(C_TABLAp, "_id=" + id, null);
}}
答案 0 :(得分:1)
不幸的是我无法评论,但就像你在评论中被告知的那样,你应该使用RecyclerView(除非你的要求包括ListView)。
这是一个RecyclerView适配器的示例,您应该可以很容易地自定义它,因为它与您现有的相似:
public class PropiedadCursorAdapter extends RecyclerView.Adapter<PropiedadCursorAdapter.ViewHolder> {
private Context context;
public SearchAdapter(Context context) {
super();
this.context = context;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private ImageView ivProfilePic;
private TextView tvUsername;
public ViewHolder(View itemView) {
super(itemView);
ivProfilePic = (ImageView) itemView.findViewById(R.id.ivProfilePic);
tvUsername = (TextView) itemView.findViewById(R.id.tvUsername);
}
}
@Override
public SearchAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_search_user, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
final User user = users.get(position);
Glide.with(context)
.load(user.getProfilePicUrl())
.centerCrop()
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.into(holder.ivProfilePic);
holder.tvUsername.setText(user.getUsername());
}
@Override
public int getItemCount() {
return users.size();
}
}