我正在使用Android应用,其中一个活动从内置的SQLite数据库中收集所有数据,并以编程方式生成一个2x [n]的值表。当按下其中一个按钮时,系统会提示用户是否要更改特定列的参数。由于某种原因,一切都编译并正常运行(对话甚至解散,我只是在数据被正确插入时才发生),但实际上没有任何内容正在更新。
警告,因为我还是Android dev的新手,所以提前做了粗制滥用的代码。
AllStreaks.java
:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_streaks);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
db = new DatabaseHelper(this);
mTableLayout = (TableLayout) findViewById(R.id.all_streak_table);
res = db.getAllData();
if (res.getCount() == 0) {
//show message
showMessage("Error", "Nothing found");
return;
}
allStreaks = this;
streakArrayList = new ArrayList<>();
int counter = 0;
while (res.moveToNext()) {
streakArrayList.add(new Streak(Integer.parseInt(res.getString(0)), res.getString(1), res.getString(2), res.getString(3), Integer.parseInt(res.getString(4))));
counter++;
}
LinearLayout.LayoutParams btnParams = new LinearLayout.LayoutParams(200, 200);
btnParams.setMargins(200, 30, 80, 30);
LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ActionBar.LayoutParams.WRAP_CONTENT);
tvParams.setMargins(100, 0, 0, 0);
i = 0;
while (i < counter) {
if (i % 2 == 0) {
mTableRow = new TableRow(this);
mTableLayout.addView(mTableRow);
}
ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
mTableRow.addView(ll);
final Button btn = new Button(this);
btn.setText("" + streakArrayList.get(i).getDaysKept());
btn.setId(i);
if(streakArrayList.get(i).getActivityCategory() == null){
btn.setBackground(getResources().getDrawable(R.drawable.round_button));
}
else if (streakArrayList.get(i).getActivityCategory().equals("Health")) {
btn.setBackground(getResources().getDrawable(R.drawable.category_health_bubble));
}
else if (streakArrayList.get(i).getActivityCategory().equals("Mental")) {
btn.setBackground(getResources().getDrawable(R.drawable.category_mental_button));
}
else if (streakArrayList.get(i).getActivityCategory().equals("Personal")) {
btn.setBackground(getResources().getDrawable(R.drawable.category_personal_bubble));
}
else if (streakArrayList.get(i).getActivityCategory().equals("Professional")) {
btn.setBackground(getResources().getDrawable(R.drawable.category_professional_bubble));
}
else if (streakArrayList.get(i).getActivityCategory().equals("Social")) {
btn.setBackground(getResources().getDrawable(R.drawable.category_social_bubble));
}
else{
btn.setBackground(getResources().getDrawable(R.drawable.round_button));
}
//btn.setBackground(getResources().getDrawable(R.drawable.round_button));
//btn.setBackground(getResources().getDrawable(R.drawable.round_button));
btn.setLayoutParams(btnParams);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final Dialog dialog = new Dialog(AllStreaks.this);
dialog.setContentView(R.layout.dialog_complete_or_view);
dialog.show();
final TextView dialogMessage = (TextView) dialog.findViewById(R.id.select_complete_or_view);
final Button yesButton = (Button) dialog.findViewById(R.id.yes_completed);
final Button viewButton = (Button) dialog.findViewById(R.id.view_streak);
final Button backButton = (Button) dialog.findViewById(R.id.close_complete_or_view);
yesButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String passNameString = Integer.toString(btn.getId());
String activityName = streakArrayList.get(btn.getId()).getActivityName();
String activityCategory = streakArrayList.get(btn.getId()).getActivityCategory();
String dateStarted = streakArrayList.get(btn.getId()).getDateStarted();
int newNum = (streakArrayList.get(btn.getId()).getDaysKept()) + 1;
boolean isUpdated = db.updateData(passNameString, activityName, activityCategory, dateStarted, newNum);
if (isUpdated == true){
finish();
startActivity(getIntent());
dialog.dismiss();
}
else{
dialog.dismiss();
Toast.makeText(AllStreaks.this, "Data not Updated", Toast.LENGTH_LONG);
}
}
});
viewButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
editor.putInt("passName", btn.getId()).commit();
Intent intent = new Intent(AllStreaks.this, EnlargedActivity.class);
MainActivity.getInstance().finish();
startActivity(intent);
}
});
backButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
}
});
ll.addView(btn);
TextView tv = new TextView(this);
tv.setText(streakArrayList.get(i).getActivityName());
tv.setId(i);
tv.setGravity(Gravity.CENTER | Gravity.BOTTOM);
tv.setTextSize(20);
tv.setLayoutParams(tvParams);
ll.addView(tv);
i++;
}
streakCount = db.getAllData().getCount();
prefs = getSharedPreferences("carter.streakly", Context.MODE_PRIVATE);
editor = prefs.edit();
backButton = (Button)findViewById(R.id.back_home_button);
backButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MainActivity.getInstance().finish();
Intent intent = new Intent(AllStreaks.this, MainActivity.class);
startActivity(intent);
}
});
//streakCount = db.getAllData().getCount();
}
public void showMessage(String title, String message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
public static AllStreaks getInstance(){
return allStreaks;
}
}
DatabaseHelper.java
:
public class DatabaseHelper extends SQLiteOpenHelper{
public static final String DATABASE_NAME = "streaks.db"; // Name of DB
public static final String TABLE_NAME = "streak_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "STREAKNAME";
public static final String COL_3 = "STREAKCATEGORY";
public static final String COL_4 = "DATESTARTED";
public static final String COL_5 = "DAYSKEPT";
public DatabaseHelper(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,STREAKNAME TEXT,STREAKCATEGORY TEXT,DATESTARTED TEXT,DAYSKEPT INTEGER);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
public boolean insertData(String STREAKNAME, String STREAKCATEGORY, String DATESTARTED, int DAYSKEPT){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, STREAKNAME);
contentValues.put(COL_3, STREAKCATEGORY);
contentValues.put(COL_4, DATESTARTED);
contentValues.put(COL_5, DAYSKEPT);
long result = db.insert(TABLE_NAME, null, contentValues);
if(result == -1){
return false;
} else {
db.close();
return true;
}
}
public Cursor getAllData(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("SELECT * FROM "+TABLE_NAME,null);
return res;
}
public boolean updateData(String id, String streakName, String streakCategory, String dateStarted, int daysKept){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1, id);
contentValues.put(COL_2, streakName);
contentValues.put(COL_3, streakCategory);
contentValues.put(COL_4, dateStarted);
contentValues.put(COL_5, daysKept);
db.update(TABLE_NAME, contentValues, "id = ?", new String[] {id});
return true;
}
public Integer deleteData(String id){
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_NAME, "id = ?", new String[] {id});
}
}