TextWatcher过滤列表但不更新元素的位置

时间:2016-10-05 10:51:33

标签: android listview onitemclicklistener android-textwatcher

我正在开发一个Android应用程序,我使用 TextWatcher 来过滤列表。

当我在编辑文本中键入单词时,列表会获得过滤器,但是当我选择任何项目时,功能会从未过滤的列表位置中选择项目。 你可以在图片中看到。在第一张图片中......你可以看到未过滤的列表,其中阿司匹林10毫克在位置(0)

in this image... you can see unfiltered list where Aspirine 10mg in at position(0)

在第二张图片中...这是过滤列表...它包含所有包含'C'的项目...当我点击第一项(在过滤列表中的位置[0])时,它将从未过滤的项目中选择项目列表位置[0]

this is filtered list... it contains all items containing 'C' ...when i click on first item(at position[0] in filtered list) it will select the item from unfiltered list position[0]

请帮助我...我该如何解决...跟着你也可以看到我的代码。

 grabResults = (EditText) findViewById(R.id.grabed_et_search);

     grabResults.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //perform Search
                grabedDb.this.adapter.getFilter().filter(s);
        }
        @Override
        public void afterTextChanged(Editable s) {
        }
    });
JsonObjectRequest jreq = new JsonObjectRequest(Request.Method.GET, url,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    try {
                        int success = response.getInt("success");

                        if (success == 1) {
                            JSONArray ja = response.getJSONArray("mydrugs");

                            for (int i = 0; i < ja.length(); i++) {

                                JSONObject jobj = ja.getJSONObject(i);
                                HashMap<String, String> item = new HashMap<String, String>();
                                item.put(ITEM_ID, jobj.getString(ITEM_ID));
                                item.put(ITEM_NAME,
                                        jobj.getString(ITEM_NAME));

                                Item_List.add(item);

                            } // for loop ends

                            String[] from = { ITEM_ID, ITEM_NAME };
                            int[] to = { R.id.item_name, R.id.item_id };

                            adapter = new SimpleAdapter(
                                    getApplicationContext(), Item_List,
                                    R.layout.list_items, from, to);
                            final ListView myList = (ListView) findViewById(R.id.list_list_listView);
                            myList.setAdapter(adapter);
                            PD.dismiss();
//////// List Item Click Method Starts here ////////
                            myList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
                                @Override
                                public void onItemClick(AdapterView<?> parent, View view, int position,
                                                        long id) {
                                    HashMap<String, String> selectedItems = new HashMap<String, String>();
                                    selectedItems = Item_List.get(position);
                                    stock_list.add(selectedItems);

                                        Toast.makeText(getApplicationContext() , "Added To Cart " + stock_list  , Toast.LENGTH_LONG ).show();
                                }
                            });
                        }
                    }

0 个答案:

没有答案