在listview适配器中使用过滤器膨胀

时间:2012-09-05 14:04:48

标签: android listview inflate baseadapter textwatcher

我遇到问题我无法在列表视图上进行搜索!

我尝试使用inflate实现listview并进行搜索,但不幸的是我无法...在logcat中出现没有错误并尝试调试但不幸的是显然它没有问题...只需在listview中不在textview中时写filtar开始......

以下是我的代码

FriendRequestListener.class

/*
 * callback after friends are fetched via me/friends.
 * 
 */
public class FriendsRequestListener extends BaseRequestListener {

    @Override
    public void onComplete(final String response, final Object state) {
        dialog.dismiss();

        try{
            arrayName = new ArrayList<String>();
            jsonArray = new JSONObject(response).getJSONArray("data");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject json_data = jsonArray.getJSONObject(i);
                if(json_data.has("name")){
                    String getFriendName = json_data.getString("name");

                    arrayName.add(getFriendName);
                }else{
                    String getFriendName = null;
                    arrayName.add(getFriendName);
                }
            }
        }catch(JSONException e){
            Log.d(TAG, e.getMessage());
            return;
        }

        stringName = new String[arrayName.size()];
        stringName = arrayName.toArray(stringName);

        EscolhaAmigosFacebook.this.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            adapter = new FriendListAdapter(EscolhaAmigosFacebook.this, stringName, arrayName);
            friendsListView.setTextFilterEnabled(true);
            friendsListView.setAdapter(adapter);
        }
        });
    }

FriendApapter.class

/**
 * Definition of the list adapter
 */
public class FriendListAdapter extends BaseAdapter implements Filterable {

    String[] fetFriendName;

    List<String> arrayList;
    List<String> mOriginalValues;

    LayoutInflater mInflater = null;
    EscolhaAmigosFacebook friendsList;

    FriendListAdapter(EscolhaAmigosFacebook friendsList, String[] stringName, ArrayList<String> arrayName) {
        this.friendsList = friendsList;
        if (Utility.model == null) {
            Utility.model = new FriendsGetProfilePics();
        }
        Utility.model.setListener(this);

        fetFriendName = stringName;

        arrayList = new ArrayList<String>();
        mOriginalValues = arrayName;

        mInflater = LayoutInflater.from(friendsList.getBaseContext());

    }

    @Override
    public int getCount() {
        return mOriginalValues.size();
    }

    @Override
    public Object getItem(int position) {
        return mOriginalValues.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        JSONObject jsonObject = null;
        final ViewHolder holder;
        View vi = convertView;

        try {
            jsonObject = jsonArray.getJSONObject(position);
        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        if (vi == null) {

            vi = mInflater.inflate(R.layout.friend_item, null);
            holder = new ViewHolder();
            holder.profile_pic = (ImageView) vi.findViewById(R.id.profile_pic);

            TextView txtFriendsName = (TextView)vi.findViewById(R.id.name);
            txtFriendsName.setText(fetFriendName[position]);
            vi.setTag(holder);
        }else{
            holder = (ViewHolder) vi.getTag();
        }

        try {
                 holder.profile_pic.setImageBitmap(Utility.model.getImage(
                 jsonObject.getString("id"), jsonObject.getString("picture")));
            } catch (JSONException e) {
                holder.name.setText("");
            }   
        return vi;
       }



    @Override
    public Filter getFilter() {
        Filter filter = new Filter(){

            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                arrayList = (List<String>) results.values;
                notifyDataSetChanged();
            }

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {

                FilterResults results = new FilterResults();
                List<String> FilteredArrList = new ArrayList<String>();

                if (constraint == null || constraint.length() == 0) {
                    results.count = mOriginalValues.size();
                    results.values = mOriginalValues;
                } else {
                    constraint = constraint.toString();

                    for (int i = 0; i < mOriginalValues.size(); i++) {
                        String data = mOriginalValues.get(i);

                        if (data.toLowerCase().startsWith(constraint.toString()))   {
                            FilteredArrList.add(data);
                        }
                    }

                    results.count = FilteredArrList.size();
                    results.values = FilteredArrList;
                }

                return results;
            }

        };
        return filter;
    }

}

方法onComplete

     et_search = (EditText) findViewById(R.id.et_Search);
    et_search.addTextChangedListener(filterTextWatcher);

}

private TextWatcher filterTextWatcher = new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        adapter.getFilter().filter(s.toString());
    }

    @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

    }
};

我知道它看起来很棒,但如果有人有耐心帮助我,请! 我试过看各种帖子但什么都没有!

1 个答案:

答案 0 :(得分:0)

我还没有在适配器上使用过滤器,但是我已经使用了很多适配器,并且在过滤后我可以告诉你将结果放在arraylist上,但是你没有在getCount上使用arrayList,getItem ,getView,因此不使用您在数据上创建的过滤器,而是使用完整的项目列表。