Search code examples
javaandroideclipsesqlitesqliteopenhelper

Changing final attribut


I have the following problem. I have two classes copied together from various tutorials. In class one I have:

public static final String BUDGET_AUSGABENART = "mood";
public static final String BUDGET_ZEIT = "timeMillis";

In class BudgetRechnerAdapter it references back to this String I'm guessing:

long timeMillis = cursor.getLong(ciZeit);
        datum.setTime(timeMillis);

Anyway when I try to change the name of the String and change the name of the long variable and the setTime I'm getting an error on my android app. Also the String mood, which is not referencing to anything lets me run into an error when I change the naming.

Any idea where I can change the naming?

Please find the coding of the 2 classes below.

public class BudgetRechnerOpenHandler extends SQLiteOpenHelper {

    private static final String TAG = BudgetRechnerOpenHandler.class
            .getSimpleName();

    // Name und Version der Datenbank
    private static final String DATABASE_NAME = "budgetrechner.db";
    private static final int DATABASE_VERSION = 1;

    // Name und Attribute der Tabelle "Budget"
    public static final String _ID = "_id";
    public static final String TABELLE_NAME_BUDGET = "budget";
    public static final String BUDGET_ZEIT = "timeMillis";
    public static final String BUDGET_AUSGABENART = "mood";
    public static final String BUDGET_BETRAG = "betrag";

    // Konstanten für die Stimmungen
    public static final int BUDGET_ESSEN = 1;
    public static final int BUDGET_GETRAENK = 2;
    public static final int BUDGET_SONSTIGES = 3;


    // Tabelle Budget anlegen
    private static final String TABELLE_BUDGET_ERSTELLEN = "CREATE TABLE "
            + TABELLE_NAME_BUDGET + " (" + _ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT, " + BUDGET_ZEIT + " INTEGER, "
            + BUDGET_BETRAG + " INTEGER," + BUDGET_AUSGABENART + " INTEGER);";
    // Tabelle Budget löschen
    private static final String TABELLE_BUDGET_DROP = "DROP TABLE IF EXISTS "
            + TABELLE_NAME_BUDGET;

    BudgetRechnerOpenHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

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

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrade der Datenbank von Version " + oldVersion + " zu "
                + newVersion + "; alle Daten werden gelöscht");
        db.execSQL(TABELLE_BUDGET_DROP);
        onCreate(db);
    }

    public void insert(int art, String beschreibung, long zeit) {
        long rowId = -1;
        try {
            // Datenbank öffnen
            SQLiteDatabase db = getWritableDatabase();
            // die zu speichernden Werte
            ContentValues wert = new ContentValues();
            wert.put(BUDGET_AUSGABENART, art);
            wert.put(BUDGET_BETRAG, beschreibung);
            wert.put(BUDGET_ZEIT, zeit);
            // in die Tabelle Budget einfügen
            rowId = db.insert(TABELLE_NAME_BUDGET, null, wert);
        } catch (SQLiteException e) {
            Log.e(TAG, "insert()", e);
        } finally {
            Log.d(TAG, "insert(): rowId=" + rowId);
        }
    }

    public Cursor query() {
        // ggf. Datenbank öffnen
        SQLiteDatabase db = getWritableDatabase();
        return db.query(TABELLE_NAME_BUDGET, null, null, null, null, null,
                BUDGET_ZEIT + " DESC"
            );
    }

    public void update(long id, int ausgabe_art_zahl) {
        // ggf. Datenbank öffnen
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(BUDGET_AUSGABENART, ausgabe_art_zahl);
        int numUpdated = db.update(TABELLE_NAME_BUDGET, values, _ID + " = ?",
                new String[] { Long.toString(id) });
        Log.d(TAG, "update(): id=" + id + " -> " + numUpdated);
    }

    public void delete(long id) {
        // ggf. Datenbank öffnen
        SQLiteDatabase db = getWritableDatabase();
        int numDeleted = db.delete(TABELLE_NAME_BUDGET, _ID + " = ?",
                new String[] { Long.toString(id) });
        Log.d(TAG, "delete(): id=" + id + " -> " + numDeleted);
    }
}

public class BudgetRechnerAdapter extends CursorAdapter {

    private final Date datum;

    private static final DateFormat DF_DATE = SimpleDateFormat
            .getDateInstance(DateFormat.MEDIUM);
    private static final DateFormat DF_TIME = SimpleDateFormat
            .getTimeInstance(DateFormat.MEDIUM);

    private Integer betrag;

    private static final Integer DF_BETRAG =0;
    private LayoutInflater inflator;
    private int ciAusgabenArt, ciZeit, ciBetrag;

    public BudgetRechnerAdapter(Context context, Cursor c) {
        super(context, c);
        datum = new Date();
        betrag = null;
        inflator = LayoutInflater.from(context);
        ciAusgabenArt = c.getColumnIndex(BudgetRechnerOpenHandler.BUDGET_AUSGABENART);
        ciZeit = c.getColumnIndex(BudgetRechnerOpenHandler.BUDGET_ZEIT);
        ciBetrag = c.getColumnIndex(BudgetRechnerOpenHandler.BUDGET_BETRAG);

    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ImageView image = (ImageView) view.findViewById(R.id.icon);
        int art = cursor.getInt(ciAusgabenArt);
        TextView textView_art = (TextView) view.findViewById(R.id.text3);
        if (art == BudgetRechnerOpenHandler.BUDGET_ESSEN) {
            image.setImageResource(R.drawable.bild_essen);
            textView_art.setText("Essen");
        } else if (art == BudgetRechnerOpenHandler.BUDGET_GETRAENK) {
            image.setImageResource(R.drawable.bild_getraenk);
            textView_art.setText("Getränk");
        } else {
            image.setImageResource(R.drawable.bild_sonstiges);
            textView_art.setText("Sonstiges");
        }
        TextView textView_Datum = (TextView) view.findViewById(R.id.text1);
        TextView textView_Zeit = (TextView) view.findViewById(R.id.text2);

        long timeMillis = cursor.getLong(ciZeit);
        datum.setTime(timeMillis);
        textView_Datum.setText(DF_DATE.format(datum));
        textView_Zeit.setText(DF_TIME.format(datum));

        TextView textView_betrag = (TextView) view.findViewById(R.id.text4);
        int beschreibung = cursor.getInt(ciBetrag);
        betrag = beschreibung;
        textView_betrag.setText(betrag.toString());

    }

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

Solution

  • final attribute means it cant be changed.

    Simply get rid of the final in the variable if you want to change its value.

    Feel free to comment if you have side problem because removing the final attribute.