|
@@ -2,6 +2,8 @@ package com.example.mywordbook;
|
|
|
|
|
|
import android.content.Context;
|
|
|
import android.content.DialogInterface;
|
|
|
+import android.text.Editable;
|
|
|
+import android.text.TextWatcher;
|
|
|
import android.view.LayoutInflater;
|
|
|
import android.view.View;
|
|
|
import android.widget.EditText;
|
|
@@ -17,10 +19,11 @@ import com.example.mywordbook.db.PreparedWord;
|
|
|
import com.example.mywordbook.db.Word;
|
|
|
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
|
|
|
|
|
-public class MainActivity extends AppCompatActivity implements WordCardDelegate {
|
|
|
+public class MainActivity extends AppCompatActivity implements WordCardDelegate, TextWatcher {
|
|
|
|
|
|
private RecyclerView wordListView;
|
|
|
private FloatingActionButton plusButton;
|
|
|
+ private EditText findInput;
|
|
|
private DBHelper db;
|
|
|
private DBAdapter wordAdapter;
|
|
|
|
|
@@ -31,6 +34,9 @@ public class MainActivity extends AppCompatActivity implements WordCardDelegate
|
|
|
db = new DBHelper(this, null);
|
|
|
wordListView = findViewById(R.id.wordListView);
|
|
|
plusButton = findViewById(R.id.plusButton);
|
|
|
+ findInput = findViewById(R.id.findInput);
|
|
|
+ findInput.setText("");
|
|
|
+ findInput.addTextChangedListener(this);
|
|
|
wordAdapter = new DBAdapter(db, this);
|
|
|
wordListView.setAdapter(wordAdapter);
|
|
|
wordListView.setLayoutManager(new LinearLayoutManager(this));
|
|
@@ -43,27 +49,40 @@ public class MainActivity extends AppCompatActivity implements WordCardDelegate
|
|
|
return (int) (dpValue * scale + 0.5f);
|
|
|
}
|
|
|
|
|
|
- private void plusButtonClicked() {
|
|
|
+ interface FinishCallBack {
|
|
|
+ void finish();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void showDialog(PreparedWord word, FinishCallBack finishCallBack) {
|
|
|
View view = LayoutInflater.from(this).inflate(R.layout.dialog_add_word, null, false);
|
|
|
EditText wordName = view.findViewById(R.id.addWordName),
|
|
|
wordTranslate = view.findViewById(R.id.addWordTranslate),
|
|
|
wordExample = view.findViewById(R.id.addWordExample);
|
|
|
+ wordName.setText(word.getWord());
|
|
|
+ wordTranslate.setText(word.getTranslate());
|
|
|
+ wordExample.setText(word.getExample());
|
|
|
new AlertDialog.Builder(this)
|
|
|
.setTitle("新建单词")
|
|
|
.setView(view)
|
|
|
.setPositiveButton("确定", (dialogInterface, i) -> {
|
|
|
- PreparedWord word = new PreparedWord();
|
|
|
word.setWord(wordName.getText().toString());
|
|
|
word.setTranslate(wordTranslate.getText().toString());
|
|
|
word.setExample(wordExample.getText().toString());
|
|
|
- db.addWord(word);
|
|
|
- this.wordAdapter.updateWordList();
|
|
|
- this.wordAdapter.notifyDataSetChanged();
|
|
|
+ finishCallBack.finish();
|
|
|
})
|
|
|
.setNegativeButton("取消",null)
|
|
|
.show();
|
|
|
}
|
|
|
|
|
|
+ private void plusButtonClicked() {
|
|
|
+ PreparedWord word = new PreparedWord();
|
|
|
+ showDialog(word, () -> {
|
|
|
+ db.addWord(word);
|
|
|
+ this.wordAdapter.updateWordList();
|
|
|
+ this.wordAdapter.notifyDataSetChanged();
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
protected void onDestroy() {
|
|
|
super.onDestroy();
|
|
@@ -76,4 +95,23 @@ public class MainActivity extends AppCompatActivity implements WordCardDelegate
|
|
|
wordAdapter.updateWordList();
|
|
|
wordAdapter.notifyItemRemoved(position);
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onEdit(Word word, int position) {
|
|
|
+ showDialog(word, () -> {
|
|
|
+ db.editWord(word.getId(), word);
|
|
|
+ this.wordAdapter.updateWordList();
|
|
|
+ this.wordAdapter.notifyItemChanged(position);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { }
|
|
|
+
|
|
|
+ @Override public void afterTextChanged(Editable editable) { }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
|
|
|
+ wordAdapter.setFilterText(charSequence.toString());
|
|
|
+ wordAdapter.notifyDataSetChanged();
|
|
|
+ }
|
|
|
}
|