机器人 - 搜索列表视图? [英] android - search listview?

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

问题描述

是否有可能与Android有一个ListView的一个搜索栏,这样,当搜索栏被触摸键盘弹出,并在文本输入到搜索栏,匹配在ListView的项目都显示<? / P>

我真正需要的是搜索栏,带来了一个键盘。

更新:

我已经添加了的EditText领域,带来了一个键盘,我可以键入的EditText领域。我想是有在ListView显示在列表中的项目的前几个字符匹配的字符输入到的EditText窗口。

下面这里列出<一的方法我试过href="http://stackoverflow.com/questions/1737009/answer-to-making-a-nice-looking-listview-filter-on-android/1737042#1737042">ListView过滤器但我有点困惑,多少滤波的ListView已经完成?

1)我需要创建一个单独的数组存储匹配键入的EditText文本的价值?从这个帖子呼叫adapter.notifyDataSetChanged ,看来ListView中已经有一个阴影阵列做到这一点,它就会被更新时adapter.notifyDataSetChanged();被称为

2)我需要调用adapter.notifyDataSetChanged();有ListView控件更新后,我键入的EditText窗口中的一些文字?

3)我需要扩展ListActivity,因为这<一个href="http://stackoverflow.com/questions/3033791/the-method-setlistadapterarrayadapter-is-undefined-for-the-type-create">post指示?如果是的话我怎么延长我的活动类,如果该活动类已被从主要活动扩展?

4)我现在有如下:

  ArrayAdapter&LT;字符串&GT;适配器= NULL;
私人的EditText filterText = NULL;

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.symptom);
    ListView控件symptomList =(ListView控件)findViewById(R.id.ListView_Symptom);
    symptomList.setTextFilterEnabled(真正的);
    symptomList.setFastScrollEnabled(真正的);
    filterText =(EditText上)findViewById(R.id.search_box);
    filterText.addTextChangedListener(filterTextWatcher);

    适配器=新的ArrayAdapter&LT;字符串&GT;(这一点,R.layout.menu_item,symptomsArray);
    symptomList.setAdapter(适配器);

    私人TextWatcher filterTextWatcher =新TextWatcher(){

        公共无效afterTextChanged(编辑S){
        }

        公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
                之后INT){
        }

        公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,
                诠释计数){
            。adapter.getFilter()过滤器(S);
            adapter.notifyDataSetChanged();
        }

    };
 

当我键入的EditText中不幸的时刻,我得到了

一个空指针异常

 发[7;&GT;过滤器](暂停(NullPointerException异常除外))
ArrayAdapter $ ArrayFilter.performFiltering(CharSequence中)线:437
过滤器$ RequestHandler.handleMessage(消息)线:234
过滤器$ RequestHandler的(处理器).dispatchMessage(消息)线:99
Looper.loop()线:123
HandlerThread.run()线:60
 

任何想法,我缺少的是什么?

解决方案

您已经做了非常小的错误: - 创建阵列适配器之前设置的文本改变监听器来编辑文本

见改正code

 公共类SearchListView扩展活动
{


/ **第一次创建活动时调用。 * /

私人的ListView LV1;
私人字符串lv_arr [] =
{机器人,iPhone,黑莓,我,J2ME,列表视图,ArrayAdapter,列表项,我们,英国,印度};
ListView的LST;
美国东部时间的EditText;
ArrayAdapter&LT;字符串&GT; arrad;



@覆盖
公共无效的onCreate(包savedInstanceState)
{
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);


    LV1 =(ListView控件)findViewById(R.id.ListView01);
    美国东部时间=(EditText上)findViewById(R.id.EditText01);

     arrad =新的ArrayAdapter&LT;字符串&GT;(这一点,android.R.layout.simple_list_item_1,lv_arr);
     lv1.setAdapter(arrad);

    //使用setTextFilterEnabled方法列表视图,我们可以过滤列表视图项目。

     lv1.setTextFilterEnabled(真正的);
     edt.addTextChangedListener(新TextWatcher()
    {


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

        }



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

        }



        @覆盖
        公共无效afterTextChanged(编辑为arg0)
        {
            // TODO自动生成方法存根
            。SearchListView.this.arrad.getFilter()过滤器(为a​​rg0);

        }
    });

}
 

}

Is it possible with Android to have a search bar for a ListView so that when the search bar is touched a keyboard pops up, and when text is typed into the search bar, the items that match in the ListView are shown?

What I really need is the search bar that brings up a keyboard.

Update:

I've added the EditText field that brings up a keyboard and I can type into the EditText field. What I want is to have the first few characters of the items in the list shown in the ListView match the characters typed into the EditText window.

I've tried following the approach listed here ListView Filter but I am a little confused as to how much filtering is already done in ListView?

1) Do I need to create a separate array that stores the values that match the text typed into EditText? From this post Call adapter.notifyDataSetChanged, it appears that ListView already has a shadow array to do this, and it gets updated when adapter.notifyDataSetChanged(); is called.

2) Do I need to call adapter.notifyDataSetChanged(); to have ListView updated after I type some text in the EditText window?

3) Do I need to extend ListActivity as this post indicates? If so how do I extend my activity class if the activity class is already being extended from the main activity?

4) What I currently have is the following:

ArrayAdapter<String> adapter = null;
private EditText filterText = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.symptom);
    ListView symptomList = (ListView) findViewById(R.id.ListView_Symptom);
    symptomList.setTextFilterEnabled(true);
    symptomList.setFastScrollEnabled(true);
    filterText = (EditText) findViewById(R.id.search_box);
    filterText.addTextChangedListener(filterTextWatcher);

    adapter = new ArrayAdapter<String>(this, R.layout.menu_item, symptomsArray);
    symptomList.setAdapter(adapter);

    private TextWatcher filterTextWatcher = new TextWatcher() {

        public void afterTextChanged(Editable s) {
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            adapter.getFilter().filter(s);
            adapter.notifyDataSetChanged();
        }

    };

Unfortunately at the moment when I type in the EditText box, I get a NullPointer Exception in

 Thread [<7> Filter] (Suspended (exception NullPointerException))   
ArrayAdapter$ArrayFilter.performFiltering(CharSequence) line: 437   
Filter$RequestHandler.handleMessage(Message) line: 234  
Filter$RequestHandler(Handler).dispatchMessage(Message) line: 99    
Looper.loop() line: 123 
HandlerThread.run() line: 60    

Any idea what I am missing?

解决方案

You have done very small mistake :- create array adapter before setting text changed Listener to the edit text

see the corrected code

public class SearchListView extends Activity
{


/** Called when the activity is first created. */

private ListView lv1;
private String lv_arr[] =
{ "Android", "iPhone", "BlackBerry", "me", "J2ME", "Listview", "ArrayAdapter", "ListItem", "Us", "UK", "India" };
ListView lst;
EditText edt;
ArrayAdapter<String> arrad;



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


    lv1=(ListView)findViewById(R.id.ListView01);
    edt = (EditText) findViewById(R.id.EditText01);

     arrad =  new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr);
     lv1.setAdapter(arrad);

    // By using setTextFilterEnabled method in listview we can filter the listview items.

     lv1.setTextFilterEnabled(true);
     edt.addTextChangedListener(new TextWatcher()
    {


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

        }



        @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
            SearchListView.this.arrad.getFilter().filter(arg0);

        }
    });

}

}

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

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