【问题标题】:Searching a ListView which uses SimpleAdapter android搜索使用 SimpleAdapter android 的 ListView
【发布时间】:2014-05-12 18:13:45
【问题描述】:

我正在尝试使用我已经拥有的搜索小部件 (android:id="@+id/searchView1") 创建一个可搜索的列表,到目前为止,我只找到了使用 ArrayAdapters 的 ListViews 的帮助,并且这些解决方案有没用

这是我的代码

public class SearchActivity extends Activity {
ListView listView ;
// Array of words: source http://www.knittinghelp.com/videos/knitting-glossary
String[] words = new String[] {
        "( )",
        "[ ]",
        "*",
        "**",
        "alt",
        "approx",
        "beg",
        "bet",
        "BO",
        "CA",
        "CB",
        "CC",
        "cdd",
        "ch",
        "cm",
        "cn",
        "CO",
        "cont"
};

// Array of meanings: source http://www.knittinghelp.com/videos/knitting-glossary
String[] meaning = new String[]{
    "work instruction between parentheses, in the place directed",
    "work instructions between brackets, as many times as directed",
    "repeat instructions following the single asterisk as directed",
    "repeat instructions between asterisks, as directed",
    "alternative",
    "approximately",
    "beginning",
    "between",
    "Bind off",
    "colour A",
    "colour B",
    "colour C",
    "centered double decrease. sl2 tog, K1, pass the slipped stitches over (together)",
    "chain (using crochet hook). Start with a slip knot.",
    "centimeter(s)",
    "cable needle: short knitting needle, used as an aid in the twisting of a cable.",
    "cast on",
    "continue"
};

/**
 * When the search activity begins, show the view as in the search xml, and list the
 * given values in the list view
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search);

    final List<HashMap<String, String>> wordList = new ArrayList<HashMap<String, String>>();        

    for(int i=0; i<words.length; i++){
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("word", words[i]);
        hashMap.put("meaning", meaning[i]);           
        wordList.add(hashMap);        
    }        

    final SimpleAdapter adapter = new SimpleAdapter(this, wordList, 
            android.R.layout.simple_list_item_2, 
            new String[] {"word", "meaning"}, 
            new int[] {android.R.id.text1, android.R.id.text2});

    ListView listView = (ListView) findViewById(R.id.listView1);

    listView.setAdapter(adapter); 

    EditText search = (EditText) findViewById(R.id.searchView1);

    search.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
             SearchActivity.this.adapter.getFilter().filter(s);

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

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

        }
    }); 
}
}

在 onTextChanged 中,适配器出现错误,提示无法解决。 如果有人能指出我正确的方向,那就太棒了:)

【问题讨论】:

  • 你想搜索什么?
  • 我添加了更多代码,但我忽略了@NoXSaeeD 我只想按单词 ie alt 进行搜索

标签: android android-listview searchview simpleadapter


【解决方案1】:

您需要在外部类中添加一个名为适配器的实例变量。根据您发布的代码块,您需要有一个名为 adapter 的 SimpleAdapter 成员变量,如下所示:

public class SearchActivity extends Activity {
   ListView listView;
   SimpleAdapter adapter; // this is what you're missing

因为SearchActivity.this.adapter.getFilter().filter(s),正在引用一个名为 SearchActivity 类的适配器的实例变量(目前不存在)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-24
    • 1970-01-01
    相关资源
    最近更新 更多