本文介绍了从列表视图的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<字符串,字符串
解决方案

将你的 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) {
            }
        });
    }
    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
解决方案

Change your onItemClick to:

    @Override
    public void onItemClick(AdapterView<?

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中搜索项目显示错误的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-03 18:47
查看更多