【问题标题】:filter option in list view fragment activity列表视图片段活动中的过滤选项
【发布时间】:2018-04-01 05:42:01
【问题描述】:

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

   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;
        }

【问题讨论】:

  • final ListAdapter adapter -> final SimpleAdapter adapter 当你这样做时 adapter.getFilter().filter(cs); 应该可以正常工作
  • Thnaks bro i woks..thanks lot..

标签: android listview search fragment addtextchangedlistener


【解决方案1】:

如何改变这一行:

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

进入:

adapter.getFilter().filter(cs);

如何为您的搜索功能使用 AutoCompleteTextview。

AutoCompleteTextView 文档

Example

【讨论】:

  • 亲爱的朋友,这里的适配器显示为红色下划线..如果我将适配器设为最终,getFilter() 显示为红色。我无法理解问题是什么..很好的帮助兄弟..我是堆栈两天后到这里
  • @KasunChaturanga 如果可行,您能接受我的回答吗?很高兴能帮上忙。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-24
  • 2015-05-20
  • 1970-01-01
相关资源
最近更新 更多