1
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);
4

1 回答 1

2

ListAdapter不实现Filterable接口。然而,一些适配器,如ArrayAdapterSimpleAdapter做。这意味着您应该考虑将您的适配器切换到实现接口的东西,或者制作您自己的自定义适配器并在那里实现它。

此外,根据上面的代码,您将您设置adapter为 null 并且您不初始化它(我没有看到对displayFriends().当然是适配器)。

于 2013-01-20T04:39:44.753 回答