SQLite and listView does not list

2

It shows me the following:

And the logCAT:

The app consists of showing the business of my town in a listView.

The Business class I have the getter and setters methods.

I have the class:

public class NegociosDBOpenHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "negocio.db";
    private static final int DATABASE_VERSION = 1;


    public static final String TABLE_NEGOCIOS = "negocios";
    public static final String COLUMN_ID = "negocio_id";
    public static final String COLUMN_NOMBRE = "nombre";
    public static final String COLUMN_DIRECCION = "direccion";
    public static final String COLUMN_DESCRIPCION = "descripcion";
    public static final String COLUMN_CATEGORIA = "categoria";


    public static final String TABLE_CREATE =               //METODO PARA CREAR TABLA
            "CREATE TABLE " + TABLE_NEGOCIOS + " (" +
                    COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    COLUMN_NOMBRE + " TEXT, " +
                    COLUMN_DIRECCION + " TEXT, " +
                    COLUMN_DESCRIPCION + " TEXT, " +
                    COLUMN_CATEGORIA + " TEXT " +
                    ")";








    public NegociosDBOpenHelper(Context context){
        super(context,DATABASE_NAME,null,DATABASE_VERSION);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(TABLE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NEGOCIOS);
        onCreate(db);
    }
}

The second class that is the datasource:

public class NegociosDataSource {
    SQLiteOpenHelper openHelper;
    SQLiteDatabase database;


    private static final String[] allcolumns = {

            NegociosDBOpenHelper.COLUMN_ID,
            NegociosDBOpenHelper.COLUMN_NOMBRE,
            NegociosDBOpenHelper.COLUMN_DIRECCION,
            NegociosDBOpenHelper.COLUMN_DESCRIPCION,
            NegociosDBOpenHelper.COLUMN_CATEGORIA
    };


    public NegociosDataSource(Context context){

            openHelper = new NegociosDBOpenHelper(context);
    }

    public void open(){
        database = openHelper.getWritableDatabase();

    }

    public void close(){

        openHelper.close();
    }

    public Negocio create (Negocio negocio){
        ContentValues values = new ContentValues();
        values.put(NegociosDBOpenHelper.COLUMN_NOMBRE, negocio.getNombre());
        values.put(NegociosDBOpenHelper.COLUMN_DESCRIPCION,negocio.getDescripcion());
        values.put(NegociosDBOpenHelper.COLUMN_DIRECCION,negocio.getDomicilio());
        values.put(NegociosDBOpenHelper.COLUMN_CATEGORIA,negocio.getCategoria());
        long insertid = database.insert(NegociosDBOpenHelper.TABLE_NEGOCIOS,null,values);
        negocio.setId(insertid);

        return negocio;

    }


    public List<Negocio> findAll(){   //PROBAR DE CAMBIAR ALLCOLUMNS POR: NEGOCIOSDBOPENHELPER.COLUMNSCATEGORIA
        Cursor cursos = database.query(NegociosDBOpenHelper.TABLE_NEGOCIOS,allcolumns,null,null,null,null,null);
        List<Negocio> negocio = cursorToList(cursos);
        return negocio;

    }


    public List<Negocio> cursorToList(Cursor cursor){
        List<Negocio> negocio = new ArrayList<Negocio>();
        if (cursor.getCount()>0){
           while (cursor.moveToNext()){
               Negocio negocios = new Negocio();
               negocios.setId(cursor.getInt(cursor.getColumnIndex(NegociosDBOpenHelper.COLUMN_ID)));
               negocios.setCategoria(cursor.getString(cursor.getColumnIndex(NegociosDBOpenHelper.COLUMN_CATEGORIA)));
               negocios.setDescripcion(cursor.getString(cursor.getColumnIndex(NegociosDBOpenHelper.COLUMN_DESCRIPCION)));
               negocios.setDomicilio(cursor.getString(cursor.getColumnIndex(NegociosDBOpenHelper.COLUMN_DIRECCION)));
               negocios.setNombre(cursor.getString(cursor.getColumnIndex(NegociosDBOpenHelper.COLUMN_NOMBRE)));
               negocio.add(negocios);
           }
        }
        return negocio;
    }

}

And the class where the ListView is

public class categoria extends AppCompatActivity {
    private NegociosDataSource dataSource;
    private ListView lista;
    List<Negocio> negocio;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_categoria);

        dataSource = new NegociosDataSource(this);
        dataSource.open();
        negocio = dataSource.findAll();

        lista = (ListView) findViewById(R.id.lista);

        if (negocio.size()==0){
            createdata();
            negocio = dataSource.findAll();
        }

        RefreshDisplay();
    }
















    private void createdata(){
        Negocio negocio = new Negocio();
        negocio.setNombre("La buena atencion");
        negocio.setDomicilio("H Yrigoyen");
        negocio.setDescripcion("Carniceria, los mejores cortes");
        negocio.setCategoria("carniceria");
        dataSource.create(negocio);

        negocio = new Negocio();
        negocio.setNombre("Citadella");
        negocio.setDomicilio("H Yrigoyen");
        negocio.setDescripcion("Panaderia del centro con buenas masas");
        negocio.setCategoria("panaderia");
        dataSource.create(negocio);


        negocio = new Negocio();
        negocio.setNombre("La union");
        negocio.setDomicilio("H Yrigoyen");
        negocio.setDescripcion("Panaderia que abre poco dias");
        negocio.setCategoria("panaderia");
        dataSource.create(negocio);

        negocio = new Negocio();
        negocio.setNombre("Electronica John");
        negocio.setDomicilio("Alem");
        negocio.setDescripcion("De todo");
        negocio.setCategoria("electronica");
        dataSource.create(negocio);
    }


    public void RefreshDisplay(){

        ArrayAdapter<Negocio> adapter = new ArrayAdapter<Negocio>(this,android.R.layout.simple_expandable_list_item_1,negocio);
        lista.setAdapter(adapter);

    }



}

The Business class:

public class Negocio {
    long id;
    String nombre;
    String descripcion;
    String domicilio;
    String categoria;


    public long getId() {
        return id;
    }


    public void setId(long id) {
        this.id = id;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public String getDescripcion() {
        return descripcion;
    }

    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }

    public String getDomicilio() {
        return domicilio;
    }

    public void setDomicilio(String domicilio) {
        this.domicilio = domicilio;
    }

    public String getCategoria() {
        return categoria;
    }

    public void setCategoria(String categoria) {
        this.categoria = categoria;
    }
}
    
asked by Ibarra Emiliano 15.10.2018 в 16:26
source

1 answer

2

You must overwrite the toString() method in your negocio object:

@Override
public String toString() {
     return nombre;
}

From this, instead of showing a representation of the object in your Adapter , it would show the value you want to display in your Adapter .

    
answered by 15.10.2018 / 16:40
source