【问题标题】:Filtering ArrayAdapter Listview not refreshing (Android)过滤 ArrayAdapter Listview 不刷新(Android)
【发布时间】:2016-07-30 09:53:25
【问题描述】:

我在尝试实现可过滤(自定义)列表视图时遇到问题。调试时,de 过滤似乎确实有效,但从未显示结果(listview 似乎没有过滤任何内容)。无论我搜索什么关键字:所有项目都会继续显示。

片段中的代码

ListView yourListView;
List<PokemonListItem> pokemonList;
PokemonListAdapter adapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_pokemon_list_overview,
            container, false);

    pDialog = new ProgressDialog(getContext());
    pDialog.setMessage("Refreshing pokemon list...");
    pDialog.setCancelable(false);
    TAG = PokemonViewer.class.getSimpleName();

    yourListView = (ListView) view.findViewById(R.id.urlListView);
    refreshPokemonListItems();

    EditText inputSearch = (EditText) view.findViewById(R.id.searchInput);

    inputSearch.addTextChangedListener(new TextWatcher() {

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

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {}

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

    adapter = new PokemonListAdapter(getContext(), R.id.searchInput, pokemonList);
    yourListView.setAdapter(adapter);

    return view;
}

我的 DetailListAdapter

public class PokemonListAdapter extends ArrayAdapter<PokemonListItem> implements Filterable{
    List<PokemonListItem> pokemonList;

    public PokemonListAdapter(Context context, int resource, List<PokemonListItem> items) {
        super(context, resource, items);
        this.pokemonList = items;
    }

    Filter myFilter = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults filterResults = new FilterResults();
            ArrayList<PokemonListItem> tempList=new ArrayList<>();
            //constraint is the result from text you want to filter against.
            //objects is your data set you will filter from
            Log.v("Logger", "The constraint is now: " + constraint.toString());
            if(constraint != null && pokemonList!=null) {
                int length=pokemonList.size();
                int i=0;
                while(i<length){
                    PokemonListItem item=pokemonList.get(i);
                    //do whatever you wanna do here
                    //adding result set output array
                    boolean containsString = item.getName().toLowerCase().contains(constraint.toString().toLowerCase());
                    if(containsString){
                        tempList.add(item);
                        //Log.v("Logger", "Constraint: " + constraint.toString().toLowerCase() + " value: " + item.getName() + " bool: " + containsString);
                    }

                    i++;
                }
                //following two lines is very important
                //as publish result can only take FilterResults objects
                filterResults.values = tempList;
                filterResults.count = tempList.size();
            }
            return filterResults;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence contraint, FilterResults results) {
            pokemonList = (List<PokemonListItem>) results.values;
            Log.v("Logger", "This is called");
            if (results.count > 0) {
                Log.v("Logger", "true is called");
                Log.v("Logger", results.values.toString());
                clear();
                for (PokemonListItem item : pokemonList) {
                    add(item);
                }
                notifyDataSetChanged();
            } else {
                Log.v("Logger", "false is called");
                notifyDataSetInvalidated();
            }
        }
    };

    @Override
    public Filter getFilter() {
        return myFilter;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v = convertView;

        if (v == null) {
            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            v = vi.inflate(R.layout.url_list_row, null);
        }

        PokemonListItem p = getItem(position);

        if (p != null) {
            TextView name = (TextView) v.findViewById(R.id.name);


            if (name != null) {
                name.setText(p.getName());
            }
        }

        return v;
    }

}

PokemonListItem

public class PokemonListItem {
    private String id;
    private String name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        //This only works with pokemon names > 2 chars
        if(name.length() > 1){
            char first = Character.toUpperCase(name.charAt(0));
            this.name = first + name.substring(1);
        }else{
            this.name = name;
        }
    }

    @Override
    public String toString(){
        return id;
    }

【问题讨论】:

    标签: java android listview filter


    【解决方案1】:

    好吧,我找到了解决方案。昨天看了几篇“Stack Overflow”的帖子:“确保你传入的对象和你过滤的对象是一样的”。

    我以为我做到了,但是...在逐行阅读我的代码后,我终于注意到...

    在onCreatedView(片段)中我设置了适配器:

    adapter = new PokemonListAdapter(getContext(), R.id.searchInput, pokemonList);
    yourListView.setAdapter(adapter);
    

    后来,在“refreshPokemonListItems”方法中写了这两行代码:

    // get data from the table by the ListAdapter
    PokemonListAdapter customAdapter = new PokemonListAdapter(getContext(), R.layout.url_list_row, pokemonList);
    
    yourListView.setAdapter(customAdapter);
    

    嗯... listview 传递了两个具有不同列表的不同 addapter。 我不得不删除“onCreateView”中的代码行并将refreshPokemonList中的代码更改为:

     adapter = new PokemonListAdapter(getContext(), R.id.searchInput, pokemonList);
     yourListView.setAdapter(adapter);
    

    仅此而已……

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-25
      相关资源
      最近更新 更多