久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

從列表視圖的edittext中搜索項目顯示錯誤的結果

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

問題描述

限時送ChatGPT賬號..

嘗試使用edittext搜索listview項目,點擊搜索項目返回錯誤位置.

假設我從給定列表中搜索C",其中列表項如下A",B",C",D",CC",并得到正確的搜索結果,但一旦點擊搜索項目,它返回錯誤的項目位置.

這里 Edittext addTextChangedListener :

edtSe??arch.addTextChangedListener(new TextWatcher() {public void onTextChanged(CharSequence s, int start, int before,整數(shù)計數(shù)){adapter.getFilter().filter(s);適配器.notifyDataSetChanged();}public void beforeTextChanged(CharSequence s, int start, int count,整數(shù)之后){}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 實現(xiàn) OnItemClickListener {列表視圖列表視圖;文本視圖 txt;ArrayAdapter<字符串>適配器;//搜索編輯文本編輯文本編輯搜索;//存儲國家名稱的字符串數(shù)組String[] countries = new String[] { "Admin Cost", "Affinity Diagram",分析"、評估成本"、利益相關者評估"、};//整數(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)建活動時調(diào)用.*/@覆蓋公共無效 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,整數(shù)計數(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

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

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中搜索項目顯示錯誤的結果的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關文檔推薦

Cut, copy, paste in android(在android中剪切、復制、粘貼)
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(更改光標在展開的 EditText 中的開始位置)
EditText, adjustPan, ScrollView issue in android(android中的EditText,adjustPan,ScrollView問題)
主站蜘蛛池模板: 91免费观看视频 | 亚洲综合激情网 | 黄色片网站免费 | 91亚洲视频 | 三级av片 | 色婷婷中文字幕 | 天堂在线中文资源 | www.毛片.com | 亚洲精品999 | 成年人黄色片 | 久久动态图 | 欧美9999 | 国产专区在线 | 免费看91| 日韩精品一区二区视频 | 天天插天天干 | 五月天婷婷在线观看 | 国产精品毛片一区视频播 | 中文字幕手机在线观看 | 毛片91 | 国产精品二| 欧美a在线观看 | 婷婷激情综合网 | 色一情一乱一乱一区91av | 欧美色影院 | 日韩一级免费视频 | 男女啪啪免费网站 | 一区二区三区在线免费观看 | 日韩精品在线一区 | 国产精品婷婷 | 午夜美女福利 | 日韩av在线网址 | 欧美在线性爱视频 | 亚洲国产第一页 | 国产一区二区在线免费 | 噜噜噜在线 | 中文字幕亚洲精品 | 成人免费高清 | 97国产精品人人爽人人做 | 亚洲午夜在线 | 在线观看日韩 |