swap picture in customview list Android Studio

1

I'm getting sqlite data in the following way.

public Cursor informacionPrincipal() {
    String whereClause = "estado = ?";
    String[] whereArgs = new String[] {"1"};

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_REGISTRO, new String[]{
            COLUMN_ID,
            COLUMN_REGISTRO,
            COLUMN_HUERTO,
            COLUMN_UBICACION
     }, whereClause, whereArgs, null, null, null);
    if (cursor != null) {
        cursor.moveToFirst();
        return cursor;
    } else {
        return null;
    }
}

and I show the information in Listview in the following way:

public void MostrarListado() {
    try {
        Cursor cursor = basededatos.informacionPrincipal();
        if (cursor == null) {
            Toast.makeText(this, "Se ha presentado un problema al cargar", Toast.LENGTH_LONG).show();
            return;
        }
        if (cursor.getCount() == 0) {
            Toast.makeText(this, "Ninguna Corte", Toast.LENGTH_LONG).show();
            finish();
            return;
        }
        String[] columns = new String[]{
                basededatos.COLUMN_ID,
                basededatos.COLUMN_REGISTRO,
                basededatos.COLUMN_HUERTO,
                basededatos.COLUMN_UBICACION
        };

        int[] boundTo = new int[]{
                R.id.txtCodigo_Listado,
                R.id.txtCreacion_Listado,
                R.id.txtHuerto_Cancelado,
                R.id.txtUbicacion_Listado
        };

        simpleCursorAdapter = new SimpleCursorAdapter(this,
                R.layout.item_listado,
                cursor,
                columns,
                boundTo,
                0);

        listado.setAdapter(simpleCursorAdapter);
    } catch (Exception ex) {
        Toast.makeText(this, "Se ha producido un error", Toast.LENGTH_LONG).show();
    }
}

I want that if COLUMN_REGISTRO es igual a 20 for example shows me an image that I already have in the folder minimap . and si es 10 me muestre otra but where should the condition go?

    
asked by DoubleM 19.12.2016 в 23:37
source

1 answer

2

With a SimpleCursorAdapter is not possible, you have to implement a CustomAdapter that extends from SimpleCursorAdapter and within bindView() , you would perform the validation.

public class CustomAdapter extends SimpleCursorAdapter {

...
...
...

@Override
public void bindView(View v, Context context, Cursor c) {

  int columnRegistro = c.getColumnIndex("column_registro");     
  ImageView imageView = (ImageView) v.findViewById(R.id.myimageview);

    if (columnRegistro == 20) {
      //carga imagen de /mipmap.
      imageView.setImageResource(R.mipmap.mi_imagen);
    }else if (columnRegistro == 10) {
      //carga otra imagen...
  imageView.setImageResource(R.mipmap.otra_imagen);
    } 

 }

...
...


}

I add an example of CustomAdapter :

  public class CustomAdapter extends SimpleCursorAdapter {

            private Context mContext;
            private Context appContext;
            private int layout;
            private Cursor c;
            private final LayoutInflater inflater;

            public CustomAdapter(Context context,int layout, Cursor c,String[] from,int[] to) {
                super(context, layout, c, from, to);
                this.layout = layout;
                this.mContext = context;
                this.inflater = LayoutInflater.from(context);
                this.c = c;
            }

            @Override
            public View newView (Context context, Cursor cursor, ViewGroup parent) {
                    return inflater.inflate(layout, null);
            }

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

            }

    }
    
answered by 19.12.2016 / 23:55
source