0

我有一个列表,其中包含我的应用用户的 Facebook 好友列表。上面Listview我有一个EditText按字母顺序搜索这个朋友列表。EditText当.I中的文本更改为列表时,我无法重新生成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。?

4

1 回答 1

2

由于我从未以任何不同的方式实现我的 Search 方法,我想,按字母顺序排列的列表完全取决于您的初始列表的排序。就类似的集成(与 Facebook 朋友)而言,我的过滤列表(GridView实际上是一个)按字母顺序列出了它们。

在这里再次复制有点太大了,所以我将简单地链接到我几天前发布的答案。ListView与我的GridView. 但是由于可以推断出逻辑(并且它对该帖子中的 OP 有效),我猜它也可能对您有所帮助。它也使用BaseAdpter

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

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

于 2012-09-26T07:19:39.087 回答