从列表视图的edittext中搜索项目显示错误的结果 [英] Search item in edittext from listview showing wrong result

查看:14
本文介绍了从列表视图的edittext中搜索项目显示错误的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用edittext搜索listview项目,点击搜索项目返回错误位置.

假设我从给定列表中搜索C",其中列表项如下A",B",C",D",CC",并得到正确的搜索结果,但一旦点击搜索项目,它返回错误的项目位置.

这里 Edittext addTextChangedListener :

edtSe​​arch.addTextChangedListener(new TextWatcher() {public void onTextChanged(CharSequence s, int start, int before,整数计数){adapter.getFilter().filter(s);适配器.notifyDataSetChanged();}public void beforeTextChanged(CharSequence s, int start, int count,整数之后){}public void afterTextChanged(Editable s) {}});}

<块引用>

完整的课程代码:

 包 com.tomar.xyz;导入 java.util.ArrayList;导入 java.util.HashMap;导入 java.util.List;导入android.app.Activity;导入android.content.Intent;导入android.os.Bundle;导入 android.text.Editable;导入 android.text.TextWatcher;导入android.view.View;导入 android.widget.AdapterView;导入 android.widget.ArrayAdapter;导入 android.widget.EditText;导入 android.widget.ListView;导入 android.widget.SimpleAdapter;导入 android.widget.TextView;导入 android.widget.AdapterView.OnItemClickListener;公共类 Tabtwo 扩展 Activity 实现 OnItemClickListener {列表视图列表视图;文本视图 txt;ArrayAdapter<字符串>适配器;//搜索编辑文本编辑文本编辑搜索;//存储国家名称的字符串数组String[] countries = new String[] { "Admin Cost", "Affinity Diagram",分析"、评估成本"、利益相关者评估"、};//整数数组指向存储在/res/drawable-ldpi/中的图像int[] flags = new int[] { R.drawable.admin, R.drawable.affinity,R.drawable.analysis,R.drawable.appraisal,R.drawable.assessment,};/** 在第一次创建活动时调用.*/@覆盖公共无效 onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.tabtwo);//列表中的每一行存储国家名称、货币和标志列表<HashMap<字符串,字符串>>aList = new ArrayList<HashMap<String, String>>();for (int i = 0; i < 4; i++) {HashMap<字符串,字符串>hm = new HashMap();hm.put("txt", 国家[i]);hm.put("flag", Integer.toString(flags[i]));aList.add(hm);}//Hashmap 中使用的键String[] from = { "flag", "txt" };//listview_layout 中视图的IDint[] to = { R.id.flag, R.id.txt };//实例化一个适配器来存储每个项目//R.layout.listview_layout 定义每一项的布局最终的 SimpleAdapter 适配器 = new SimpleAdapter(getBaseContext(),aList, R.layout.listview_layout, from, to);//获取 main.xml 布局文件的 listview 的引用ListView listView = (ListView) findViewById(R.id.listview);edtSe​​arch = (EditText) findViewById(R.id.Search_box);txt = (TextView) findViewById(R.id.txt);listView.setOnItemClickListener(this);//将适配器设置为 listViewlistView.setAdapter(适配器);listView.setTextFilterEnabled(true);listView.setOnItemClickListener(this);edtSe​​arch.addTextChangedListener(new TextWatcher() {public void onTextChanged(CharSequence s, int start, int before,整数计数){adapter.getFilter().filter(s);适配器.notifyDataSetChanged();}public void beforeTextChanged(CharSequence s, int start, int count,整数之后){}public void afterTextChanged(Editable s) {}});}@覆盖public void onItemClick(AdapterView

解决方案

将你的 onItemClick 更改为:

 @Overridepublic void onItemClick(AdapterView

当您过滤列表时,项目会重新排列并且它们的位置会发生变化,因此请根据该位置上的值而不是位置导航到下一个活动.

Trying to search a listview items using edittext, where clicking on searched item returning wrong position.

Supposed i search "C" from given list where list item is as follows "A","B","C","D","CC", and got the correct search result but once making the click on search item, It returning the wrong item position.

Here Edittext addTextChangedListener :

edtSearch.addTextChangedListener(new TextWatcher() {

            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                adapter.getFilter().filter(s);
                adapter.notifyDataSetChanged();

            }

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

            }

            public void afterTextChanged(Editable s) {
            }
        });
    }

Complete class code:

    package com.tomar.xyz;

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

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class Tabtwo extends Activity implements OnItemClickListener {

    ListView listView;
    TextView txt;

    ArrayAdapter<String> adapter;

    // Search EditText
    EditText edtSearch;

    // Array of strings storing country names
    String[] countries = new String[] { "Admin Cost", "Affinity Diagram",
            "Analyse", "Apprasal Costs", "Assessment of Stakeholders",
              };

    // Array of integers points to images stored in /res/drawable-ldpi/
    int[] flags = new int[] { R.drawable.admin, R.drawable.affinity,
            R.drawable.analysis, R.drawable.appraisal, R.drawable.assessment,
              };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabtwo);

        // Each row in the list stores country name, currency and flag
        List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();

        for (int i = 0; i < 4; i++) {
            HashMap<String, String> hm = new HashMap<String, String>();
            hm.put("txt", countries[i]);

            hm.put("flag", Integer.toString(flags[i]));
            aList.add(hm);
        }

        // Keys used in Hashmap
        String[] from = { "flag", "txt" };

        // Ids of views in listview_layout
        int[] to = { R.id.flag, R.id.txt };

        // Instantiating an adapter to store each items
        // R.layout.listview_layout defines the layout of each item
        final SimpleAdapter adapter = new SimpleAdapter(getBaseContext(),
                aList, R.layout.listview_layout, from, to);

        // Getting a reference to listview of main.xml layout file
        ListView listView = (ListView) findViewById(R.id.listview);
        edtSearch = (EditText) findViewById(R.id.Search_box);
        txt = (TextView) findViewById(R.id.txt);
        listView.setOnItemClickListener(this);
        // Setting the adapter to the listView
        listView.setAdapter(adapter);

        listView.setTextFilterEnabled(true);
        listView.setOnItemClickListener(this);
        edtSearch.addTextChangedListener(new TextWatcher() {

            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                adapter.getFilter().filter(s);
                adapter.notifyDataSetChanged();

            }

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

            }

            public void afterTextChanged(Editable s) {
            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
            long arg3) {
        // TODO Auto-generated method stub
        if (position == 0) {
            Intent int0 = new Intent(getApplicationContext(), Admincost.class);
            startActivity(int0);
        }

        if (position == 1) {
            Intent int1 = new Intent(getApplicationContext(), Affinity.class);
            startActivity(int1);
        }
        if (position == 2) {
            Intent int2 = new Intent(getApplicationContext(), Analyse.class);
            startActivity(int2);
        }

        if (position == 3) {
            Intent int3 = new Intent(getApplicationContext(),
                    ApprasalCosts.class);
            startActivity(int3);
        }
        if (position == 4) {
            Intent int1 = new Intent(getApplicationContext(), Assessment.class);
            startActivity(int1);
        } }
    }
}

解决方案

Change your onItemClick to:

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
            long arg3) {
        // TODO Auto-generated method 

        if (((String)adapter.getItem(position)).equals("Admin Cost")) {
            Intent int0 = new Intent(getApplicationContext(), Admincost.class);
            startActivity(int0);
        }
          .........................

    }

When you filter your list, items get rearranged and thier position change so navigate to next activity based on the value on that position instead of position.

这篇关于从列表视图的edittext中搜索项目显示错误的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆