gpt4 book ai didi

java - 不能从另一个类访问字符串?

转载 作者:行者123 更新时间:2023-12-03 18:14:04 25 4
gpt4 key购买 nike

我正在关注android网站上的教程。 http://developer.android.com/training/basics/data-storage/databases.html#DefineContract

SQL_DATA_ENTRIES 在 FeedReaderDbHelper 中以红色点亮。如何从 FeedReaderDbHelper 访问它?我是使用 getter 和 setter 还是有更好的方法?

//FeedReaderDbHelper

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
* Created by Neil Armstrong on 6/5/2015.
*/
public class FeedReaderDbHelper extends SQLiteOpenHelper {
// If you change the database schema, you must increment the database version.
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "FeedReader.db";

public FeedReaderDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// This database is only a cache for online data, so its upgrade policy is
// to simply to discard the data and start over
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
}

//FeedReaderContract
import android.provider.BaseColumns;

/**
* Created by Neil Armstrong on 6/5/2015.
*/
public final class FeedReaderContract {
// To prevent someone from accidentally instantiating the contract class,
// give it an empty constructor.
public FeedReaderContract() {}

/* Inner class that defines the table contents */
public static abstract class FeedEntry implements BaseColumns {
public static final String TABLE_NAME = "entry";
public static final String COLUMN_NAME_ENTRY_ID = "entryid";
public static final String COLUMN_NAME_TITLE = "title";
public static final String COLUMN_NAME_SUBTITLE = "subtitle";
private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ",";
//this is what I am trying to get.
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + FeedEntry.TABLE_NAME + " (" +
FeedEntry._ID + " INTEGER PRIMARY KEY," +
FeedEntry.COLUMN_NAME_ENTRY_ID + TEXT_TYPE + COMMA_SEP +
FeedEntry.COLUMN_NAME_TITLE + TEXT_TYPE + COMMA_SEP +
// Any other options for the CREATE command
" )";

private static final String SQL_DELETE_ENTRIES =
"DROP TABLE IF EXISTS " + FeedEntry.TABLE_NAME;

}
}

最佳答案

当您使用 private String SQL_CREATE_ENTRIES FreeReaderContract 中的变量类,您应该使用 getter 和 setter 来检索/编辑它。

或者,您可以将它们设为私有(private)并使用
FreeREaderContract.SQL_CREATE_ENTRIES
来自 FreeReaderDbHelperstatic变量。 (假设它们在同一个包中)

关于java - 不能从另一个类访问字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30678224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com