Problem with programming logic in Java

-3

Good afternoon to all,

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

link

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

It has a class called car, I understand object-oriented, but I have never seen a class as strange as that

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 will only comment on the parts that have gone red.

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

In order to exist Carros it was necessary that there was a class called Carros , but in the book project there is no class called Carros , it recognized only _ID

The oddest thing is that Carros was able to see _ID .

So I really can not explain.

This other piece of code is also red:

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

What went red was CONTENT .

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

    
asked by anonymous 02.12.2014 / 21:47

1 answer

1

Some lines are not where they should be. Class Carros was too short and contained only one constructor. All of the lines below it actually belong to the Carros class. The class Carros therefore looks 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 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 BaseColumns interface, class Carros has acquired a _ID field, which is the Carros._ID seen at the beginning of the code posted in the question.

    
02.12.2014 / 23:29