【问题标题】:error name: The method getFilter() is undefined for the type yourActivity.ListAdapter错误名称:类型 yourActivity.ListAdapter 的方法 getFilter() 未定义
【发布时间】:2013-01-20 04:34:35
【问题描述】:
private ArrayList <HashMap<String, Object>> availableFriends;
public ListAdapter adapter = null;
friendsearch = (EditText)findViewById(R.id.friendsearch);
        friendsearch.addTextChangedListener(new TextWatcher() {

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

在上面的行中我得到了错误,这里我想做的是在自定义列表视图中搜索(带有图像和名称的列表视图) }

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

        }

        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
        }
    });

下面的代码向您展示了我如何获取列表数据以及如何显示列表数据。 如果需要,请观看,否则请离开。

public List<Friend> getFriendsList(){
    String accessToken = null;
    DatabaseHelper helper = new DatabaseHelper(getApplicationContext());
    DatabaseUtility dao = new DatabaseUtility(helper);
    try {
        accessToken = dao.getAccessToken();
    } catch (Exception e1) {
        handler.sendEmptyMessage(1);
        return null;
    }
    if(accessToken == null || accessToken.length() == 0){
        handler.sendEmptyMessage(1);
        return null;
    }

    Map<String , String> params = new HashMap<String,String>();
    params.put(Constants.ACCESS_TOKEN_PARAM, accessToken);

    List<Friend> friendsList = null;
    try {
            friendsList = Utils.getFriendsList(params,this);
    } catch (NullPointerException e) {
        handler.sendEmptyMessage(12);
        return null;
    } catch (JSONException e) {
        //handler.sendEmptyMessage(11);
        return null;
    }
    return friendsList;
}

public void displayFriends(List<Friend> friendList){
    List<Friend> friendsList = friendList;
    availableFriends = new ArrayList<HashMap<String,Object>>();
    HashMap<String, Object> friendData = null;
    Friend friend = null;
    try{
        for(int i = 0 ; i < friendsList.size() ; i++){
            friend = friendsList.get(i);
            friendData = new HashMap<String, Object>();
            friendData.put(FRIENDS_NAME_KEY, friend.getFriendName());
            friendData.put(IS_IMAGE_KEY, friend.getIsImage());
            friendData.put(IDKEY, friend.getFriendID());
            availableFriends.add(friendData);
        }

    adapter = new ListAdapter(availableFriends,FriendsActivity.this);
    listView.setAdapter(adapter);
    listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

【问题讨论】:

    标签: android


    【解决方案1】:

    ListAdapter 没有实现Filterable 接口。但是,某些适配器,例如 ArrayAdapterSimpleAdapter 可以。这意味着您应该考虑将您的适配器切换到实现接口的东西制作您自己的自定义适配器并在那里实现它。

    此外,根据上面的代码,您将 adapter 设置为 null 并且您没有对其进行初始化(我没有看到对 displayFriends() 的调用。您还应该调查一下以确保您的初始化是正确的(当然是在选择更合适的适配器之后)。

    【讨论】:

    • 我想为 listadapter 实现过滤功能,我该怎么做呢?
    • 您将必须创建一个扩展 ListAdapter 的单独类,并使其实现 Filterable 从那里您实现该方法。我自己实际上并没有使用过这个,所以我不知道实现的全部细节。 This SO question 给出了一个例子以及this SO 的答案。
    • @srilatha 不,谢谢,我会坚持使用 stackoverflow。您可能还应该从您的评论中删除链接(您可以编辑您的评论)。无论如何,我建议你尝试一下,做一些研究,一旦你有具体问题再回来。
    • 好的,谢谢你的时间,我没有积分可以为你投票,谢谢你的时间,从现在开始禁用链接它不起作用。
    猜你喜欢
    • 1970-01-01
    • 2017-08-27
    • 2016-04-30
    • 2018-03-31
    • 1970-01-01
    • 1970-01-01
    • 2020-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多