这是我用来获取数据的一些代码。这来自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