5

以前の問題にいくらかの進展がありました。今、新たな問題があります。GridView のテキストは、正しい結果を示しています。ただし、画像はリストの最初と同じです。

例: 「Sidd 」を検索すると、3 つの結果が表示されますが、「 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 に答える