안드로이드 리사이클러뷰3 필터링 - 전화번호부 앱

YAMAMAMO·2021년 12월 24일
0

오랜만에 전화번호부앱 관련 포스팅입니다.
리사이클러뷰의 아이템들 필터 기능입니다. TextWatcher 를 사용해서 에딧텍스트에 변경되는 값을 받아 리사이클러뷰의 아이템을 필터링합니다.

activity_main.xml

-에딧텍스트가 추가된다.

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

   <EditText
       android:id="@+id/edit_name"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:hint="이름을 입력해주세요."/>

    <EditText
        android:id="@+id/edit_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="핸드폰 번호를 입력해주세요."/>

    <Button
        android:id="@+id/btn_save"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="저장"/>

    <EditText
        android:id="@+id/edit_search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="검색"/>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</androidx.appcompat.widget.LinearLayoutCompat>

MainActivity

public class MainActivity extends AppCompatActivity implements TextWatcher {
    private String TAG = "MainActivity";
    private Context mContext;
    private ArrayList<Data> mArrayList, mFilteredList;//필터링할 데이터 담을 어레이리스트
    private Adapter mAdapter;
    private RecyclerView mRecyclerView;
    private EditText edit_name, edit_number, edit_search;
    private Button btn_save;
    private DBHelper mDbHelper;
    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_main);
        mContext = MainActivity.this;
        edit_name = findViewById (R.id.edit_name);
        edit_number = findViewById (R.id.edit_number);
        btn_save = findViewById (R.id.btn_save);
        mRecyclerView = findViewById (R.id.recycler);
        edit_search = findViewById (R.id.edit_search);
        edit_search.addTextChangedListener (this);

  ......

    }

........

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }
    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }
		
		//에딧텍스트에 입력받는 값을 감지한다.
    @Override
    public void afterTextChanged(Editable editable) {
        String searchText = edit_search.getText().toString();
        searchFilter(searchText);
    }
		
		//에딧텍스트 값을 받아 mFilteredList에 데이터를 추가한다. 
    public void searchFilter(String searchText) {
        mFilteredList.clear();

        for (int i = 0; i < mArrayList.size(); i++) {
            if (mArrayList.get(i).getName().toLowerCase().contains(searchText.toLowerCase())) {
                mFilteredList.add(mArrayList.get(i));
            }
        }
        mAdapter.listFilter (mFilteredList);
    }

}

Adapter

public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder>{
//    implements Filterable {
    private String TAG = "Adapter";
    private Context mContext;
    private ArrayList<Data> mArrayList; //데이터를 담을 어레이리스트
//    private ArrayList<Data> filterList;

    public Adapter(Context context, ArrayList<Data> arrayList) {
        this.mArrayList = arrayList;
        this.mContext =context;
//        this.filterList=arrayList;
    }

.......

    //매개변수로 필터링된 어레이리스트를 받아 notifyDataSetChanged()를 호출한다. 
    public void listFilter(ArrayList<Data> filteredList) {
        mArrayList = filteredList;
        notifyDataSetChanged();
    }

}




-에딧텍스트에 입력을 해서 리사이클러뷰가 필터링된 것을 확인할 수 있습니다.

전체코드
https://github.com/yamamamo/MobileNumberSample/tree/master

profile
안드로이드 개발자

0개의 댓글