pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

從列表視圖的edittext中搜索項(xiàng)目顯示錯(cuò)誤的結(jié)果

Search item in edittext from listview showing wrong result(從列表視圖的edittext中搜索項(xiàng)目顯示錯(cuò)誤的結(jié)果)
本文介紹了從列表視圖的edittext中搜索項(xiàng)目顯示錯(cuò)誤的結(jié)果的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時(shí)送ChatGPT賬號..

嘗試使用edittext搜索listview項(xiàng)目,點(diǎn)擊搜索項(xiàng)目返回錯(cuò)誤位置.

假設(shè)我從給定列表中搜索C",其中列表項(xiàng)如下A",B",C",D",CC",并得到正確的搜索結(jié)果,但一旦點(diǎn)擊搜索項(xiàng)目,它返回錯(cuò)誤的項(xiàng)目位置.

這里 Edittext addTextChangedListener :

edtSe??arch.addTextChangedListener(new TextWatcher() {public void onTextChanged(CharSequence s, int start, int before,整數(shù)計(jì)數(shù)){adapter.getFilter().filter(s);適配器.notifyDataSetChanged();}public void beforeTextChanged(CharSequence s, int start, int count,整數(shù)之后){}public void afterTextChanged(Editable s) {}});}

<塊引用>

完整的課程代碼:

 包 com.tomar.xyz;導(dǎo)入 java.util.ArrayList;導(dǎo)入 java.util.HashMap;導(dǎo)入 java.util.List;導(dǎo)入android.app.Activity;導(dǎo)入android.content.Intent;導(dǎo)入android.os.Bundle;導(dǎo)入 android.text.Editable;導(dǎo)入 android.text.TextWatcher;導(dǎo)入android.view.View;導(dǎo)入 android.widget.AdapterView;導(dǎo)入 android.widget.ArrayAdapter;導(dǎo)入 android.widget.EditText;導(dǎo)入 android.widget.ListView;導(dǎo)入 android.widget.SimpleAdapter;導(dǎo)入 android.widget.TextView;導(dǎo)入 android.widget.AdapterView.OnItemClickListener;公共類 Tabtwo 擴(kuò)展 Activity 實(shí)現(xiàn) OnItemClickListener {列表視圖列表視圖;文本視圖 txt;ArrayAdapter<字符串>適配器;//搜索編輯文本編輯文本編輯搜索;//存儲國家名稱的字符串?dāng)?shù)組String[] countries = new String[] { "Admin Cost", "Affinity Diagram",分析"、評估成本"、利益相關(guān)者評估"、};//整數(shù)數(shù)組指向存儲在/res/drawable-ldpi/中的圖像int[] flags = new int[] { R.drawable.admin, R.drawable.affinity,R.drawable.analysis,R.drawable.appraisal,R.drawable.assessment,};/** 在第一次創(chuàng)建活動(dòng)時(shí)調(diào)用.*/@覆蓋公共無效 onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.tabtwo);//列表中的每一行存儲國家名稱、貨幣和標(biāo)志列表<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 };//實(shí)例化一個(gè)適配器來存儲每個(gè)項(xiàng)目//R.layout.listview_layout 定義每一項(xiàng)的布局最終的 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);//將適配器設(shè)置為 listViewlistView.setAdapter(適配器);listView.setTextFilterEnabled(true);listView.setOnItemClickListener(this);edtSe??arch.addTextChangedListener(new TextWatcher() {public void onTextChanged(CharSequence s, int start, int before,整數(shù)計(jì)數(shù)){adapter.getFilter().filter(s);適配器.notifyDataSetChanged();}public void beforeTextChanged(CharSequence s, int start, int count,整數(shù)之后){}public void afterTextChanged(Editable s) {}});}@覆蓋public void onItemClick(AdapterView

解決方案

將你的 onItemClick 更改為:

 @Overridepublic void onItemClick(AdapterView

當(dāng)您過濾列表時(shí),項(xiàng)目會重新排列并且它們的位置會發(fā)生變化,因此請根據(jù)該位置上的值而不是位置導(dǎo)航到下一個(gè)活動(dòng).

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.

這篇關(guān)于從列表視圖的edittext中搜索項(xiàng)目顯示錯(cuò)誤的結(jié)果的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Cut, copy, paste in android(在android中剪切、復(fù)制、粘貼)
android EditText blends into background(android EditText 融入背景)
Change Line Color of EditText - Android(更改 EditText 的線條顏色 - Android)
EditText showing numbers with 2 decimals at all times(EditText 始終顯示帶 2 位小數(shù)的數(shù)字)
Changing where cursor starts in an expanded EditText(更改光標(biāo)在展開的 EditText 中的開始位置)
EditText, adjustPan, ScrollView issue in android(android中的EditText,adjustPan,ScrollView問題)
主站蜘蛛池模板: 骨密度仪-骨密度测定仪-超声骨密度仪-骨龄测定仪-天津开发区圣鸿医疗器械有限公司 | 婚博会2024时间表_婚博会门票领取_婚博会地址-婚博会官网 | 等离子表面处理机-等离子表面活化机-真空等离子清洗机-深圳市东信高科自动化设备有限公司 | 压缩空气冷冻式干燥机_吸附式干燥机_吸干机_沪盛冷干机 | 立式硫化罐-劳保用品硫化罐-厂家直销-山东鑫泰鑫硫化罐厂家 | 宁夏档案密集柜,智能密集柜,电动手摇密集柜-盛隆柜业宁夏档案密集柜厂家 | 世界箱包品牌十大排名,女包小众轻奢品牌推荐200元左右,男包十大奢侈品牌排行榜双肩,学生拉杆箱什么品牌好质量好 - Gouwu3.com | 酒店品牌设计-酒店vi设计-酒店标识设计【国际级】VI策划公司 | 智成电子深圳tdk一级代理-提供TDK电容电感贴片蜂鸣器磁芯lambda电源代理经销,TDK代理商有哪些TDK一级代理商排名查询。-深圳tdk一级代理 | 有机废气处理-rto焚烧炉-催化燃烧设备-VOC冷凝回收装置-三梯环境 | 沟盖板_复合沟盖板厂_电力盖板_树脂雨水篦子-淄博拜斯特 | 南京泽朗生物科技有限公司-液体饮料代加工_果汁饮料代加工_固体饮料代加工 | 东莞螺杆空压机_永磁变频空压机_节能空压机_空压机工厂批发_深圳螺杆空压机_广州螺杆空压机_东莞空压机_空压机批发_东莞空压机工厂批发_东莞市文颖设备科技有限公司 | 轴流风机-鼓风机-离心风机-散热风扇-罩极电机,生产厂家-首肯电子 | 语料库-提供经典范文,文案句子,常用文书,您的写作得力助手 | TwistDx恒温扩增-RAA等温-Jackson抗体-默瑞(上海)生物科技有限公司 | 建筑资质代办-建筑资质转让找上海国信启航 | 河北中仪伟创试验仪器有限公司是专业生产沥青,土工,水泥,混凝土等试验仪器的厂家,咨询电话:13373070969 | 分子精馏/精馏设备生产厂家-分子蒸馏工艺实验-新诺舜尧(天津)化工设备有限公司 | 大米加工设备|大米加工机械|碾米成套设备|大米加工成套设备-河南成立粮油机械有限公司 | b2b网站大全,b2b网站排名,找b2b网站就上地球网 | 铝扣板-铝方通-铝格栅-铝条扣板-铝单板幕墙-佳得利吊顶天花厂家 elisa试剂盒价格-酶联免疫试剂盒-猪elisa试剂盒-上海恒远生物科技有限公司 | 风信子发稿-专注为企业提供全球新闻稿发布服务 | 耐高温硅酸铝板-硅酸铝棉保温施工|亿欧建设工程 | 空气能采暖,热泵烘干机,空气源热水机组|设备|厂家,东莞高温热泵_正旭新能源 | 环氧铁红防锈漆_环氧漆_无溶剂环氧涂料_环氧防腐漆-华川涂料 | 全自动在线分板机_铣刀式在线分板机_曲线分板机_PCB分板机-东莞市亿协自动化设备有限公司 | 北京中创汇安科贸有限公司 | 钢板仓,大型钢板仓,钢板库,大型钢板库,粉煤灰钢板仓,螺旋钢板仓,螺旋卷板仓,骨料钢板仓 | 真空搅拌机-行星搅拌机-双行星动力混合机-广州市番禺区源创化工设备厂 | 世界箱包品牌十大排名,女包小众轻奢品牌推荐200元左右,男包十大奢侈品牌排行榜双肩,学生拉杆箱什么品牌好质量好 - Gouwu3.com | 步入式高低温测试箱|海向仪器| 天津力值检测-天津管道检测-天津天诚工程检测技术有限公司 | 包塑丝_高铁绑丝_地暖绑丝_涂塑丝_塑料皮铁丝_河北创筹金属丝网制品有限公司 | 成都中天自动化控制技术有限公司 | 论文查重_免费论文查重_知网学术不端论文查重检测系统入口_论文查重软件 | 齿轮减速机_齿轮减速电机-VEMT蜗轮蜗杆减速机马达生产厂家瓦玛特传动瑞环机电 | AGV无人叉车_激光叉车AGV_仓储AGV小车_AGV无人搬运车-南昌IKV机器人有限公司[官网] | 长沙印刷厂-包装印刷-画册印刷厂家-湖南省日大彩色印务有限公司 青州搬家公司电话_青州搬家公司哪家好「鸿喜」青州搬家 | 机床主轴维修|刀塔维修|C轴维修-常州翔高精密机械有限公司 | 泰来华顿液氮罐,美国MVE液氮罐,自增压液氮罐,定制液氮生物容器,进口杜瓦瓶-上海京灿精密机械有限公司 |