Skip to content

Commit

Permalink
Long press to modify the history record name
Browse files Browse the repository at this point in the history
  • Loading branch information
ZnDong committed Sep 19, 2024
1 parent 85a8eba commit 7d543ee
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 16 deletions.
11 changes: 11 additions & 0 deletions app/src/main/java/com/zcshou/database/DataBaseHistoryLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,15 @@ public static void saveHistoryLocation(SQLiteDatabase sqLiteDatabase, ContentVal
XLog.e("DATABASE: insert error");
}
}

// 修改历史记录名称
public static void updateHistoryLocation(SQLiteDatabase sqLiteDatabase, String locID, String location) {
try{
ContentValues contentValues = new ContentValues();
contentValues.put(DB_COLUMN_LOCATION, location);
sqLiteDatabase.update(TABLE_NAME, contentValues, DB_COLUMN_ID + " = ?", new String[]{locID});
} catch (Exception e){
XLog.e("DATABASE: update error");
}
}
}
74 changes: 58 additions & 16 deletions app/src/main/java/com/zcshou/gogogo/HistoryActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import androidx.appcompat.app.AlertDialog;
import androidx.preference.PreferenceManager;

import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
Expand All @@ -19,6 +20,9 @@
import android.widget.SearchView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.PopupMenu;
import android.view.Gravity;

import java.math.BigDecimal;
import java.math.RoundingMode;
Expand Down Expand Up @@ -255,6 +259,40 @@ public boolean onQueryTextChange(String newText) {// 当搜索内容改变时触
});
}

private void showDeleteDialog(String locID) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("警告");
builder.setMessage("确定要删除该项历史记录吗?");
builder.setPositiveButton("确定", (dialog, whichButton) -> {
boolean deleteRet = deleteRecord(Integer.parseInt(locID));
if (deleteRet) {
GoUtils.DisplayToast(HistoryActivity.this, getResources().getString(R.string.history_delete_ok));
updateRecordList();
}
});
builder.setNegativeButton("取消", null);

builder.show();
}

private void showInputDialog(String locID, String name) {
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_TEXT);
input.setText(name);

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("名称");
builder.setView(input);
builder.setPositiveButton("确认", (dialog, whichButton) -> {
String userInput = input.getText().toString();
DataBaseHistoryLocation.updateHistoryLocation(mHistoryLocationDB, locID, userInput);
updateRecordList();
});
builder.setNegativeButton("取消", null);

builder.show();
}

private void initRecordListView() {
noRecordText = findViewById(R.id.record_no_textview);
mSearchLayout = findViewById(R.id.search_linear);
Expand All @@ -277,23 +315,27 @@ private void initRecordListView() {
});

mRecordListView.setOnItemLongClickListener((parent, view, position, id) -> {
new AlertDialog.Builder(HistoryActivity.this)
.setTitle("警告")//这里是表头的内容
.setMessage("确定要删除该项历史记录吗?")//这里是中间显示的具体信息
.setPositiveButton("确定",
(dialog, which) -> {
String locID = (String) ((TextView) view.findViewById(R.id.LocationID)).getText();
boolean deleteRet = deleteRecord(Integer.parseInt(locID));
PopupMenu popupMenu = new PopupMenu(HistoryActivity.this, view);
popupMenu.setGravity(Gravity.END | Gravity.BOTTOM);
popupMenu.getMenu().add("编辑");
popupMenu.getMenu().add("删除");

popupMenu.setOnMenuItemClickListener(item -> {
String locID = ((TextView) view.findViewById(R.id.LocationID)).getText().toString();
String name = ((TextView) view.findViewById(R.id.LocationText)).getText().toString();
switch (item.getTitle().toString()) {
case "编辑":
showInputDialog(locID, name);
return true;
case "删除":
showDeleteDialog(locID);
return true;
default:
return false;
}
});

if (deleteRet) {
GoUtils.DisplayToast(this, getResources().getString(R.string.history_delete_ok));
updateRecordList();
}
})
.setNegativeButton("取消",
(dialog, which) -> {
})
.show();
popupMenu.show();
return true;
});

Expand Down

0 comments on commit 7d543ee

Please sign in to comment.