XCBOSA 3 سال پیش
والد
کامیت
2a7b8f4cdd

+ 44 - 6
src/main/java/com/example/mywordbook/MainActivity.java

@@ -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();
+    }
 }

+ 1 - 0
src/main/java/com/example/mywordbook/WordCardDelegate.java

@@ -6,5 +6,6 @@ import com.example.mywordbook.db.Word;
 public interface WordCardDelegate {
 
     void onDelete(Word word, int position);
+    void onEdit(Word word, int position);
 
 }

+ 6 - 3
src/main/java/com/example/mywordbook/WordCardPrototype.java

@@ -1,6 +1,7 @@
 package com.example.mywordbook;
 
 import android.view.View;
+import android.widget.Button;
 import android.widget.TextView;
 import androidx.annotation.NonNull;
 import androidx.recyclerview.widget.RecyclerView;
@@ -13,15 +14,17 @@ public class WordCardPrototype extends RecyclerView.ViewHolder {
     public WordCardDelegate delegate;
 
     private TextView name, translate, example;
-    private FloatingActionButton deleteButton;
+    private Button btnDelete, btnEdit;
 
     public WordCardPrototype(@NonNull View itemView) {
         super(itemView);
         name = itemView.findViewById(R.id.wordName);
         translate = itemView.findViewById(R.id.wordTranslate);
         example = itemView.findViewById(R.id.wordExample);
-        deleteButton = itemView.findViewById(R.id.deleteButton);
-        deleteButton.setOnClickListener(view -> delegate.onDelete(word, getAdapterPosition()));
+        btnDelete = itemView.findViewById(R.id.btnDelete);
+        btnDelete.setOnClickListener(view -> delegate.onDelete(word, getAdapterPosition()));
+        btnEdit = itemView.findViewById(R.id.btnEdit);
+        btnEdit.setOnClickListener(view -> delegate.onEdit(word, getAdapterPosition()));
     }
 
     public void updateWord(Word word) {

+ 13 - 1
src/main/java/com/example/mywordbook/db/DBAdapter.java

@@ -17,6 +17,7 @@ public class DBAdapter extends RecyclerView.Adapter<WordCardPrototype> {
     private DBHelper dbConnection;
     private WordCardDelegate delegate;
     private List<Word> wordList;
+    private String filterText = "";
 
     public DBAdapter(DBHelper dbConnection, WordCardDelegate delegate) {
         this.dbConnection = dbConnection;
@@ -24,7 +25,9 @@ public class DBAdapter extends RecyclerView.Adapter<WordCardPrototype> {
     }
 
     public void updateWordList() {
-        wordList = dbConnection.getWords();
+        if (filterText.length() == 0)
+            wordList = dbConnection.getWords();
+        else wordList = dbConnection.getWords(filterText);
     }
 
     public void initWordList() {
@@ -53,4 +56,13 @@ public class DBAdapter extends RecyclerView.Adapter<WordCardPrototype> {
         initWordList();
         return wordList.size();
     }
+
+    public String getFilterText() {
+        return filterText;
+    }
+
+    public void setFilterText(String filterText) {
+        this.filterText = filterText;
+        updateWordList();
+    }
 }

+ 15 - 2
src/main/java/com/example/mywordbook/db/DBHelper.java

@@ -26,8 +26,7 @@ public class DBHelper extends SQLiteOpenHelper {
         onCreate(sqLiteDatabase);
     }
 
-    public List<Word> getWords() {
-        Cursor cursor = getWritableDatabase().rawQuery("select * from words", new String[0]);
+    public List<Word> getWords(Cursor cursor) {
         List<Word> words = new ArrayList<Word>();
         while (cursor.moveToNext()) {
             int word_id = cursor.getColumnIndex("id"),
@@ -43,6 +42,14 @@ public class DBHelper extends SQLiteOpenHelper {
         return words;
     }
 
+    public List<Word> getWords() {
+        return getWords(getWritableDatabase().rawQuery("select * from words", new String[0]));
+    }
+
+    public List<Word> getWords(String filter) {
+        return getWords(getWritableDatabase().rawQuery("select * from words where name like ?", new String[] { "%" + filter + "%" }));
+    }
+
     public void addWord(PreparedWord word) {
         getWritableDatabase().execSQL("insert into words(name, translate, example) values (?, ?, ?)",
                 new Object[] { word.getWord(), word.getTranslate(), word.getExample() });
@@ -52,4 +59,10 @@ public class DBHelper extends SQLiteOpenHelper {
         getWritableDatabase().execSQL("delete from words where id=?", new Object[] { id });
     }
 
+    public void editWord(int id, PreparedWord word) {
+        getWritableDatabase().execSQL("update words set name=?, translate=?, example=? where id=?", new Object[] {
+                word.getWord(), word.getTranslate(), word.getExample(), id
+        });
+    }
+
 }

+ 19 - 5
src/main/res/layout/activity_main.xml

@@ -6,12 +6,26 @@
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         tools:context=".MainActivity">
+
+    <EditText
+        android:id="@+id/findInput"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:ems="10"
+        android:inputType="textPersonName"
+        android:text="Name"
+        app:layout_constraintEnd_toStartOf="@+id/wordListView"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toTopOf="parent" />
+
     <androidx.recyclerview.widget.RecyclerView
-            android:layout_width="match_parent"
-            android:layout_height="match_parent" app:layout_constraintTop_toTopOf="parent"
-            app:layout_constraintBottom_toBottomOf="parent"
-            app:layout_constraintEnd_toEndOf="parent"
-            app:layout_constraintStart_toStartOf="parent" android:id="@+id/wordListView"/>
+        android:id="@+id/wordListView"
+        android:layout_width="match_parent"
+        android:layout_height="0dp"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toBottomOf="@+id/findInput" />
     <com.google.android.material.floatingactionbutton.FloatingActionButton
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"

+ 47 - 30
src/main/res/layout/dialog_add_word.xml

@@ -1,40 +1,57 @@
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-              android:orientation="vertical"
-              android:layout_width="match_parent"
-              android:layout_height="match_parent">
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:orientation="vertical">
 
     <TextView
-            android:text="请输入单词:"
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content" android:id="@+id/textView" android:layout_weight="1"
-            android:textSize="20dp" android:gravity="bottom|left"/>
+        android:id="@+id/textView"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_weight="1"
+        android:gravity="bottom|left"
+        android:text="请输入单词:"
+        android:textSize="20dp" />
+
     <EditText
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:inputType="textPersonName"
-            android:ems="10"
-            android:id="@+id/addWordName" android:layout_weight="2"/>
+        android:id="@+id/addWordName"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_weight="2"
+        android:ems="10"
+        android:inputType="textPersonName" />
+
     <TextView
-            android:text="请输入单词的含义:"
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content" android:id="@+id/textView2" android:layout_weight="1"
-            android:textSize="20dp" android:gravity="bottom|left"/>
+        android:id="@+id/textView2"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_weight="1"
+        android:gravity="bottom|left"
+        android:text="请输入单词的含义:"
+        android:textSize="20dp" />
+
     <EditText
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:inputType="textPersonName"
-            android:ems="10"
-            android:id="@+id/addWordTranslate" android:layout_weight="2"/>
+        android:id="@+id/addWordTranslate"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_weight="2"
+        android:ems="10"
+        android:inputType="textPersonName" />
+
     <TextView
-            android:text="请输入单词的例句:"
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content" android:id="@+id/textView3" android:layout_weight="1"
-            android:textSize="20dp" android:gravity="bottom|left"/>
+        android:id="@+id/textView3"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_weight="1"
+        android:gravity="bottom|left"
+        android:text="请输入单词的例句:"
+        android:textSize="20dp" />
+
     <EditText
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:inputType="textPersonName"
-            android:ems="10"
-            android:id="@+id/addWordExample" android:layout_weight="2"/>
+        android:id="@+id/addWordExample"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_weight="2"
+        android:ems="10"
+        android:inputType="textPersonName" />
 </LinearLayout>

+ 52 - 21
src/main/res/layout/layout_word_card.xml

@@ -4,30 +4,61 @@
                                    android:layout_width="match_parent"
                                    android:layout_height="250dp" app:cardCornerRadius="15dp"
 >
+
+    <Button
+        android:id="@+id/btnDelete"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_gravity="right"
+        android:layout_marginTop="3dp"
+        android:layout_marginRight="10dp"
+        android:text="删除"
+        app:backgroundTint="#C36A6A" />
+
+    <Button
+        android:id="@+id/btnEdit"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_gravity="left"
+        android:layout_marginLeft="10dp"
+        android:layout_marginTop="3dp"
+        android:text="修改"
+        app:backgroundTint="#7B8A69" />
+
     <LinearLayout
-            android:orientation="vertical"
-            android:layout_width="match_parent"
-            android:layout_height="match_parent" android:background="#BA8E7A">
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:background="#BA8E7A"
+        android:orientation="vertical">
+
         <TextView
-                android:text="Word Name Placeholder"
-                android:layout_width="match_parent"
-                android:layout_height="0dp" android:id="@+id/wordName" android:textSize="25sp"
-                android:gravity="center" android:layout_weight="1"/>
+            android:id="@+id/wordName"
+            android:layout_width="match_parent"
+            android:layout_height="0dp"
+            android:layout_weight="1"
+            android:gravity="center"
+            android:text="Word Name Placeholder"
+            android:textSize="25sp" />
+
         <TextView
-                android:text="Word Translate Placeholder"
-                android:layout_width="match_parent"
-                android:layout_height="0dp" android:id="@+id/wordTranslate" android:layout_weight="1"
-                android:textSize="16sp" android:gravity="center|left" android:layout_margin="20dp"/>
+            android:id="@+id/wordTranslate"
+            android:layout_width="match_parent"
+            android:layout_height="0dp"
+            android:layout_margin="20dp"
+            android:layout_weight="1"
+            android:gravity="center|left"
+            android:text="Word Translate Placeholder"
+            android:textSize="16sp" />
+
         <TextView
-                android:text="Word Example Placeholder"
-                android:layout_width="match_parent"
-                android:layout_height="0dp" android:id="@+id/wordExample" android:layout_weight="1"
-                android:textSize="15sp" android:gravity="center|left" android:layout_margin="20dp"/>
+            android:id="@+id/wordExample"
+            android:layout_width="match_parent"
+            android:layout_height="0dp"
+            android:layout_margin="20dp"
+            android:layout_weight="1"
+            android:gravity="center|left"
+            android:text="Word Example Placeholder"
+            android:textSize="15sp" />
     </LinearLayout>
-    <com.google.android.material.floatingactionbutton.FloatingActionButton
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:clickable="true" app:srcCompat="@android:drawable/ic_delete"
-            android:id="@+id/deleteButton" android:layout_weight="1" android:layout_gravity="right|top"
-            android:layout_margin="10dp"/>
+
 </androidx.cardview.widget.CardView>