0

I have a list, that consists Facebook friend list of my app user. Above Listview I have an EditText to search this friend list alphabetically. I can't regenerate list when text changed in EditText.I extends BaseAdapter for list. Here is my code:

public class FriendListActivity extends Activity implements OnItemClickListener

inside this:

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

        }

    });

here is Updated My Adapter with Filter

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;

}

Now i can get search list in Log cat but Listview not Populated. ?

Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
Shihab Uddin
  • 6,699
  • 2
  • 59
  • 74

1 Answers1

2

Since I never implemented my Search method any differently, I suppose, the alphabetical listing depends entirely on the sorting of your initial list. As far as a similar integration (with Facebook friends) is concerned, my filtered List (its a GridView actually) lists them in their alphabetical order.

It's a little too big to reproduce here again, so I will simply link to the answer I had posted a few days back. The OP was using a ListView too as compared to my GridView. But since the logical can be extrapolated (and it worked for the OP in that post), I am guessing it might help you too. It uses a BaseAdpter too

Give it a try: https://stackoverflow.com/a/12363961/450534

Note: Because the code is literally my entire implementation of my Friends list, with the activity and the adapter, it has become a mighty lengthy post, so some patience will be recommended. ;-)

Community
  • 1
  • 1
Siddharth Lele
  • 27,623
  • 15
  • 98
  • 151
  • 1
    thanks for response.i am already do the job using custom adapter which you have. but i have no option to regenerate the list with it's adapter. Seems i need to create a jsonArray object inside ** if (textlength <= name.length()){....} method...but i haven't find the way yet.. – Shihab Uddin Sep 26 '12 at 07:41
  • Yo...man...i just finish the job. Thanks for Ur Answer. I just follow Link.@Siddharth Lele – Shihab Uddin Sep 29 '12 at 13:43
  • Ya, ur solution works super. but let me know one thing where u Handle image. Is it possible to look me a view of ImageLoader Class?@Siddharth Lele – Shihab Uddin Sep 30 '12 at 05:13
  • I'm not doing anything different for the images. If you look at `ArrayList` in the linked answer and the corresponding class for it, you will notice that all the details for a particular user are held in one object. – Siddharth Lele Sep 30 '12 at 08:48