如何在片段回收站视图中使用搜索栏

时间:2018-09-07 08:52:11

标签: android json android-fragments android-recyclerview

///基本上,我想在片段中添加搜索栏。 在此片段中,recyclerview中的所有值都已获得,但其中有300多个值,我必须通过搜索栏进行过滤。 // addTextListener()函数用于相同功能,但会出现错误

package Fragment;

    import android.app.FragmentManager;
    import android.app.FragmentTransaction;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.text.Editable;
    import android.text.TextWatcher;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.EditText;

    import com.example.rspl_aequm.operation.Api;
    import com.example.rspl_aequm.operation.R;

    import java.util.ArrayList;
    import java.util.List;

    import Adapter.Tab1_Adapter;
    import Pojo.Tab1_Pojo;
    import retrofit2.Call;
    import retrofit2.Callback;
    import retrofit2.Response;
    import retrofit2.Retrofit;
    import retrofit2.converter.gson.GsonConverterFactory;

    public class Tab1Fragment extends Fragment {

        private List<Tab1_Pojo> itemlist = new ArrayList<>();
        private RecyclerView recyclerView;
        private RecyclerView.LayoutManager layoutManger;
        private RecyclerView.Adapter adapter;
        public static String Url = "http://35.201.138.23:8080/Operation_Myql/Api/Store_Status.jsp";
        public EditText search;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            final View rootView = inflater.inflate(R.layout.fragment_one, container, false);

            //return inflater.inflate(R.layout.fragment_one, container, false);

            recyclerView = (RecyclerView) rootView.findViewById(R.id.recycleview);
            recyclerView.setHasFixedSize(true);

           // final ProgressDialog loading = ProgressDialog.show(getActivity()," Fetching data...","please wait....",false,false);

            layoutManger = new LinearLayoutManager(getActivity());
            recyclerView.setLayoutManager(layoutManger);
            search = (EditText)rootView.findViewById( R.id.search);

            addTextListener();
            getStoreList();

            return rootView;

        }


        public void getStoreList() {
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(Api.BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create()) //Here we are using the GsonConverterFactory to directly convert json data to object
                    .build();

            Api api = retrofit.create(Api.class);

            Call<Tab1_Pojo> call = api.getStoreList("Created");

            call.enqueue(new Callback<Tab1_Pojo>() {
                @Override
                public void onResponse(Call<Tab1_Pojo> call, Response<Tab1_Pojo> response) {
                    List<Tab1_Pojo>tab1_pojos = response.body().getStore_Status();
                    recyclerView.setAdapter(new Tab1_Adapter(Tab1Fragment.this,tab1_pojos));
                }

                @Override
                public void onFailure(Call<Tab1_Pojo> call, Throwable t) {
                }
            });
        }

        public void sendtoanotehrfragment(Bundle bundle){
            System.out.println(" im Here ");
            FragmentManager manager=getActivity().getFragmentManager();
            StoreDetails fragment = new StoreDetails();
            FragmentTransaction ft = manager.beginTransaction()
                    .replace(R.id.cont,fragment,"fragment");
            ft.addToBackStack("fragment");
            fragment.setArguments(bundle);
            ft.commit();
        }



    public void addTextListener() {
        search.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable s) {
            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            public void onTextChanged(CharSequence query, int start, int before, int count) {

                query = query.toString().toLowerCase();

                final List<Tab1_Pojo> filteredList = new ArrayList<>();

                for (int i = 0; i < itemlist.size(); i++) {

                    final String id = itemlist.get(i).getSTORE_ID();
                    final String name = itemlist.get(i).getSTORE_NAME();
                    final String status = itemlist.get(i).getSTATUS();



                    if (id.toLowerCase().contains(query)||name.toLowerCase().contains(query)||status.toLowerCase().contains(query)) {

                        filteredList.add(itemlist.get(i));
                    }


    //

                }
                recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
// here in below line am getting error
                adapter = new Tab1_Adapter(filteredList, getContext(),getFragmentManager());
                recyclerView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }

        });
    }
    }

//请帮助我。 我在这里附了完整的片段 谢谢

2 个答案:

答案 0 :(得分:0)

我建议您在您的“适配器”中实现Filterable并重写getFilter(),以便在其中放置过滤逻辑。之后,您可以直接在OnTextChanged中执行此操作。

adapter.getFilter().filter(query);

查看此链接,了解如何在适配器中实现Filterable:

https://www.androidhive.info/2017/11/android-recyclerview-with-search-filter-functionality/

答案 1 :(得分:0)

首先,我建议您将数据存储在某个地方(数据库),然后再搜索数据库(FTS-> https://developer.android.com/training/search/search

我可以建议您的第二个想法是将业务逻辑(REST调用,搜索)移出UI(ActivityFragment)之外,并像{ {1}}和Android Architecture Components