gpt4 book ai didi

android - 使用 Baseadapter 过滤文本而不是图像过滤 ListView

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:51:38 26 4
gpt4 key购买 nike

较早的问题有了一些进展。现在有一个新的问题。 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;
}

}

和截图:

enter image description here

最佳答案

首先重构您的代码。创建一个将姓名、照片和其他 friend 数据保存在一起的类。

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;

  • 更改 getViewfriendsList 访问数据的方法对象列表。

经过这些更改后,代码看起来会更好,工作也会更好。

更新:

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

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

关于android - 使用 Baseadapter 过滤文本而不是图像过滤 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10764675/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com