5

There was some progress made to the earlier problem. Now there is a new problem. The text in the GridView shows the correct result. However, the images are the same as at the start of the list.

For example: If I search for "Sidd" it displays three results but the photos still start as for the users starting with "A". Attached is a screenshot for clarity.

This is the BaseAdapter code:

public class TagFriendsAdapter extends BaseAdapter implements Filterable {

    List<String> arrayListNames;
    List<String> mOriginalNames;

    List<String> arrayPictures;
    List<String> mOriginalPictures;

    Activity activity;
    String[] fetFriendID;
    String[] fetFriendName;
    String[] fetFriendPicture;

    LayoutInflater inflater = null;
    ImageLoader imageLoader;

    TagFriendsAdapter(Activity a, String[] stringUID, String[] stringName, String[] stringPicture,
            ArrayList<String> arrayName, ArrayList<String> arrayPicture) {

        activity = a;
        fetFriendID = stringUID;
        fetFriendName = stringName;
        fetFriendPicture = stringPicture;

        this.arrayListNames = arrayName;
        this.arrayPictures = arrayPicture;

        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader = new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return arrayListNames.size();
    }

    public Object getItem(int position) {
        return position;
    }

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

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if(convertView == null)
            vi = inflater.inflate(R.layout.friends_grid_items, null);

        ImageView imgProfilePicture = (ImageView)vi.findViewById(R.id.imgProfilePicture);
        TextView txtUserName = (TextView)vi.findViewById(R.id.txtUserName);

        txtUserName.setText(arrayListNames.get(position));

        if (arrayPictures.get(position) != null){
            imageLoader.DisplayImage(arrayPictures.get(position), imgProfilePicture);
        }
        else if (arrayPictures.get(position) == null) {
            imgProfilePicture.setVisibility(View.GONE);
        }

        return vi;
    }

    @Override
    public Filter getFilter() {

        Filter filter = new Filter() {

            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {

                arrayListNames = (List<String>) results.values;
                notifyDataSetChanged();
            }

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {

                FilterResults results = new FilterResults();
                ArrayList<String> FilteredArrayNames = new ArrayList<String>();

                if (mOriginalNames == null && mOriginalPictures == null)    {
                    mOriginalNames = new ArrayList<String>(arrayListNames);
                    mOriginalPictures = new ArrayList<String>(arrayPictures);
                }
                if (constraint == null || constraint.length() == 0) {
                    results.count = mOriginalNames.size();
                    results.values = mOriginalNames;
                } else {
                    constraint = constraint.toString().toLowerCase();
                    for (int i = 0; i < mOriginalNames.size(); i++) {
                        String dataNames = mOriginalNames.get(i);
                        if (dataNames.toLowerCase().startsWith(constraint.toString()))  {
                            FilteredArrayNames.add(dataNames);
                        }
                    }

                    results.count = FilteredArrayNames.size();
                    System.out.println(results.count);

                    results.values = FilteredArrayNames;
                    Log.e("VALUES", results.values.toString());
                }

                return results;
            }
        };

        return filter;
    }

}

And the screenshot:

enter image description here

1
  • Forgot to +1 the other users answers for having tried to offer a solution and the effort to read through my mess. ;-) May 31, 2012 at 7:57

4 Answers 4

9
+100

First refactor your code. Create a class that holds name, picture and other friend data together.

class Friend {
    public String name;
    public String picture;
    ... /* more members and access methods*/
};

Then modify your adapter and filtering code accordingly.

  • FilterResults should contain the ArrayList<Friend>, i.e. a list of Friend objects and not just the names.

  • In Adapter, replace

    List<String> arrayListNames;
    List<String> arrayPictures;

    with

    List<Friend> friendsList;

  • Change the getView method to access data from the friendsList object list.

After these changes the code will look better and work better.

Update:

Make sure your adapter's getItem method returns a Friend object

public Object getItem(int position) {
    return mFriendsList.get(position);
}
12
  • Could you add a little more detail to the answer you have posted? I have tried the first two questions and none of them work. I would ideally love to spend more time trying to debug either of those two solution's but your suggestion seems to make more sense going forward. For example, in the first suggestion you make, refactoring the code bit, you mean move the entire portion where I parse the JSON result to this class? May 28, 2012 at 11:20
  • Perhaps I could repost my entire Activity that sends the data to this adapter again. If that helps. May 28, 2012 at 11:21
  • create a list of Friend objects from the JSON data. In a loop, create a new Friend object and add it to an ArrayList. Each Friend object will represent a single friend info.
    – Ron
    May 28, 2012 at 11:27
  • Is there a working example I can borrow some logic from? Your answer is a little vague from my point of view. Again, if I had the time, I would research and find out, but unfortunately, I don't. Sorry to say, but an example is the need of the hour. May 29, 2012 at 4:34
  • I think theres no other approach that will work for filtering in your case. As you have 2 differnt list for your data...and you can return only 1 list of results in your performFiltering method. So you got to make a single object(containing name and picture) for it to work.
    – Ron
    May 29, 2012 at 6:55
6

Problem is because you are not updating the list of picture names while you filter the array based on edittext input, you also need to update them too,

I have tried to modify your code,check this

    public static List<String> temparrayPictures; 
    public static List<String> temparrayListNames; 
    public class TagFriendsAdapter extends BaseAdapter implements Filterable { 

        List<String> arrayListNames; 
        List<String> arrayPictures; 

        Activity activity; 
        String[] fetFriendID; 
        String[] fetFriendName; 
        String[] fetFriendPicture; 

        LayoutInflater inflater = null; 
        ImageLoader imageLoader; 

        TagFriendsAdapter(Activity a, String[] stringUID, String[] stringName, String[] stringPicture, 
                ArrayList<String> arrayName, ArrayList<String> arrayPicture) { 

            activity = a; 
            fetFriendID = stringUID; 
            fetFriendName = stringName; 
            fetFriendPicture = stringPicture; 

            this.arrayListNames = arrayName; 
            this.arrayPictures = arrayPicture; 
            temparrayPictures = arrayPicture;
            temparrayListNames = arrayName;

            inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
            imageLoader = new ImageLoader(activity.getApplicationContext()); 
        } 

        public int getCount() { 
            return temparrayListNames.size(); 
        } 

        public Object getItem(int position) { 
            return position; 
        } 

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

        @Override 
        public void notifyDataSetChanged() { 
            super.notifyDataSetChanged(); 
        } 

        public View getView(final int position, View convertView, ViewGroup parent) { 
            View vi = convertView; 
            if(convertView == null) 
                vi = inflater.inflate(R.layout.friends_grid_items, null); 

            ImageView imgProfilePicture = (ImageView)vi.findViewById(R.id.imgProfilePicture); 
            TextView txtUserName = (TextView)vi.findViewById(R.id.txtUserName); 

            txtUserName.setText(temparrayListNames.get(position)); 

            if (temparrayPictures.get(position) != null){ 
                imageLoader.DisplayImage(temparrayPictures.get(position), imgProfilePicture); 
            } 
            else if (temparrayPictures.get(position) == null) { 
                imgProfilePicture.setVisibility(View.GONE); 
            } 

            return vi; 
        } 

        @Override 
        public Filter getFilter() { 

            Filter filter = new Filter() { 

                @SuppressWarnings("unchecked") 
                @Override 
                protected void publishResults(CharSequence constraint, FilterResults results) { 

                    notifyDataSetChanged(); 
                } 

                @Override 
                protected FilterResults performFiltering(CharSequence constraint) { 

                    FilterResults results = new FilterResults();
                    temparrayPictures.clear();
                    temparrayListNames.clear();

                    if (temparrayListNames == null && temparrayPictures == null)    { 
                        temparrayListNames = new ArrayList<String>(arrayListNames); 
                        temparrayPictures = new ArrayList<String>(arrayPictures); 
                    } 
                    if (constraint == null || constraint.length() == 0) { 
                        results.count = arrayListNames.size(); 
                        results.values = arrayListNames; 

                        temparrayPictures = arrayPictures;
                        temparrayListNames = arrayListNames;
                    } else { 
                        constraint = constraint.toString().toLowerCase(); 
                        for (int i = 0; i < mOriginalNames.size(); i++) { 
                            String dataNames = arrayName.get(i); 
                            String picNames =  arrayPicture.get(i);
                            if (dataNames.toLowerCase().startsWith(constraint.toString()))  { 
                                temparrayPictures.add(picNames);
                                temparrayListNames.add(dataNames);
                            } 
                        } 

                        results.count = temparrayListNames.size(); 
                        System.out.println(results.count); 

                        results.values = temparrayListNames; 
                        Log.e("VALUES", results.values.toString()); 
                    } 

                    return results; 
                } 
            }; 

            return filter; 
        } 

    } 
1
  • Although, It needs more memory for temp lists but anyways good for few requirements. mOriginalNames should be arrayName
    – Jambaaz
    Sep 14, 2013 at 18:55
4

Try this:

public class TagFriendsAdapter extends BaseAdapter implements Filterable
{

    List<String> arrayListNames;
    List<String> mOriginalNames;

    List<String> arrayPictures;
    List<String> mOriginalPictures;

    Activity activity;
    String[] fetFriendID;
    String[] fetFriendName;
    String[] fetFriendPicture;

    LayoutInflater inflater = null;
    ImageLoader imageLoader;

    private Hashtable<String, String> picturesMap = new Hashtable<String, String>();

    public void setNamesAndPictures(List<String> arrayListNames, List<String> arrayPictures) {
        for(int i = 0; i < arrayListNames.size(); i++){
            picturesMap.put(arrayListNames.get(i), arrayPictures.get(i));
        }

        this.arrayListNames = arrayListNames;
    }

    TagFriendsAdapter(Activity a, String[] stringUID, String[] stringName, String[] stringPicture,
            ArrayList<String> arrayName, ArrayList<String> arrayPicture) {

        activity = a;
        fetFriendID = stringUID;
        fetFriendName = stringName;
        fetFriendPicture = stringPicture;

        this.arrayListNames = arrayName;
        this.arrayPictures = arrayPicture;

        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader = new ImageLoader(activity.getApplicationContext());
    }

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

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

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

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if(convertView == null)
            vi = inflater.inflate(R.layout.friends_grid_items, null);

        ImageView imgProfilePicture = (ImageView) vi.findViewById(R.id.imgProfilePicture);
        TextView txtUserName = (TextView) vi.findViewById(R.id.txtUserName);

        txtUserName.setText(arrayListNames.get(position));

        String url = picturesMap.get(arrayListNames.get(position));
        if(url != null){
            imageLoader.DisplayImage(url, imgProfilePicture);
        }
        else if(arrayPictures.get(position) == null){
            imgProfilePicture.setVisibility(View.GONE);
        }

        return vi;
    }

    @Override
    public Filter getFilter() {

        Filter filter = new Filter() {

            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {

                arrayListNames = (List<String>) results.values;
                notifyDataSetChanged();
            }

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {

                FilterResults results = new FilterResults();
                ArrayList<String> FilteredArrayNames = new ArrayList<String>();

                if(mOriginalNames == null && mOriginalPictures == null){
                    mOriginalNames = new ArrayList<String>(arrayListNames);
                    mOriginalPictures = new ArrayList<String>(arrayPictures);
                }
                if(constraint == null || constraint.length() == 0){
                    results.count = mOriginalNames.size();
                    results.values = mOriginalNames;
                }
                else{
                    constraint = constraint.toString().toLowerCase();
                    for(int i = 0; i < mOriginalNames.size(); i++){
                        String dataNames = mOriginalNames.get(i);
                        if(dataNames.toLowerCase().startsWith(constraint.toString())){
                            FilteredArrayNames.add(dataNames);
                        }
                    }

                    results.count = FilteredArrayNames.size();
                    System.out.println(results.count);

                    results.values = FilteredArrayNames;
                    Log.e("VALUES", results.values.toString());
                }

                return results;
            }
        };

        return filter;
    }

}
6
  • I am sorry, but what FilterResults class? You mean the source that is sent to this adapter? May 28, 2012 at 9:10
  • And the fact that my 'arrayPictures' never changes is the source of the problem. My filtering is done on the user's name. How do I link the name with the position of the picture is the problem I suspect. May 28, 2012 at 9:18
  • @SiddharthLele Create a hash that store URL with user name. Then when you apply the name you will have access to the image URL May 28, 2012 at 9:24
  • @SiddharthLele yep! If you got less then 10,000 records it will do the job, if you got more then that, then use database May 28, 2012 at 9:28
  • Hmm... Could you point me to an example, or suggest changes in my own code? I had added the original activity earlier when started the thread and can add it back up if you would want it. May 28, 2012 at 9:30
1

@MKJParekh did a great job!To sum up his implementation:

  1. Declare a temporary List of objects at the beginning of the class that reflect the incoming data used for each item (1 object per item).
  2. In the performFiltering()set the condition for nulls and empty strings, and also if the constraint matches the string value of the corresponding object, then add the object to the temp List. That way you have only a "filtered" list of objects.
  3. Use data from your temp list, rather than your data source (as you normally would), for methods such as getCount(), getItem(), and getView(). Otherwise, you'll just be loading ALL the items everytime, since untimately notifyDataSetChanged() will be called after each filtering.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.