更新搜索结果懒惰适配器在android系统 [英] Update Search Results to Lazy Adapter in android

查看:144
本文介绍了更新搜索结果懒惰适配器在android系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有项目的名单,并想实现搜索功能吧。因此,我有addTextChangedListener给它一个文本框。

I have list of Item and would like to implement the search feature for it. Hence, I have a text box with addTextChangedListener to it.

搜索结果来了很好,但是当我尝试设置的结果,ListView中新的结果得到追加到老的效果。

The search Results are coming fine, but when I try to set the results to ListView the new results are getting appended to old results.

我使用的是懒惰的适配器。我如何清除旧的结果从我的适配器?

I am using a Lazy Adapter. How do I clear the old results from my adapter?

下面是code:

ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
ArrayList<HashMap<String, String>> searchResults = new ArrayList<HashMap<String, String>>();
 searchText = (EditText) findViewById(R.id.searchText);

 //Count from the server 
 int count = dataCount();

for (int i = 0; i < dataCount; i++) 
{
    HashMap<String, String> map = new HashMap<String, String>();

    // adding data to HashMap key => value
    map.put(KEY_ID, trackNumber);
    map.put(KEY_TITLE, trackTitle);
    map.put(KEY_ARTIST, trackArtist);
    map.put(KEY_DURATION, trackDuration);
    map.put(KEY_THUMB_URL, trackAlbumArt);

    // adding HashList to ArrayList
    songsList.add(map);

}

adapter=new LazyAdapter(this, songsList);        
list.setAdapter(adapter);

TextWatcher

TextWatcher

 nameText.addTextChangedListener(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)
        {

            getSong(s);
        }

    });

有关取得成果:

  Map getSong(CharSequence s) 
  {

    for (HashMap<String, String> map : songsList)
    {
        if(map.get(KEY_TITLE).contains(s))
        {
            Log.e("Map","Son"+map);

            HashMap<String, String> map2 = new HashMap<String, String>();
            // adding each child node to HashMap key => value
            map2.put(KEY_ID, map.get(KEY_ID));
            map2.put(KEY_TITLE, map.get(KEY_TITLE));
            map2.put(KEY_ARTIST, map.get(KEY_ARTIST));
            map2.put(KEY_DURATION, map.get(KEY_DURATION));
            map2.put(KEY_THUMB_URL, map.get(KEY_THUMB_URL));

            searchResults.add(map2);            
        }
    }

    adapter=new LazyAdapter(this, searchResults);
    list.setAdapter(adapter);
    adapter.notifyDataSetChanged();

    return Collections.emptyMap();
    }

谢谢!

推荐答案

在开始之前,你不清洁旧的搜索结果。

You're not "cleaning" old search results before you begin.

添加以下行:

searchResults = new ArrayList<HashMap<String, String>>();

在方法 getSong()的开头(前为环)

in the beginning of the method getSong() (before the for-loop)

这篇关于更新搜索结果懒惰适配器在android系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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