Problem with programming logic in Java

Asked

Viewed 108 times

-3

Good afternoon to all,

started two weeks ago a sequence of studies on programming for Android. I am studying by this book

http://novatec.com.br/livros/googleand/sumario9788575221860.pdf

bought it a while ago and thank God I finally got on page 341 which is database.

has a class called car, I understand object orientation, but I’ve never seen such a strange class as this

package com.example.googleplay.model;

import java.net.URI;

import android.content.ContentUris;
import android.net.Uri;
import android.provider.BaseColumns;

public class Carro {
    public static String[] colunas = new String[] { Carros._ID, Carros.NOME,
            Carros.PLACA, Carros.ANO };

    public static final String AUTHORITY = "com.example.googleplay.model.carro";

    public long id;
    public String nome;
    public String placa;
    public int ano;

    public Carro() {

    }

    public Carro(String nome, String placa, int ano) {
        super();
        this.nome = nome;
        this.placa = placa;
        this.ano = ano;

    }

    public Carro(long id, String nome, String placa, int ano) {
        super();
        this.id = id;
        this.nome = nome;
        this.placa = placa;
        this.ano = ano;

    }

    public static final class Carros implements BaseColumns {
        private Carros() {
        }
    }

    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
            + "/carros");
    public static final String CONTENT_TYPE = "vnd.android.curso.dir/vnd.google.carros";
    public static final String CONTENT_ITEM_TYPE = "vnd.android.curso.item/vnd.google.carros";
    public static final String DEFAULT_SORT_ORDER = "_id_ASC";

    public static final String NOME = "nome";
    public static final String ANO = "ano";
    public static final String PLACA = "placa";

    public static Uri getUriId() {
        Uri uriCarro = ContentUris.withAppendedId(Carros.CONTENT_URI, id);
        return uriCarro;
    }

    @Override
    public String toString() {
        return "Nome: " + nome + ", Placa " + placa + ", Ano " + ano;
    }
}

I’ll just comment on the parts that went red.

public static String[] colunas = new String[] { Carros._ID, Carros.NOME,
        Carros.PLACA, Carros.ANO };

To exist Carros there needed to be a class called Carros, but in the book project there is no class called Carros, he recognized only _ID

The other weirdest thing is Carros could see _ID.

Then I really can’t explain.

This other piece of code was also in red:

    public static Uri getUriId() {
        Uri uriCarro = ContentUris.withAppendedId(Carros.CONTENT_URI, id);
        return uriCarro;
    }

What turned red was the CONTENT.

I understand that it’s complicated to have to help without having access to the book, but even so I posted, I hope to get some help.

1 answer

1


Some lines are not where they should be. The class Carros became very short and contained only one constructor. All lines below it actually belong inside the class Carros. The class Carros so it would look like this:

public static final class Carros implements BaseColumns {
    private Carros() {
    }

    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
            + "/carros");
    public static final String CONTENT_TYPE = "vnd.android.curso.dir/vnd.google.carros";
    public static final String CONTENT_ITEM_TYPE = "vnd.android.curso.item/vnd.google.carros";
    public static final String DEFAULT_SORT_ORDER = "_id_ASC";

    public static final String NOME = "nome";
    public static final String ANO = "ano";
    public static final String PLACA = "placa";

    public static Uri getUriId() {
        Uri uriCarro = ContentUris.withAppendedId(Carros.CONTENT_URI, id);
        return uriCarro;
    }

    @Override
    public String toString() {
        return "Nome: " + nome + ", Placa " + placa + ", Ano " + ano;
    }
}

Note that now all fields CONTENT_URI, CONTENT_TYPE, as well as NOME, ANO, PLACA, etc. belong to the class Carros and therefore IDE will recognize the fields Carros.NOME, Carros.CONTENT_URI, etc. which were once in red.

Note also that by implementing the interface BaseColumns, the class Carros acquired a field _ID, which is the Carros._ID seen more at the beginning of the code posted in the question.

  • our bro! perfect, it worked, thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.