0

アプリユーザーのFacebookの友達リストで構成されるリストがあります。上で、この友人リストをアルファベット順に検索するListview必要があります。.I extends for listEditTextでテキストが変更されたときにリストを再生成できません。これが私のコードです:EditTextBaseAdapter

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実際には) はそれらをアルファベット順にリストします。

ここでもう一度再現するには少し大きすぎるので、数日前に投稿した回答にリンクするだけです。OP はListView、私の と比較して a も使用していましたGridView。しかし、論理は外挿できるため (そして、その投稿の OP で機能した)、それも役立つと思います。それはBaseAdpterあまりにも使用します

試してみてください: https://stackoverflow.com/a/12363961/450534

注: このコードは文字どおり、 と を使用してフレンド リストを完全に実装したactivityものadapterであるため、非常に長い記事になっているため、しばらくお待ちいただくことをお勧めします。;-)

于 2012-09-26T07:19:39.087 に答える