将应用程序中使用的URL存储为静态的最佳方法是什么?
例如,我想保存每次打开应用程序时调用的Feed的URL。
最好的方法是什么?
一个例子很棒!
这是我的Feed.class
public class Feed implements Comparable<Feed> {
static private HashMap<String, Feed> FEEDS=new HashMap<String,Feed>();
static{
addFeed("Example", "http://www.example.xom/rssfedd/")
}
private String name;
private String url;
public static Feed addFeed(String name, String url){
Feed result = new Feed(name,url);
addFeed(result);
return(result);
}
private static void addFeed(Feed feed){
FEEDS.put(feed.getKey(), feed);
}
public static ArrayList<Feed>getFeeds(){
ArrayList<Feed> result = new ArrayList<Feed>(FEEDS.values());
Collections.sort(result);
return(result);
}
private Feed getFeed(String key){
return(FEEDS.get(key));
}
private Feed(String name, String url){
this.name = name;
this.url = url;
}
public String getKey(){
return(toString());
}
public String toString(){
return(name);
}
public String getUrl(){
return(url);
}
public int compareTo(Feed another) {
return (toString().compareTo(another.toString()));
}
}
=
答案 0 :(得分:3)
对于在SQLite DB中存储,最好的方法是创建一个扩展SQLiteOpenHelper
超类的类。在这个类中,您将定义要在数据库中插入的函数,删除,创建表格和等等。
最简单的方法是 - 如果你知道数据库的结构(如果你只在一个表格中存储URL),你可以使用SQLiteBrowser创建数据库。如果你感兴趣,我可以给你代码和浏览器的链接。好的运气!!
更新:
public class DBAdapter extends SQLiteOpenHelper {
public static final String DATABASE_PATH = "data/data/com.Server_1/database";
public static final String DATABASE_NAME = "gps_date";
public static final String TABLE_1 = "user";
public static final String TABLE_2 = "route";
public static final String TABLE_3 = "data";
public static final String KEY_ROWID_1 = "_id";
public static final String KEY_USER = "user";
public static final String KEY_ROWID_2 = "_id";
public static final String KEY_SURSA = "sursa";
public static final String KEY_DESTINATIE = "destinatie";
public static final String KEY_DATE = "date";
public static final String KEY_USER_ID = "user_id";
public static final String KEY_ROWID_3 = "_id";
public static final String KEY_LONGITUDE = "longitude";
public static final String KEY_LATITUDE = "latitude";
public static final String KEY_SPEED = "speed";
public static final String KEY_TIME = "time";
public static final String KEY_USER_ID_ = "user_id";
public boolean t = false;
public SQLiteDatabase db;
private final Context myContext;
public DBAdapter(Context context) {
super(context, DATABASE_NAME, null, 1);
this.myContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
createDB();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w("SqlHelper", "Upgrading database from version " + oldVersion
+ " to " + newVersion + ", which will destroy all old data");
onCreate(db);
}
public void createDatabase() {
createDB();
}
private void createDB() {
boolean dbExist = DBExists();
if (!dbExist) {
copyDBFromResource();
}
}
private boolean DBExists() {
SQLiteDatabase db = null;
try {
String databasePath = DATABASE_PATH + DATABASE_NAME;
db = SQLiteDatabase.openDatabase(databasePath, null,
SQLiteDatabase.OPEN_READWRITE);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
db.setVersion(1);
} catch (SQLiteException e) {
Log.e("SqlHelper", "database not found");
}
if (db != null) {
db.close();
}
return db != null ? true : false;
}
private void copyDBFromResource() {
InputStream inputStream = null;
OutputStream outStream = null;
String dbFilePath = DATABASE_PATH + DATABASE_NAME;
try {
inputStream = myContext.getAssets().open(DATABASE_NAME);
outStream = new FileOutputStream(dbFilePath);
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outStream.write(buffer, 0, length);
}
outStream.flush();
outStream.close();
inputStream.close();
} catch (IOException e) {
throw new Error("Problem copying database from resource file.");
}
}
public void openDataBase() throws SQLException {
String myPath = DATABASE_PATH + DATABASE_NAME;
db = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close() {
if (db != null)
db.close();
super.close();
}
public long insertData1(String user) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_USER, user);
return db.insert(TABLE_1, null, initialValues);
}
public long insertData2(String sursa, String destinatie, String date,
int user_id) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_SURSA, sursa);
initialValues.put(KEY_DESTINATIE, destinatie);
initialValues.put(KEY_DATE, date);
initialValues.put(KEY_USER_ID, user_id);
return db.insert(TABLE_2, null, initialValues);
}
public long insertData3(String longitude, String latitude, float speed,
String time, int user_id) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_LONGITUDE, longitude);
initialValues.put(KEY_LATITUDE, latitude);
initialValues.put(KEY_SPEED, speed);
initialValues.put(KEY_TIME, time);
initialValues.put(KEY_USER_ID_, user_id);
return db.insert(TABLE_3, null, initialValues);
}
public Cursor getCursor(String prefix) {
String[] args = { prefix };
String[] asColumnsToReturn = new String[] {KEY_ROWID_2,
KEY_SURSA, KEY_DESTINATIE, KEY_DATE, KEY_USER_ID};
Cursor mCursor = db.query(TABLE_2, asColumnsToReturn,
"sursa like '' || ? || '%'", args, null, null, "sursa", null);
return mCursor;
}
public Cursor getCursor1(String prefix1)
{
Cursor mCursor=db.query(TABLE_1, new String[] {KEY_ROWID_1, KEY_USER},KEY_ROWID_1 + "=" + prefix1,
null,null,null,null);
if(mCursor != null)
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor getCursor2(String TABLE_NAME, String prefix2) {
Cursor mCursor = db.query(TABLE_NAME, new String[] { KEY_ROWID_2,
KEY_SURSA, KEY_DESTINATIE, KEY_DATE, KEY_USER_ID }, KEY_USER_ID
+ "=" + prefix2, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor getCursor3(String TABLE_NAME, String prefix) {
Cursor mCursor = db.query(TABLE_NAME,
new String[] { KEY_ROWID_3, KEY_LONGITUDE, KEY_LATITUDE,
KEY_SPEED, KEY_TIME, KEY_USER_ID }, KEY_USER_ID + "="
+ prefix, null, null, null, null);
return mCursor;
}
public Cursor getViteze(int prefix, String arg) {
Cursor mCursor = db.query(TABLE_3,
new String[] { KEY_ROWID_3, KEY_LONGITUDE, KEY_LATITUDE,
KEY_SPEED, KEY_TIME, KEY_USER_ID_ }, KEY_SPEED + ">"
+ prefix + " AND " + KEY_USER_ID_ + "=" + arg, null,
null, null, KEY_SPEED + " DESC");
return mCursor;
}
public boolean updateRoute(int rowId, String destinatie) {
ContentValues args = new ContentValues();
args.put(KEY_DESTINATIE, destinatie);
return db.update(TABLE_2, args, KEY_ROWID_2 + "=" + rowId, null) > 0;
}
public Cursor getAllData() {
return db.query(TABLE_1, new String[] { KEY_ROWID_1, KEY_USER }, null,
null, null, null, null);
}
public Cursor getAllData2() {
return db.query(TABLE_2, new String[] { KEY_ROWID_2, KEY_SURSA,
KEY_DATE }, null, null, null, null, null);
}
public String[] getAllfromDB(String TABLE_NAME, String KEY) {
Cursor cursor = this.db.query(TABLE_NAME, new String[] { KEY }, null,
null, null, null, null);
if (cursor.getCount() > 0) {
String[] str = new String[cursor.getCount()];
int i = 0;
while (cursor.moveToNext()) {
str[i] = cursor.getString(cursor.getColumnIndex(KEY));
i++;
}
return str;
} else {
return new String[] {};
}
}
public boolean deleteRoute(long rowID) {
return db.delete(TABLE_2, KEY_ROWID_2 + "=" + rowID, null) > 0;
}
public boolean deleteDateRoute(long rowID) {
return db.delete(TABLE_3, KEY_USER_ID_ + "=" + rowID, null) > 0;
}
以下是链接:http://sourceforge.net/projects/sqlitebrowser/
Now, a few things about how to use this:
1.Create the structure of the DB using the browser and put it in the assets folder of your application.
2. You will have to change the variable `DATABASE_PATH` accoring to your application, this way:
"data/data/`name of the package`/database"
3.You will also have to change the name of your `DATABASE_NAME`.
4.The code I sent you is done for three tabels, you will have to adapt it to your needs.
Good luck!
答案 1 :(得分:0)
您的问题是“如何使用数据库存储数据?” (在这种情况下,您真的会将问题与这些不必要的细节混淆)或者您只是问“我如何存储URL?”。
如果后者......一个URL只是文本,那么只需将其存储为文本字符串。
如果是前者,http://www.vogella.de/articles/AndroidSQLite/article.html可能有帮助。
答案 2 :(得分:0)
如果您只需要存储网址,则不需要数据库。
使用Activity.getSharedPreferences()
或Context.openFileInput(filename)
/ Context.openFileOutput(filename, mode)
应该没问题。
您只需使用一些面向文件的简短代码来存储和检索您的URL。