Filtering A Listview With Baseadapter Filters Text Not Images
Solution 1:
First refactor your code. Create a class that holds name, picture and other friend data together.
classFriend{
publicString name;
publicString picture;
... /* more members and access methods*/
};
Then modify your adapter and filtering code accordingly.
FilterResults
should contain theArrayList<Friend>
, i.e. a list ofFriend
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 thefriendsList
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);
}
Solution 2:
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
publicstatic List<String> temparrayPictures;
publicstatic List<String> temparrayListNames;
publicclassTagFriendsAdapterextendsBaseAdapterimplementsFilterable {
List<String> arrayListNames;
List<String> arrayPictures;
Activity activity;
String[] fetFriendID;
String[] fetFriendName;
String[] fetFriendPicture;
LayoutInflaterinflater=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 = newImageLoader(activity.getApplicationContext());
}
publicintgetCount() {
return temparrayListNames.size();
}
public Object getItem(int position) {
return position;
}
publiclonggetItemId(int position) {
return position;
}
@OverridepublicvoidnotifyDataSetChanged() {
super.notifyDataSetChanged();
}
public View getView(finalint position, View convertView, ViewGroup parent) {
Viewvi= convertView;
if(convertView == null)
vi = inflater.inflate(R.layout.friends_grid_items, null);
ImageViewimgProfilePicture= (ImageView)vi.findViewById(R.id.imgProfilePicture);
TextViewtxtUserName= (TextView)vi.findViewById(R.id.txtUserName);
txtUserName.setText(temparrayListNames.get(position));
if (temparrayPictures.get(position) != null){
imageLoader.DisplayImage(temparrayPictures.get(position), imgProfilePicture);
}
elseif (temparrayPictures.get(position) == null) {
imgProfilePicture.setVisibility(View.GONE);
}
return vi;
}
@Overridepublic Filter getFilter() {
Filterfilter=newFilter() {
@SuppressWarnings("unchecked")@OverrideprotectedvoidpublishResults(CharSequence constraint, FilterResults results) {
notifyDataSetChanged();
}
@Overrideprotected FilterResults performFiltering(CharSequence constraint) {
FilterResultsresults=newFilterResults();
temparrayPictures.clear();
temparrayListNames.clear();
if (temparrayListNames == null && temparrayPictures == null) {
temparrayListNames = newArrayList<String>(arrayListNames);
temparrayPictures = newArrayList<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 (inti=0; i < mOriginalNames.size(); i++) {
StringdataNames= arrayName.get(i);
StringpicNames= 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;
}
}
Solution 3:
Try this:
publicclassTagFriendsAdapterextendsBaseAdapterimplementsFilterable
{
List<String> arrayListNames;
List<String> mOriginalNames;
List<String> arrayPictures;
List<String> mOriginalPictures;
Activity activity;
String[] fetFriendID;
String[] fetFriendName;
String[] fetFriendPicture;
LayoutInflaterinflater=null;
ImageLoader imageLoader;
private Hashtable<String, String> picturesMap = newHashtable<String, String>();
publicvoidsetNamesAndPictures(List<String> arrayListNames, List<String> arrayPictures) {
for(inti=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 = newImageLoader(activity.getApplicationContext());
}
@OverridepublicintgetCount() {
return arrayListNames.size();
}
@Overridepublic Object getItem(int position) {
return position;
}
@OverridepubliclonggetItemId(int position) {
return position;
}
@OverridepublicvoidnotifyDataSetChanged() {
super.notifyDataSetChanged();
}
@Overridepublic View getView(finalint position, View convertView, ViewGroup parent) {
Viewvi= convertView;
if(convertView == null)
vi = inflater.inflate(R.layout.friends_grid_items, null);
ImageViewimgProfilePicture= (ImageView) vi.findViewById(R.id.imgProfilePicture);
TextViewtxtUserName= (TextView) vi.findViewById(R.id.txtUserName);
txtUserName.setText(arrayListNames.get(position));
Stringurl= picturesMap.get(arrayListNames.get(position));
if(url != null){
imageLoader.DisplayImage(url, imgProfilePicture);
}
elseif(arrayPictures.get(position) == null){
imgProfilePicture.setVisibility(View.GONE);
}
return vi;
}
@Overridepublic Filter getFilter() {
Filterfilter=newFilter() {
@SuppressWarnings("unchecked")@OverrideprotectedvoidpublishResults(CharSequence constraint, FilterResults results) {
arrayListNames = (List<String>) results.values;
notifyDataSetChanged();
}
@Overrideprotected FilterResults performFiltering(CharSequence constraint) {
FilterResultsresults=newFilterResults();
ArrayList<String> FilteredArrayNames = newArrayList<String>();
if(mOriginalNames == null && mOriginalPictures == null){
mOriginalNames = newArrayList<String>(arrayListNames);
mOriginalPictures = newArrayList<String>(arrayPictures);
}
if(constraint == null || constraint.length() == 0){
results.count = mOriginalNames.size();
results.values = mOriginalNames;
}
else{
constraint = constraint.toString().toLowerCase();
for(inti=0; i < mOriginalNames.size(); i++){
StringdataNames= 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;
}
}
Solution 4:
@MKJParekh did a great job!To sum up his implementation:
- 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).
- 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. - Use data from your temp list, rather than your data source (as you normally would), for methods such as
getCount()
,getItem()
, andgetView()
. Otherwise, you'll just be loading ALL the items everytime, since untimatelynotifyDataSetChanged()
will be called after each filtering.
Post a Comment for "Filtering A Listview With Baseadapter Filters Text Not Images"