如何添加搜索功能中的ListView

如何添加搜索功能中的ListView

本文介绍了如何添加搜索功能中的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何添加搜索功能在我的Andr​​oid应用程序?我有一个列表视图中,我想添加搜索功能,我有code,但我不知道我如何用我的应用程序正确地集成它这里是code -

  inputSearch.addTextChangedListener(新TextWatcher(){

@覆盖
公共无效onTextChanged(CharSequence的CS,诠释ARG1,诠释ARG2,诠释ARG3){
    //当用户更改文本
    。MainActivity.this.adapter.getFilter()过滤器(CS);
}

@覆盖
公共无效beforeTextChanged(CharSequence的arg0中,诠释ARG1,INT ARG2,
        INT ARG3){
    // TODO自动生成方法存根

}

 @覆盖
公共无效afterTextChanged(编辑为arg0){
    // TODO自动生成方法存根
}
});
 

和这里是我的java文件

 包com.Example.app;
进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.ArrayAdapter;
进口android.widget.ListView;

公共类MainActivity延伸活动{

私人的ListView List1中;

私人字符串数组[] = {iPhone,教程,画廊,机器人,

项目1,项目2,项目3,条款4,项目1,项目2,项目3,条款4,项目1,项目2,item3的,条款4};


   @覆盖

公共无效的onCreate(包savedInstanceState){

super.onCreate(savedInstanceState);

的setContentView(R.layout.activity_main);

List1的=(的ListView)findViewById(R.id.ListView01);

通过使用setAdpater方法​​列表视图//我们一个附加的字符串数组

//名单。

list1.setAdapter(新ArrayAdapter(这一点,

android.R.layout.simple_list_item_1,数组));

list1.setOnItemClickListener(新OnItemClickListener(){

公共无效onItemClick(适配器视图<>母公司视图中查看,
        INT位置,长的id){
     如果(位置== 1)
    {
      意图myIntent =新的意图(getApplicationContext(),Baba.class);
      startActivity(myIntent);
    }
  }
});
}

}
 

main.xml中

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:stackFromBottom =真正的>
<的EditText
机器人:ID =@ + ID / inputSearch
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =FILL_PARENT
机器人:提示=搜索
机器人:inputType =textVisiblePassword>
< /的EditText>
<的ListView
    机器人:ID =@ + ID / ListView01
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:scrollbarSize =5DP

    >

< / ListView控件>

< / LinearLayout中>
 

解决方案

请尝试以下

 的EditText inputSearch;
ArrayAdapter<字符串>适配器;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
inputSearch =(EditText上)findViewById(R.id.inputSearch); //初始化的EditText
List1的=(的ListView)findViewById(R.id.ListView01);
适配器=新的ArrayAdapter<字符串>(这一点,android.R.layout.simple_list_item_1,数组));
list1.setAdapter(适配器);
inputSearch.addTextChangedListener(新TextWatcher(){

@覆盖
公共无效onTextChanged(CharSequence的CS,诠释ARG1,诠释ARG2,诠释ARG3){
    //当用户更改文本
    。MainActivity.this.adapter.getFilter()过滤器(CS);
}

@覆盖
公共无效beforeTextChanged(CharSequence的arg0中,诠释ARG1,INT ARG2,
        INT ARG3){
    // TODO自动生成方法存根

}

 @覆盖
公共无效afterTextChanged(编辑为arg0){
    // TODO自动生成方法存根
}
});
list1.setOnItemClickListener(新OnItemClickListener(){
公共无效onItemClick(适配器视图<>母公司视图中查看,
        INT位置,长的id){
     如果(位置== 1)
    {
      意图myIntent =新的意图(getApplicationContext(),Baba.class);
      startActivity(myIntent);
    }
  }
});
}
 

初​​始化EDITTEXT inputSearch在的onCreate

  inputSearch =(EditText上)findViewById(R.id.inputSearch); //初始化的EditText
 

初​​始化适配器

 适配器=新的ArrayAdapter<字符串>(这一点,android.R.layout.simple_list_item_1,数组));
 

设置适配器操作列表视图

  list1.setAdapter(适配器);
 

这是搜索情况,其中

  @覆盖
公共无效onTextChanged(CharSequence的CS,诠释ARG1,诠释ARG2,诠释ARG3){
    //当用户更改文本
    。MainActivity.this.adapter.getFilter()过滤器(CS); //过滤器基于输入
}
 

how do i add Search Functionality in my android app?i have a listview in which i want to add search functionality , i have the code but i dont know how do i integrate it properly with my apphere is the code -

inputSearch.addTextChangedListener(new TextWatcher() {

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

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

}

 @Override
public void afterTextChanged(Editable arg0) {
    // TODO Auto-generated method stub
}
});

and here is my java file

package com.Example.app;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

private ListView list1;

private String array[] = { "Iphone", "Tutorials", "Gallery", "Android",

"item 1", "item 2", "item3", "item 4", "item 1", "item 2", "item3", "item 4","item 1",        "item 2", "item3", "item 4"};


   @Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

list1 = (ListView) findViewById(R.id.ListView01);

// By using setAdpater method in listview we an add string array in

// list.

list1.setAdapter(new ArrayAdapter(this,

android.R.layout.simple_list_item_1, array));

list1.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
     if (position == 1)
    {
      Intent myIntent = new Intent(getApplicationContext(),Baba.class);
      startActivity(myIntent);
    }
  }
});
}

}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stackFromBottom="true">
<EditText
android:id="@+id/inputSearch"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:hint="Search"
android:inputType="textVisiblePassword">
</EditText>
<ListView
    android:id="@+id/ListView01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scrollbarSize="5dp"

    >

</ListView>

</LinearLayout>
解决方案

Try the below

EditText inputSearch;
ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputSearch = (EditText) findViewById(R.id.inputSearch); // initialize edittext
list1 = (ListView) findViewById(R.id.ListView01);
adapter =new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, array));
list1.setAdapter(adapter);
inputSearch.addTextChangedListener(new TextWatcher() {

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

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

}

 @Override
public void afterTextChanged(Editable arg0) {
    // TODO Auto-generated method stub
}
});
list1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
     if (position == 1)
    {
      Intent myIntent = new Intent(getApplicationContext(),Baba.class);
      startActivity(myIntent);
    }
  }
});
}

Initialize editText inputSearch in onCreate.

inputSearch = (EditText) findViewById(R.id.inputSearch); // initialize edittext

Initialize adapter

adapter =new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, array));

Set the adapter op listview

list1.setAdapter(adapter);

This is where the search happens

 @Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
    // When user changed the Text
    MainActivity.this.adapter.getFilter().filter(cs);   // filter based on input
}

这篇关于如何添加搜索功能中的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:05