【问题标题】:How to clear all items in a ListView while using List Adapter onTextChange?使用 List Adapter onTextChange 时如何清除 ListView 中的所有项目?
【发布时间】:2014-02-20 01:39:24
【问题描述】:

我一直在努力寻找答案,但很难找到有效的解决方案。

我尝试将适配器设置为 null,清除实际列表,但似乎都不起作用。

我正在使用带有 ListAdapter 的 ListView,并试图在文本更改时明确搜索文本的更改。

list.clear();有效,但在文本更改时不会发生。

这是我的代码:

    private EditText search_input;
        private Button search_button;

        // progress bar for search results
        private ProgressDialog search_loading;

        private ListView wordSearchList;
        private ListAdapter adapter;

    // no result layout
        private LinearLayout no_res;

        // create list for adapter
        ArrayList<HashMap<String, String>> list;
        // database helper
        private DatabaseHelper db;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.dictionary_search);

            search_input = (EditText) findViewById(R.id.search_dictionary);
            search_button = (Button) findViewById(R.id.search_button);
                search_button.setOnClickListener(this);
    // linear layout for no results
            no_res = (LinearLayout) findViewById(R.id.search_result_ll);

            // create hashmap list
            list = new ArrayList<HashMap<String, String>>();


        // remove views if they exist
            search_input.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before,
                        int count) {

                        // REMOVE LIST VIEW AND ADAPTER
                // list.clear();
                if (no_res.getChildCount() > 0) {
                    no_res.removeAllViews();
                }

                }

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

                @Override
                public void afterTextChanged(Editable s) {
                }
            });

        }

    @Override
        public void onClick(View v) {
            if (v == search_button) {
    // clear list for fresh start
            list.clear();
            no_res.removeAllViews();

                // validate input and that something was entered
                if (search_input.getText().toString().length() < 1) {

                    // missing required info (null was this but lets see)
                    Toast.makeText(getApplicationContext(),
                            "Please search for something!", Toast.LENGTH_LONG)
                            .show();

                } else {

                    String search_data;
                    search_data = search_input.getText().toString();

                    // remove any current views on search again
                    // REMOVE THE LIST VIEW

                    // execute the query search
                    List<DatabaseWordsFTS> search_results = db
                            .getSingleWordSearch(search_data);
                    // if no search results returned
                    if (search_results.size() <= 0) {
                        TextView no_results_tv = new TextView(this);
                    no_results_tv.setText("No results found.");

                    no_res.addView(no_results_tv);




    }

// setup listview
                wordSearchList = (ListView) findViewById(R.id.wordSearchList);

                for (DatabaseWordsFTS word_found : search_results) {

                    // have to create hashmap in loop
                    HashMap<String, String> map = new HashMap<String, String>();

                    // convert d id to long
                    Integer dictionary_id_convert = (int) (long) word_found._dictionaryId;

                    // extract dictionary from d-id - since it is not a list and
                    // just a variable
                    DatabaseDictionary dictionary_found = db
                            .getDictionary(dictionary_id_convert);

                    // extract languages to send below
                    Integer dln_1 = (int) dictionary_found._language1Id;
                    Integer dln_2 = (int) dictionary_found._language2Id;
                    Integer dln_3 = (int) dictionary_found._language3Id;
                    Integer dln_4 = (int) dictionary_found._language4Id;

                    // get languages for the words based on ids passed in
                    List<DatabaseLanguages> LanguagesForD = db
                            .getAllLanguagesWithId(dln_1, dln_2, dln_3, dln_4);

                    // add name to hashmap and rest of the data as strings
                    map.put("w_1", word_found.get_word1_fts());
                    map.put("l_1", LanguagesForD.get(0)._language_name);
                    map.put("d_id", String.valueOf(dictionary_id_convert));
                    map.put("w_id", String.valueOf(word_found.get_id()));

                    if (word_found.get_word2_fts() != null) {
                        map.put("w_2", word_found.get_word2_fts());
                        map.put("l_2", LanguagesForD.get(1)._language_name);
                    }

                    if (word_found.get_word3_fts() != null) {
                        map.put("w_3", word_found.get_word3_fts());
                        map.put("l_3", LanguagesForD.get(2)._language_name);
                    }
                    if (word_found.get_word4_fts() != null) {
                        map.put("w_4", word_found.get_word4_fts());
                        map.put("l_4", LanguagesForD.get(3)._language_name);
                    }

                    list.add(map);

                    // used to dismiss progress bar for searching
                    search_loading.dismiss();
                }

                String[] from = { "w_1", "w_2", "w_3", "w_4" }; // , "word3",
                                                                // "word4"
                int[] to = { R.id.textName, R.id.textLanguage };
                adapter = new SimpleAdapter(this, list,
                        R.layout.dictionary_row, from, to);

                wordSearchList.setAdapter(adapter);
                wordSearchList
                        .setOnItemClickListener(new AdapterView.OnItemClickListener() {

                            @Override
                            public void onItemClick(AdapterView<?> parent,
                                    View view, int position, long id) {

                                // ListView Clicked item index
                                int itemPosition = position;

                                // ListView Clicked item value
                                HashMap itemValue = (HashMap) wordSearchList
                                        .getItemAtPosition(position);

                                String w_id = (String) itemValue.get("w_id");
                                String d_id = (String) itemValue.get("d_id");
                                String l_1 = (String) itemValue.get("l_1");
                                String l_2 = (String) itemValue.get("l_2");
                                String l_3 = (String) itemValue.get("l_3");
                                String l_4 = (String) itemValue.get("l_4");
                                String w_1 = (String) itemValue.get("w_1");
                                String w_2 = (String) itemValue.get("w_2");
                                String w_3 = (String) itemValue.get("w_3");
                                String w_4 = (String) itemValue.get("w_4");

                                // Show Alert
                                Toast.makeText(
                                        getApplicationContext(),
                                        "Position :" + itemPosition
                                                + "  ListItem : " + w_id,
                                        Toast.LENGTH_LONG).show();

                                // creating bundle
                                Bundle d_data = new Bundle();

                                // add to bundle
                                d_data.putString("w_id", w_id);
                                d_data.putString("wd_id", d_id);
                                d_data.putString("w_1", w_1);
                                d_data.putString("l_1", l_1);

                                // get tags only if it exists
                                if (w_2 != null) {
                                    d_data.putString("w_2", w_2);
                                    d_data.putString("l_2", l_2);
                                }

                                if (w_3 != null) {
                                    d_data.putString("w_3", w_3);
                                    d_data.putString("l_3", l_3);
                                }

                                if (w_4 != null) {
                                    d_data.putString("w_4", w_4);
                                    d_data.putString("l_4", l_4);
                                }

                                // start new intent based on the tag -
                                Intent single_word_view = new Intent(
                                        DictionaryWordSearch.this,
                                        DictionarySingleWordView.class);

                                // call extras
                                single_word_view.putExtras(d_data);

                                // new_dictionary_view.putExtra("d_id",
                                // WhatAmISupposeToPassInHere);
                                startActivity(single_word_view);

                            }

                        });
            }

编辑:(下面为我工作)

将 ListAdapter 更改为 SimpleAdapter

if(adapter != null){list.clear(); adapter.notifyDataSetChanged();} 

在onTextChange中添加了上述代码

【问题讨论】:

  • 使用 Adapter.clear() 和 Adapter.notifyDataSetChanged()
  • 不适用于列表适配器

标签: java android listview android-listview


【解决方案1】:

如果你想要TextView 没有结果,你可以实现这个代码

listView.setEmptyView(emptyView)

并将您的TextView 传递给此方法,

要清除 ListView,您可以清除您的收藏并调用 notifyChangeDataSet 或将适配器设置为 null 尝试两者并反馈给我

【讨论】:

  • noResults 我用不同的方法让它工作,所以很好,只是列表没有清除 textChanged 这是我唯一遇到的问题
  • 嘿,如果你可以编辑你的代码,最后的工作是这一行 if(adapter != null){list.clear(); adapter.notifyDataSetChanged();} 然后我可以标记你的答案正确
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多