我有一个应用程序,用于检查我的业务。我希望看到15个最近的分数,所以我可以看到我们是变得越来越好。它应该工作的方式是,每次提交新的时,最新的一个将返回顶部并删除最旧的一个。
我的共享偏好类
public class SharedPreference {
public static final String PREFS_NAME = "AOP_PREFS";
public static final String PREFS_KEY = "AOP_PREFS_String";
public SharedPreference() {
super();
}
public void save(Context context, String text) {
SharedPreferences settings;
Editor editor;
//settings = PreferenceManager.getDefaultSharedPreferences(context);
settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); //1
editor = settings.edit(); //2
editor.putString(PREFS_KEY, text); //3
editor.commit(); //4
}
public String getValue(Context context) {
SharedPreferences settings;
String text;
//settings = PreferenceManager.getDefaultSharedPreferences(context);
settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
text = settings.getString(PREFS_KEY, null);
return text;
}
public void clearSharedPreference(Context context) {
SharedPreferences settings;
Editor editor;
//settings = PreferenceManager.getDefaultSharedPreferences(context);
settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
editor = settings.edit();
editor.clear();
editor.commit();
}
public void removeValue(Context context) {
SharedPreferences settings;
Editor editor;
settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
editor = settings.edit();
editor.remove(PREFS_KEY);
editor.commit();
}
}
我的View
就像这样
public class past extends AppCompatActivity {
// UI References
private TextView textTxt;
private String text;
private SharedPreference sharedPreference;
Activity context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_past);
sharedPreference = new SharedPreference();
findViewsById();
//Retrieve a value from SharedPreference
text = sharedPreference.getValue(context);
textTxt.setText(text);
}
private void findViewsById() {
textTxt = (TextView) findViewById(R.id.scores);
}
}
现在它显示的是最近的一个,没有别的。我怎么做到它将持有15个字符串?
答案 0 :(得分:0)
使用sqlite数据库,这是我可以展示的简单实施指南,有关谷歌搜索的更多信息。
您的DB_helper看起来如何,您可以在此处定义参数ect。
public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "yourDBname.db";
public static final String TABLE_NAME = "yourtableName";
public static final String COL_1 = "ID";
public static final String COL_2 = "FIRSTColumnName";
public static final String COL_3 = "SECONDColumnName";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table "+ TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, FIRSTColumnName INTEGER,SECONDColumnName INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS"+ TABLE_NAME);
onCreate(db);
}
public boolean insertData(int FIRSTColumnName, int SECONDColumnName){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2,FIRSTColumnName);
contentValues.put(COL_3,SECONDColumnName);
long reuslt = db.insert(TABLE_NAME,null,contentValues);
if (reuslt == -1)
return false;
else
return true;
}
}
然后在你的主应用程序中:
public void AddData(){
gumb_poslji.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int seekBarProgressValue = seekBar.getProgress();//for example
int seekBarProgressValue2 = seekBar2.getProgress();//for example
String str1 = String.valueOf(seekBarProgressValue);
String str2 = String.valueOf(seekBarProgressValue2);
String data = str1;
String data2 = str2;
boolean check_of_entry = myDb.insertData(data ,data2);
if (check_of_entry ==true)
Toast.makeText(MainActivity.this, "Success",Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this, "Try again",Toast.LENGTH_LONG).show();
}
});
}
如果您需要任何进一步的帮助,请告诉我。