Natlum
Natlum

Reputation: 160

Failed to retrieve data from a database, to complete a listview

I'm using databases to complete a listview. But I've run into a problem that is not solved. The problem arises when I create a variable lugar of the class Lugar.

Here is the class code Lugar:

public class Lugar {
    private String nombre;
    private String direccion;
    private GeoPuntoAlt posicion;
    private String foto;
    private int telefono;
    private String url;
    private String comentario;
    private long fecha;
    private float valoracion;
    private TipoLugar tipo;

    public Lugar(String nombre, String direccion, double longitud,
                 double latitud, double altura, TipoLugar tipo,  int telefono, String url, String comentario,
                 int valoracion) {
        fecha = System.currentTimeMillis();
        posicion = new GeoPuntoAlt(longitud, latitud, altura);
        this.nombre = nombre;
        this.direccion = direccion;
        this.telefono = telefono;
        this.url = url;
        this.comentario = comentario;
        this.valoracion = valoracion;
        this.tipo = tipo;
        fecha = System.currentTimeMillis();
        posicion = new GeoPuntoAlt(longitud, latitud, altura);
        tipo = TipoLugar.OTROS;
    }


    public String getNombre() {
        return nombre;
    }

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

    public String getDireccion() {
        return direccion;
    }

    public void setDireccion(String direccion) {
        this.direccion = direccion;
    }

    public GeoPunto getPosicion() {
        return posicion;
    }

    public void setPosicion(GeoPuntoAlt posicion) {
        this.posicion = posicion;
    }

    public String getFoto() {
        return foto;
    }

    public void setFoto(String foto) {
        this.foto = foto;
    }

    public int getTelefono() {
        return telefono;
    }

    public void setTelefono(int telefono) {
        this.telefono = telefono;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getComentario() {
        return comentario;
    }

    public void setComentario(String comentario) {
        this.comentario = comentario;
    }

    public long getFecha() {
        return fecha;
    }

    public void setFecha(long fecha) {
        this.fecha = fecha;
    }

    public float getValoracion() {
        return valoracion;
    }

    public void setValoracion(float valoracion) {
        this.valoracion = valoracion;
    }

    public TipoLugar getTipo() {
        return tipo;
    }

    public void setTipo(TipoLugar tipo) {
        this.tipo = tipo;
    }

    @Override
    public String toString() {
        return "Lugar{" +
                "nombre='" + nombre + '\'' +
                ", direccion='" + direccion + '\'' +
                ", posicion=" + posicion +
                ", foto='" + foto + '\'' +
                ", telefono=" + telefono +
                ", url='" + url + '\'' +
                ", comentario='" + comentario + '\'' +
                ", fecha=" + fecha +
                ", valoracion=" + valoracion +
                ", tipo=" + tipo +
                '}';
    }
}

And this is the Class Lugar where the Element method is I put lugar = new Lugar();. This line is what gives me error and I do not understand why.

public class Lugares {

    private static LugaresBD lugaresBD;


    protected static List<Lugar> vectorLugares = ejemploLugares();

    public Lugares() {
        vectorLugares = ejemploLugares();
    }

    public static Lugar elemento(int id) {
        Lugar lugar = null;
        SQLiteDatabase bd = lugaresBD.getReadableDatabase();
        Cursor cursor = bd.rawQuery("SELECT * FROM lugares WHERE _id = " + id, null);
        if (cursor.moveToNext()){
            lugar = new Lugar();
            lugar.setNombre(cursor.getString(1));
            lugar.setDireccion(cursor.getString(2));
            lugar.setPosicion((GeoPuntoAlt) new GeoPunto(cursor.getDouble(3), cursor.getDouble(4)));
            lugar.setTipo(TipoLugar.values()[cursor.getInt(5)]);
            lugar.setFoto(cursor.getString(6));
            lugar.setTelefono(cursor.getInt(7));
            lugar.setUrl(cursor.getString(8));
            lugar.setComentario(cursor.getString(9));
            lugar.setFecha(cursor.getLong(10));
            lugar.setValoracion(cursor.getFloat(11));
        }
        cursor.close();
        bd.close();
        return lugar;
    }

    static void anyade(Lugar lugar){
        vectorLugares.add(lugar);
    }

    final static String TAG = "MisLugares";
    protected static GeoPunto posicionActual = new GeoPunto(0,0);

    static int nuevo(){
        Lugar lugar = new Lugar("1Escuela Politécnica Superior de Gandía",
                "C/ Paranimf, 1 46730 Gandia (SPAIN)", -0.166093, 38.995656, 1500,
                TipoLugar.EDUCACION,962849300, "http://www.epsg.upv.es",
                "Uno de los mejores lugares para formarse.", 3);
        vectorLugares.add(lugar);
        return vectorLugares.size()-1;
    }

    static void borrar(int id){
        vectorLugares.remove(id);
    }

    public static int size() {
        return vectorLugares.size();
    }

    public static ArrayList<Lugar> ejemploLugares() {
        ArrayList<Lugar> lugares = new ArrayList<Lugar>();
        lugares.add(new Lugar("Escuela Politécnica Superior de Gandía",
                "C/ Paranimf, 1 46730 Gandia (SPAIN)", -0.166093, 38.995656, 800,
                TipoLugar.EDUCACION,962849300, "http://www.epsg.upv.es",
                "Uno de los mejores lugares para formarse.", 3));

        lugares.add(new Lugar("Al de siempre",
                "P.Industrial Junto Molí Nou - 46722, Benifla (Valencia)",
                -0.190642, 38.925857, 150, TipoLugar.BAR, 636472405, "",
                "No te pierdas el arroz en calabaza.", 3));

        lugares.add(new Lugar("androidcurso.com",
                "ciberespacio", 0.0, 0.0, 2000, TipoLugar.EDUCACION,
                962849300, "http://androidcurso.com",
                "Amplia tus conocimientos sobre Android.", 5));

        lugares.add(new Lugar("Barranco del Infierno",
                "Vía Verde del río Serpis. Villalonga (Valencia)",
                -0.295058, 38.867180, 000, TipoLugar.NATURALEZA,

                0, "http://sosegaos.blogspot.com.es/2009/02/lorcha-villalonga-via-verde-del-rio.html",
                "Espectacular ruta para bici o andar", 4));

        lugares.add(new Lugar("La Vital",
                "Avda. de La Vital, 0 46701 Gandía (Valencia)",
                -0.1720092, 38.9705949, 400, TipoLugar.COMPRAS,
                962881070, "http://www.lavital.es/",
                "El típico centro comercial", 2));

        return lugares;

    }

    static List listaNombres(){
        ArrayList resultado = new ArrayList();
        for (Lugar lugar:vectorLugares){
            resultado.add(lugar.getNombre());
        }
        return resultado;
    }

    public static void indicializaBD(Context contexto){
        lugaresBD = new LugaresBD(contexto);
    }

    public static Cursor listado() {
        SQLiteDatabase bd = lugaresBD.getReadableDatabase();
        return bd.rawQuery("SELECT * FROM lugares", null);
    }
}

The sample error is as follows:

Lugar() in Lugar cannot be applied to:

Thank you very much for the help, I am learning a lot.

Upvotes: 0

Views: 73

Answers (1)

Mithun
Mithun

Reputation: 2075

Since you have

public Lugar(String nombre, String direccion, double longitud,
             double latitud, double altura, TipoLugar tipo,  int telefono, String url, String comentario,
             int valoracion) 

in your Lugar class, default constructor won't be available.

Either you need to always use above constructor or define an empty constructor

public Lugar() {
}

Read more here

Upvotes: 2

Related Questions