所以我正在开发一个Android应用程序,用户可以在其中创建对象,以编程方式创建的2x [n] scrollview活动查看所有对象,并删除这些对象。
我删除时会出现问题。由于我以编程方式创建AllStreaks
,因此使用i获取我想要编辑的SQLite行的ID的方法很快就会崩溃。例如,如果我有2个对象,删除第二个对象,添加第三个对象,然后去删除第三个对象,我的程序仍将寻找第二个对象(已经不见了),因为我必须以编程方式创建(和因此重置)AllStreaks
。
我仍然不习惯使用SQLite,而且我不确定如何根据以编程方式创建的活动获取SQLite表的正确ID。
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);
prefs = getSharedPreferences("carter.streakly", MODE_PRIVATE);
editor = prefs.edit();
//createAllStreaks();
}
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;
}
@Override
public void onBackPressed(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Use other Buttons");
builder.setMessage("Please use the on-screen back buttons");
builder.show();
}
public void onResume(){
super.onResume();
createAllStreaks();
}
private void createAllStreaks(){
db = new DatabaseHelper(this);
mTableLayout = (TableLayout) findViewById(R.id.all_streak_table);
res = db.getAllData();
if (res.getCount() == 0) {
AlertDialog.Builder returnDiag = new AlertDialog.Builder(this);
returnDiag.setTitle("Empty");
returnDiag.setMessage("Go add some streaks, then come here!");
returnDiag.setNegativeButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(AllStreaks.this, MainActivity.class);
startActivity(intent);
}
});
returnDiag.show();
}
allStreaks = this;
streakArrayList = new ArrayList<>();
int counter = prefs.getInt("allCounter", 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++;
editor.putInt("allCounter", prefs.getInt("allCounter",0) + 1);
}
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);
try{
int startingPoint = prefs.getInt("idCount", 0);
}catch(NullPointerException e){
editor.putInt("idCount", 0);
}
i = 0;
while (i < res.getCount()) {
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(prefs.getInt("idCount", 0));
int idCount = prefs.getInt("idCount", 0) + 1;
editor.putInt("idCount", idCount).commit();
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.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);
yesButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
String passNameString = Integer.toString(btn.getId());
String activityName = streakArrayList.get(btn.getId()).getActivityName();
String activityCategory = streakArrayList.get(btn.getId()).getActivityCategory();
int newNum = (streakArrayList.get(btn.getId()).getDaysKept()) + 1;
boolean isUpdated = db.updateData(passNameString, activityName, activityCategory, newNum);
if (isUpdated == true){
Log.d("carter.streakly", passNameString + Integer.toString(newNum));
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) {
dialog.dismiss();
editor.putInt("currButtonID", btn.getId()+1).commit();
editor.putString("currButtonActivityName", streakArrayList.get(btn.getId()).getActivityName()).commit();
editor.putString("currButtonActivityCategory", streakArrayList.get(btn.getId()).getActivityCategory()).commit();
editor.putInt("currButtonDaysKept", streakArrayList.get(btn.getId()).getDaysKept()).commit();
Log.d("carter.streakly", btn.getId() + "" + prefs.getInt("currButtonID", 999) + prefs.getString("currButtonActivityName", "") + prefs.getString("currButtonActivityCategory", "") +
prefs.getInt("currButtonDaysKept", 999));
Intent intent = new Intent(AllStreaks.this, EnlargedActivity.class);
startActivity(intent);
}
});
}
});
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();
backHomeButton = (Button)findViewById(R.id.back_home_button);
backHomeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(AllStreaks.this, MainActivity.class);
startActivity(intent);
}
});
}
}
EditStreak
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_streak);
prefs = getSharedPreferences("carter.streakly", Context.MODE_PRIVATE);
editor = prefs.edit();
db = new DatabaseHelper(this);
res = db.getAllData();
if(res.getCount() ==0) {
//show message
showMessage("Error", "Nothing found");
return;
}
/*
Gson gson = new Gson();
String json = prefs.getString("doneList", "");
final ArrayList<Integer> doneList = gson.fromJson(json, ArrayList.class);
if (doneList == null){
doneList.add(0);
}*/
editStreak = this;
final int passName = prefs.getInt("passName", 0);
final String passNameString = Integer.toString(passName);
Log.d("carter.streakly", passNameString);
streakArrayList = new ArrayList<>();
streakIcon = new EditText(this);
doneButton = (Button) findViewById(R.id.edit_done_button);
editBackground = (RelativeLayout) findViewById(R.id.edit_background);
deleteStreakButton = (Button) findViewById(R.id.delete_streak_buton);
streakIcon = (EditText)findViewById(R.id.edit_streak_name);
categoryIcon = (EditText) findViewById(R.id.edit_streak_category);
streakDaysKept = (EditText) findViewById(R.id.edit_days_kept);
streakIcon.setGravity(Gravity.CENTER);
streakIcon.setTextSize(30);
//res.getInt(res.getColumnIndex("ID"));
ArrayList doneList = new ArrayList();
int i = 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))));
}
streakIcon.setText(prefs.getString("currButtonActivityName", ""));
streakDaysKept.setText(Integer.toString(prefs.getInt("currButtonDaysKept", 0)));
categoryIcon.setText(prefs.getString("currButtonActivityCategory", ""));
doneButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
editor.putString("currButtonActivityName", streakIcon.getText().toString()).commit();
editor.putString("currButtonActivityCategory", categoryIcon.getText().toString()).commit();
editor.putInt("currButtonDaysKept", Integer.parseInt(streakDaysKept.getText().toString().trim())).commit();
String updateID = Integer.toString(prefs.getInt("currButtonID", 0));
String updateName = prefs.getString("currButtonActivityName", "");
String updateCategory = prefs.getString("currButtonActivityCategory", "");
int updateDaysKept = prefs.getInt("currButtonDaysKept", 0);
boolean isUpdated = db.updateData(updateID, updateName, updateCategory, updateDaysKept);
if (isUpdated == true){
Log.d("carter.streakly", "AFTER SUCCESS: ID: " + prefs.getInt("currButtonID", 0) + " Name: " + prefs.getString("currButtonActivityName", "") + " Category: " +
prefs.getString("currButtonActivityCategory", "") + " Days Kept: " + prefs.getInt("currButtonDaysKept", 9));
Intent intent = new Intent(EditStreak.this, EnlargedActivity.class);
startActivity(intent);
finish();
}
else{
Toast.makeText(EditStreak.this, "Data not Updated", Toast.LENGTH_LONG);
}
}
});
deleteStreakButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String deleteID = Integer.toString(prefs.getInt("currButtonID", 0));
int num = prefs.getInt("currButtonID", 0);
Log.d("carter.streakly", "COUNT: " + res.getCount());
checkDoneList(num);
//int doneListAdder = prefs.getInt("currButtonID", 0);
//Log.d("DELETE ID: ", "" + doneListAdder);
//doneList.add(doneListAdder);
//addIntToDoneList(doneList, doneListAdder);
//checkDoneList(doneList, (prefs.getInt("currButtonID", 0))-1);
Log.d("carter.streakly", "BEFORE DELETION: " + deleteID);
Integer deletedRows = db.deleteData(deleteID);
if (deletedRows > 0){
editor.putInt(""+deleteID, Integer.parseInt(deleteID)).commit();
Log.d("carter.streakly", "AFTER DELETION: " + deleteID);
//streakArrayList.remove(passName);
/*
Gson gson = new Gson();
String json = gson.toJson(doneList);
editor.putString("doneList", json).commit();*/
db.vacuum();
Intent intent = new Intent(EditStreak.this, AllStreaks.class);
startActivity(intent);
finish();
}
else{
Toast.makeText(EditStreak.this,"Data not Deleted", Toast.LENGTH_LONG);
}
}
});
}
private int checkDoneList(int deleteNum){
for (int j = deleteNum; j < res.getCount(); j++){
if (streakArrayList.get(j).getActivityName().equals("") || streakArrayList.get(j).getActivityName() == null){
deleteNum++;
}
}
return deleteNum;
}
private void addIntToDoneList(ArrayList<Integer> doneList, int doneListAdder){
doneList.add(doneListAdder);
}
public void onPause(){
super.onPause();
finish();
}
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 EditStreak getInstance(){
return editStreak;
}
}