过滤一个ListView与Baseadapter过滤文字不是图像图像、文字、不是、ListView

2023-09-04 09:59:17 作者:瓬洎鱾

有一些进展,先前的问题进行。现在出现了新的问题。在GridView的文本显示正确的结果。然而,图像中的相同在列表的开头。

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.

例如:如果我搜索的 SIDD 的它显示了三个结果,但照片还是开始为用户以 A 。附件是一个屏幕截图的清晰度。

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.

这是BaseAdapter code:

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

}

和截图:

推荐答案

首先重构code。创建包含姓名,照片和其他朋友在一起的数据类。

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

然后修改您的适配器和过滤code相应。

Then modify your adapter and filtering code accordingly.

FilterResults 应该包含的ArrayList&LT;朋友&GT; ,即列表朋友的对象,而不仅仅是名字。

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

在适配器,取代

名单,其中,字符串&GT; arrayListNames; 名单,其中,字符串&GT; arrayPictures;

名单,其中;朋友&GT; friendsList;

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

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

这些变化之后,code会更好看,更好​​地工作。

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

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

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

public Object getItem(int position) {
    return mFriendsList.get(position);
}