Android的列表视图搜索过滤器 [英] Android Listview Search filter

查看:119
本文介绍了Android的列表视图搜索过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个列表视图搜索机器人。我发现有很多教程,做到这一点的地方 一个搜索栏放置在顶部,如果在框中键入查询结果进行过滤。

I am trying to make a list view search for Android. I have found many tutorials that do just that where a search-bar is placed at the top and if you type in the box the results get filtered.

在我的应用程序我想在给定的项目单击筛选完成后,我已经实现setOnItemClickListener。问题是,过滤,我想打开的变化和不正确的页面打开每个类的位置之后。我无法找到一个解决方案......

In my app I want to click on given items after filtering has been completed, I have implemented setOnItemClickListener. The issue is that after filtering the position of each class that I want to open changes and the incorrect pages open. I was unable to find a solution....

下面是java的code:

Here is the jave code:

package com.equations.search;

import java.util.ArrayList;
import java.util.HashMap;
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.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class EquationsSearch extends Activity {
// List view
private ListView lv;

// Listview Adapter
ArrayAdapter<String> adapter;

// Search EditText
EditText inputSearch;

// ArrayList for Listview
ArrayList<HashMap<String, String>> productList;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.equations_search);

    // Listview Data
    final String products[] = { "Dell Inspiron", "HTC One X",
            "HTC Wildfire S", "HTC Sense", "HTC Sensation XE" };

    lv = (ListView) findViewById(R.id.list_view);
    inputSearch = (EditText) findViewById(R.id.inputSearch);

    // Adding items to listview
    adapter = new ArrayAdapter<String>(this,
            R.layout.equations_search_list, R.id.product_name, products);
    lv.setAdapter(adapter);

    /**
     * Enabling Search Filter
     * */

    inputSearch.addTextChangedListener(new TextWatcher() {

        public void onTextChanged(CharSequence cs, int arg1, int arg2,
                int arg3) {
            // When user changed the Text
            EquationsSearch.this.adapter.getFilter().filter(cs);
        }

        public void beforeTextChanged(CharSequence arg0, int arg1,
                int arg2, int arg3) {
            // TODO Auto-generated method stub

        }

        public void afterTextChange(Editable arg0) {
            // TODO Auto-generated method stub

        }

        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    });
    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // if(position == 1)
            String openClass = products[position];
            if (openClass.equals("HTC Wildfire S")) {
                // code specific to first list item
                Intent myIntent = new Intent(view.getContext(), A6262.class);
                startActivityForResult(myIntent, 0);
            }
        }
    });

}
}

和这里是XML equations_search.xml

and here is the xml equations_search.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<!-- Editext for Search -->
<EditText android:id="@+id/inputSearch"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="Search products.."
    android:inputType="textVisiblePassword"/>

<!-- List View -->
<ListView
    android:id="@+id/list_view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

和equations_search_list.xml

and equations_search_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<!-- Single ListItem -->

<!-- Product Name -->
<TextView android:id="@+id/product_name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dip"
    android:textSize="16dip"
    android:textStyle="bold"/>    

</LinearLayout>

感谢你在前进。

推荐答案

而不是产品[位置] ,使用 adapter.getItem(位置) 。当的ListView 不在滤镜模式,这两个东西是一样的。但是,在滤镜模式,的getItem()将采取过滤的考虑。

Rather than products[position], use adapter.getItem(position). When the ListView is not in filter mode, those two things will be the same. But, in filter mode, getItem() will take the filtering into account.

这篇关于Android的列表视图搜索过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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