0

我正在开发一个使用TextWatcher过滤列表的 android 应用程序。

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

在这张图片中...您可以看到未过滤的列表,其中阿司匹林 10mg 在位置 (0)

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

这是过滤列表...它包含所有包含“C”的项目...当我单击第一项(在过滤列表中的位置 [0] 处)时,它将从未过滤列表位置 [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();
                                }
                            });
                        }
                    }
4

0 回答 0