1. 程式人生 > >SearchView(檢索後,不管是否滿足檢索條件,列表中的第一個數始終是list.get(0)的值,的問題解決)

SearchView(檢索後,不管是否滿足檢索條件,列表中的第一個數始終是list.get(0)的值,的問題解決)

問題的引起是在第4步中,view=null,加上就解決問題了。
1、MainActivity.xml:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context=".MainActivity"
    android:orientation="vertical">
    
    <SearchView
        android:id="@+id/searchview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:iconifiedByDefault="false"
        android:paddingRight="10dp"/>
    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"/>

</LinearLayout>

2、list_item,也就是列表顯示的樣子,在搜尋框中輸入文字,列表呈現的樣子


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/list_item_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="liebiao"
        android:textSize="18dp"/>
</LinearLayout>

3、MainActivity.java


public class MainActivity extends AppCompatActivity {
    String TAG="MainActivity";
    private ListView listView;
    private SearchView searchView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    private void initData() {
        searchView.setFocusable(false);
        final List<String> list=new ArrayList<>();
        list.add("張健");
        list.add("趙雲");
        list.add("劉備");
        list.add("新野");
        list.add("落鳳坡");
//        ListAdatper adatper=new ListAdatper(list);
        final SearchListAdapter adapter=new SearchListAdapter(list);
        listView.setAdapter(adapter);

//        searchlist.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arr));
        listView.setTextFilterEnabled(true);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            }
        });

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            // 當點選搜尋按鈕時觸發該方法
            @Override
            public boolean onQueryTextSubmit(String query) {
                return true;
            }

            // 當搜尋內容改變時觸發該方法
            @Override
            public boolean onQueryTextChange(String newText) {
                if (!TextUtils.isEmpty(newText)){
                    adapter.getFilter().filter(newText);
//                    searchlist.setFilterText(newText);
//                    searchlist.getFilter.performFiltering(newText);
                }else{
//                    searchlist.clearTextFilter();
                    adapter.getFilter().filter("");
                }
                return true;
            }
        });
    }

    private void initView() {
        listView=findViewById(R.id.list);
        searchView=findViewById(R.id.searchview);
    }
}

4、SearchListAdapter


public class SearchListAdapter extends BaseAdapter {
    String TAG="Adapter";
    private List<String> list;
    private List<String> listdata;
    MyFilter mFilter ;
    public SearchListAdapter(List<String> list){
        this.list=list;
        listdata=list;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view=null;//這裡如果不寫,檢索後的列表中會始終顯示list.get(0)的值;花半天時間才調試出來
        if(view==null) {
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, viewGroup, false);
            TextView tv=view.findViewById(R.id.list_item_textview);
            tv.setText(list.get(i));
            Log.e(TAG, "getView: "+list.get(i) );
        }
        return view;
    }
    public Filter getFilter() {
        if (mFilter ==null){
            mFilter = new MyFilter();
        }
        return mFilter;
    }

    class MyFilter extends Filter{
        //我們在performFiltering(CharSequence charSequence)這個方法中定義過濾規則
        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {
            FilterResults result = new FilterResults();
            List<String> lists ;
            if (TextUtils.isEmpty(charSequence)){//當過濾的關鍵字為空的時候,我們則顯示所有的資料
                lists  = listdata;
            }else {//否則把符合條件的資料物件新增到集合中
                lists = new ArrayList<>();
                for (String data:listdata){
                    if (data.contains(charSequence)){ //要匹配的item中的view
                        lists.add(data);
                    }
                }
            }
            list=lists;

            result.values = lists; //將得到的集合儲存到FilterResults的value變數中
            result.count = lists.size();//將集合的大小儲存到FilterResults的count變數中
            return result;

        }
        //在publishResults方法中告訴介面卡更新介面
        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
            list = (List<String>)filterResults.values;
//            LogUtil.d("publishResults:"+filterResults.count);
            if (filterResults.count>0){
                notifyDataSetChanged();//通知資料發生了改變
//                LogUtil.d("publishResults:notifyDataSetChanged");
            }else {
                notifyDataSetInvalidated();//通知資料失效
//                LogUtil.d("publishResults:notifyDataSetInvalidated");
            }
        }
    }
}