5

较早的问题取得了一些进展。现在有一个新问题。GridView 中的文本显示了正确的结果。但是,图像与列表开头的图像相同。

例如:如果我搜索“ Sidd ”,它会显示三个结果,但照片仍然以“ A ”开头的用户开始。为清楚起见,附上截图。

这是 BaseAdapter 代码:

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;
    }

}

和截图:

在此处输入图像描述

4

4 回答 4

9

首先重构你的代码。创建一个将姓名、图片和其他朋友数据保存在一起的类。

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

然后相应地修改您的适配器和过滤代码。

  • FilterResults应该包含ArrayList<Friend>,即Friend对象列表而不仅仅是名称。

  • 在适配器中,替换

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

    List<Friend> friendsList;

  • 更改从对象列表getView访问数据的方法。friendsList

在这些更改之后,代码将看起来更好并且工作得更好。

更新:

确保您的适配器的getItem方法返回一个Friend对象

public Object getItem(int position) {
    return mFriendsList.get(position);
}
于 2012-05-28T10:39:52.427 回答
6

问题是因为您在根据edittext输入过滤数组时没有更新图片名称列表,您还需要更新它们,

我试图修改你的代码,检查这个

    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; 
        } 

    } 
于 2012-05-28T09:36:58.977 回答
4

试试这个:

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;
    }

}
于 2012-05-28T09:04:10.813 回答
1

@MKJParekh 做得很好!总结一下他的实现:

  1. 在类的开头声明一个临时对象列表,以反映用于每个项目的传入数据(每个项目 1 个对象)。
  2. performFiltering()设置空值和空字符串的条件,并且如果约束匹配相应对象的字符串值,则将该对象添加到临时列表中。这样你就只有一个“过滤”的对象列表。
  3. 使用临时列表中的数据,而不是数据源中的数据(像往常一样),用于诸如getCount()getItem()getView(). 否则,您每次都将加载所有项目,因为notifyDataSetChanged()每次过滤后都会调用 untimely。
于 2014-04-19T19:56:38.540 回答