如何按字母顺序在列表视图中搜索朋友列表?

时间:2012-09-26 06:46:28

标签: android facebook

我有一个列表,其中包含我的app用户的Facebook好友列表。高于Listview我有EditText按字母顺序搜索此好友列表。在EditText中更改文本时,我无法重新生成列表。我将BaseAdapter扩展为列表。这是我的代码:

public class FriendListActivity extends Activity implements OnItemClickListener

在这里:

editTextFriendSearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
          // here  i use filter in listAdapter.
        friendListAdapter.getFilter().filter(s);

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

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

        }

    });

此处更新了我的带过滤器的适配器

public class FriendListAdapter extends BaseAdapter implements Filterable {

    private LayoutInflater mInflater;
    FriendListActivity friendsList;

    public FriendListAdapter(FriendListActivity friendsList) {

        this.friendsList = friendsList;
        if (Utility.model == null) {
            Utility.model = new FriendsGetProfilePics();
        }

        Utility.model.setListener(this);
        mInflater = LayoutInflater.from(friendsList.getBaseContext());
    }

    @Override
    public int getCount() {

        return jsonArray.length();
    }

    @Override
    public Object getItem(int arg0) {

        return null;
    }

    @Override
    public long getItemId(int arg0) {

        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        JSONObject jsonObject = null;
        try {
            jsonObject = jsonArray.getJSONObject(position);
        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        View hView = convertView;
        if (convertView == null) {
            hView = mInflater.inflate(R.layout.row_view, null);
            ViewHolder holder = new ViewHolder();
            holder.profile_pic = (ImageView) hView
                    .findViewById(R.id.profile_pic);
            holder.name = (TextView) hView.findViewById(R.id.name);
            hView.setTag(holder);
        }
        ViewHolder holder = (ViewHolder) hView.getTag();
        try {

            if (graph_or_fql.equals("graph")) {
                holder.profile_pic.setImageBitmap(Utility.model.getImage(
                        jsonObject.getString("id"),
                        jsonObject.getString("picture")));
            } else {
                holder.profile_pic.setImageBitmap(Utility.model.getImage(
                        jsonObject.getString("uid"),
                        jsonObject.getString("pic_square")));
            }
            holder.name.setText(jsonObject.getString("name"));

        } catch (Exception e) {

        }

        return hView;
    }   
// update : Now i use filter here..
    @Override
    public Filter getFilter() {
        if (newfilter == null) {
            newfilter = new Filter() {

                @Override
                protected void publishResults(CharSequence constraint,
                        FilterResults results) {
                    Log.d("----Filter----", "publishResults");
                    notifyDataSetChanged();
                }

                @Override
                protected FilterResults performFiltering(
                        CharSequence constraint) {

                    constraint = constraint.toString().toLowerCase();
                    Log.d("Filter constraints", "constraint : "
                            + constraint);

                    // here i create json array object but 
                    // not sure am i walking right way ??
                    ArrayList<JSONObject> jsonObjects = new ArrayList<JSONObject>();

                    if (constraint != null && constraint.length() > 0) {
                        for (int i = 0; i < jsonArray.length(); i++) {
                            try {
                                JSONObject o = (JSONObject) jsonArray
                                        .get(i);
                                if (o.getString("name").toLowerCase()
                                        .contains(constraint)) {
                                    Log.d("----Results-----", "equals : "
                                            + o.getString("name"));

                                    jsonObjects.add(o);
                                }

                            } catch (JSONException e) {

                                e.printStackTrace();
                            }

                        }

                    }

                    FilterResults newFilterResults = new FilterResults();
                    newFilterResults.count = jsonObjects.size();
                    newFilterResults.values = jsonObjects;
                    Log.d("----Count----", " " + newFilterResults.count
                            + " " + newFilterResults.values);
                    return newFilterResults;

                }
            };

        }
        return newfilter;

    }

}

class ViewHolder {
    ImageView profile_pic;
    TextView name;

}

现在我可以在Log cat中获取搜索列表,但Listview没有填充。 ?

1 个答案:

答案 0 :(得分:2)

由于我从未以不同的方式实现我的搜索方法,我想,按字母顺序排列的列表完全取决于初始列表的排序。就类似的整合(与Facebook好友)而言,我的过滤列表(实际上是GridView)按字母顺序列出它们。

再次在这里复制有点太大了,所以我只想链接到几天前发布的答案。与ListView相比,OP也使用了GridView。但由于逻辑可以推断(并且它适用于该帖子中的OP),我猜它也可能对你有所帮助。它也使用BaseAdpter

试一试:https://stackoverflow.com/a/12363961/450534

注意:因为代码实际上是我的好友列表的整个实现,activityadapter,所以它已成为一个冗长的帖子,因此建议您耐心等待。 ; - )