列表视图片段活动中的筛选选项

时间:2017-10-20 04:48:37

标签: android listview search fragment addtextchangedlistener

朋友,我想在片段活动中添加搜索选项列表视图,但我无法从addTextChangedListener访问列表视图适配器。你能帮我吗。我找不到网上的答案,你能不能帮我解决这个.data加载部分正常工作。我想通过代码过滤数据。

enter image description here

   public class FindPeopleFragment extends Fragment implements  AdapterView.OnItemClickListener {
        private static final String TAG_CONTACTS = "contacts";
        private static final String TAG_ID = "GUID";
        private static final String TAG_NAME = "GUNO";
        private static final String TAG_Code = "ShipNo";
        private static final String TAG_ADDRESS = "Description";
        private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
        private static final int CAMERA_CAPTURE_VIDEO_REQUEST_CODE = 200;
        public static final int MEDIA_TYPE_IMAGE = 1;
        public static final int MEDIA_TYPE_VIDEO = 2;

        private static final String IMAGE_DIRECTORY_NAME = "Hello Camera";
       // private static final int RESULT_OK = 1 ;
       // private static final int RESULT_CANCELED = 2 ;

        private Uri fileUri; // file url to store image/video

        ImageView imgPreview;
        private VideoView videoPreview;

        private String baid = "";
        private PopupWindow pwindo;
        Button btndeliver;
        Button btnreject;
        Button btncannot;
        Button btnother;
        Button btnClosePopup;
        EditText inputSearch;

      //  Button btnCallPopup;
      //  Button  btnwtsappPopup;
        public String pnumb;
        public String CusMobileNo;


        // contacts JSONArray
        JSONArray contacts = null;

        // Hashmap for ListView
        ArrayList<HashMap<String, String>> contactList;


        public FindPeopleFragment(){}

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

            if (android.os.Build.VERSION.SDK_INT > 9) {
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                StrictMode.setThreadPolicy(policy);
            }

            Movie movie = new Movie();
            baid=movie.getGUID();

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

            contactList = new ArrayList<HashMap<String, String>>();



            WebServiceCaller webServiceCaller = new WebServiceCaller();
            String Result = webServiceCaller.mydespatchDettails(baid);

            try {
                JSONObject Jasonobject = new JSONObject(Result);
                JSONArray Jarray = Jasonobject.getJSONArray("PendingDespatch");

                for (int i = 0; i < Jarray.length(); i++) {
                    JSONObject json_data = Jarray.getJSONObject(i);

                    String id = json_data.getString(TAG_ID);
                    String name = json_data.getString(TAG_NAME);
                    String code = json_data.getString(TAG_Code);
                    String address = json_data.getString(TAG_ADDRESS);

                    HashMap<String, String> contact = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    contact.put(TAG_ID, id);
                    contact.put(TAG_NAME, name);
                    contact.put(TAG_Code, code);
                    contact.put(TAG_ADDRESS, address);

                    // adding contact to contact list
                    contactList.add(contact);

                }
                ListView list = (ListView) rootView.findViewById(R.id.list);

                  final ListAdapter adapter = new SimpleAdapter(getActivity()
                        ,contactList,
                        (R.layout.list_item), new String[] { TAG_NAME,TAG_Code,TAG_ID
                        ,TAG_ADDRESS }, new int[] { R.id.code,
                        R.id.names, R.id.city,R.id.address });

                list.setAdapter(adapter);

                list.setOnItemClickListener(this);
                //list.setListAdapter(adapter);
                inputSearch = (EditText) rootView.findViewById(R.id.inputSearch);

                inputSearch.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence cs, int start, int count, int after) {

                      System.out.println("Text ["+cs+"]");
                     FindPeopleFragment.this.adapter.getFilter().filter(cs); 

    //***This is problem i cannot access adapter here..i want to to search by code***  





                        // FindPeopleFragment.TAG_Code..getFilter().filter(cs);
                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                    }
                });

            } catch (JSONException e) {
                e.printStackTrace();
            }
            return rootView;
        }

1 个答案:

答案 0 :(得分:0)

如何更改此行:

FindPeopleFragment.this.adapter.getFilter().filter(cs); 

成:

adapter.getFilter().filter(cs);

如何将AutoCompleteTextview用于搜索功能。

AutoCompleteTextView文档

Example