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問題)
主站蜘蛛池模板: 不锈钢钢格栅板_热浸锌钢格板_镀锌钢格栅板_钢格栅盖板-格美瑞 | 上海APP开发-APP制作-APP定制开发-上海APP开发制作公司-咏熠科技 | 顶呱呱交易平台-行业领先的公司资产交易服务平台 | 耐高温电缆厂家-远洋高温电缆 | 微波消解仪器_智能微波消解仪报价_高压微波消解仪厂家_那艾 | 实验室pH计|电导率仪|溶解氧测定仪|离子浓度计|多参数水质分析仪|pH电极-上海般特仪器有限公司 | 手术室净化厂家-成都做医院净化工程的公司-四川华锐-15年特殊科室建设经验 | 脱硝喷枪-氨水喷枪-尿素喷枪-河北思凯淋环保科技有限公司 | 【连江县榕彩涂料有限公司】官方网站 | Eiafans.com_环评爱好者 环评网|环评论坛|环评报告公示网|竣工环保验收公示网|环保验收报告公示网|环保自主验收公示|环评公示网|环保公示网|注册环评工程师|环境影响评价|环评师|规划环评|环评报告|环评考试网|环评论坛 - Powered by Discuz! | 河南膏药贴牌-膏药代加工-膏药oem厂家-洛阳今世康医药科技有限公司 | 电销卡 防封电销卡 不封号电销卡 电话销售卡 白名单电销卡 电销系统 外呼系统 | 铝单板_铝窗花_铝单板厂家_氟碳包柱铝单板批发价格-佛山科阳金属 | 企典软件一站式企业管理平台,可私有、本地化部署!在线CRM客户关系管理系统|移动办公OA管理系统|HR人事管理系统|人力 | 交变/复合盐雾试验箱-高低温冲击试验箱_安奈设备产品供应杭州/江苏南京/安徽马鞍山合肥等全国各地 | 华夏医界网_民营医疗产业信息平台_民营医院营销管理培训 | 橡胶接头|可曲挠橡胶接头|橡胶软接头安装使用教程-上海松夏官方网站 | 济南网站建设_济南网站制作_济南网站设计_济南网站建设公司_富库网络旗下模易宝_模板建站 | 高精度-恒温冷水机-螺杆式冰水机-蒸发冷冷水机-北京蓝海神骏科技有限公司 | 中红外QCL激光器-其他连续-半导体连续激光器-筱晓光子 | 劳动法网-专业的劳动法和劳动争议仲裁服务网 | 南京租车,南京汽车租赁,南京包车,南京会议租车-南京七熹租车 | 飞扬动力官网-广告公司管理软件,广告公司管理系统,喷绘写真条幅制作管理软件,广告公司ERP系统 | 铝合金脚手架厂家-专注高空作业平台-深圳腾达安全科技 | 干式变压器厂_干式变压器厂家_scb11/scb13/scb10/scb14/scb18干式变压器生产厂家-山东科锐变压器有限公司 | 电子巡更系统-巡检管理系统-智能巡检【金万码】 | 美缝剂_美缝剂厂家_美缝剂加盟-地老板高端瓷砖美缝剂 | 加中寰球移民官网-美国移民公司,移民机构,移民中介,移民咨询,投资移民 | 骨灰存放架|骨灰盒寄存架|骨灰架厂家|智慧殡葬|公墓陵园管理系统|网上祭奠|告别厅智能化-厦门慈愿科技 | 捆扎机_气动捆扎机_钢带捆扎机-沈阳海鹞气动钢带捆扎机公司 | 台式恒温摇床价格_大容量恒温摇床厂家-上海量壹科学仪器有限公司 | 对夹式止回阀_对夹式蝶形止回阀_对夹式软密封止回阀_超薄型止回阀_不锈钢底阀-温州上炬阀门科技有限公司 | 亮化工程,亮化设计,城市亮化工程,亮化资质合作,长沙亮化照明,杰奥思【官网】 | 广域铭岛Geega(际嘉)工业互联网平台-以数字科技引领行业跃迁 | 美国查特CHART MVE液氮罐_查特杜瓦瓶_制造全球品质液氮罐 | 南京精锋制刀有限公司-纵剪机刀片_滚剪机刀片_合金刀片厂家 | 国产液相色谱仪-超高效液相色谱仪厂家-上海伍丰科学仪器有限公司 | 免费分销系统 — 分销商城系统_分销小程序开发 -【微商来】 | 办公室家具公司_办公家具品牌厂家_森拉堡办公家具【官网】 | 厦门ISO认证|厦门ISO9001认证|厦门ISO14001认证|厦门ISO45001认证-艾索咨询专注ISO认证行业 | 东莞动力锂电池保护板_BMS智能软件保护板_锂电池主动均衡保护板-东莞市倡芯电子科技有限公司 |