這是我用來獲取數據的一些代碼。這來自developer.android.com的官方Google教程。你可以使用SQLite數據庫中的數據或類似的東西進行搜索。
import android.provider.BaseColumns;
/**
* Created by User on 11/3/2016.
*/
public final class FeedReaderContract {
private FeedReaderContract(){}
// Inner class that defines table content
public static class FeedEntry implements BaseColumns{
public static final String TABLE_NAME = "clickdata";
public static final String COLUMN_NAME_CLICKS = "clicks";
public static final String COLUMN_NAME_TIME = "time";
}
}
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by User on 11/3/2016.
*/
public class FeedReaderDbHelper extends SQLiteOpenHelper {
private static final String INTEGER_TYPE = " INTEGER";
private static final String STRING_TYPE = " STRING";
private static final String COMMA_SEP = ",";
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + FeedReaderContract.FeedEntry.TABLE_NAME + " (" +
FeedReaderContract.FeedEntry._ID + " INTEGER PRIMARY KEY," +
FeedReaderContract.FeedEntry.COLUMN_NAME_CLICKS + INTEGER_TYPE + COMMA_SEP +
FeedReaderContract.FeedEntry.COLUMN_NAME_TIME + STRING_TYPE +")";
private static final String SQL_DELETE_ENTRIES =
"DROP TABLE IF EXISTS " + FeedReaderContract.FeedEntry.TABLE_NAME;
// 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 = "ClickData.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);
}
}
MainActivity:
private void saveData(int clicks, String time){
FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(this);
SQLiteDatabase db = mDbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_CLICKS, clicks);
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TIME, time);
long newRowId = db.insert(FeedReaderContract.FeedEntry.TABLE_NAME, null, values);
}
private void readFromFile(){
FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(this);
SQLiteDatabase db = mDbHelper.getWritableDatabase();
String[] projection = {
FeedReaderContract.FeedEntry.COLUMN_NAME_CLICKS,
FeedReaderContract.FeedEntry.COLUMN_NAME_TIME,
};
// Filter results WHERE "title" = 'My Title'
Cursor c = db.query(
FeedReaderContract.FeedEntry.TABLE_NAME, // The table to query
projection, // The columns to return
null, // The columns for the WHERE clause
null, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
null // The sort order
);
c.moveToFirst();
clickCount = new int[c.getCount()];
int i = 0;
while(c.moveToNext()){
int uname = c.getInt(c.getColumnIndexOrThrow(FeedReaderContract.FeedEntry.COLUMN_NAME_CLICKS));
clickCount[i] = uname;
i++;
}
dateCount = new String[c.getCount()];
int i2 = 0;
while(c.moveToNext()){
String uname = c.getString(c.getColumnIndexOrThrow(FeedReaderContract.FeedEntry.COLUMN_NAME_TIME));
dateCount[i2] = uname;
i2++;
}
}
希望這個代碼可以幫助你可以使其適應你需要做什麼。這些類在單獨的文件中,方法在MainActivity.java中。剛剛說過,當我第一次創建數據庫時,我遇到了困難,但有時您必須廢棄所有的代碼。有時候不要害怕這樣做。
很多解決我的問題的例子和方法的很好的答案。謝謝 – Blackess