LazyList 's listview can not refresh with new arraylist - java

I m trying to use searchView with LazyList. In my lazyAdapter I m updating my arraylist , this works smoothly but my listview doesn't update with arrayList. This is my filer method. I suppose notifyDataSetChanged does not work.Please help how can I refresh my listview?
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
//list.clear();
list = new ArrayList<HashMap<String, String>>();
if (charText.length() == 0) {
list.addAll(arraylist);
}
else
{
for (HashMap<String, String> map : arraylist)
{
if (map.get("radio").toString().toLowerCase(Locale.getDefault()).contains(charText))
{
list.add(map);
}
}
}
notifyDataSetChanged();
}

The variable list holds a different object after the third line. You are changing this new ArrayList but the adapter still remembers the old one. Instead of
list = new ArrayList<HashMap<String, String>>();
write
list.clear();
You then work with the same object as before.

LazyList 's listview can not refresh with new arraylist
Because you are not adding new list in current Adapter of Listview before calling notifyDataSetChanged() :
Add list to adapter using addAll method:
listviewAdapter.addAll(list);
//notify data-source change to adapter
notifyDataSetChanged();
if addAll method not available then create a method in Adapter class and pass list as parameter then call add method:
public void addAll(ArrayList<HashMap<String, String>> data) {
for(String item: data) {
add(item);
}
notifyDataSetChanged();
}

public class MovieListAdapter extends BaseAdapter {
private Context context;
ArrayList<HashMap<String, String>> movielist;
private ArrayList<HashMap<String, String>> listAdapter;
public MovieListAdapter(Context context, ArrayList<HashMap<String, String>> movielist) {
this.context = context;
this.movielist = movielist;
this.listAdapter = new ArrayList<HashMap<String, String>>();
this.listAdapter.addAll(movielist);
}
#Override
public int getCount() {
return movielist.size();
}
#Override
public Object getItem(int position) {
return movielist.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View griView = inflater.inflate(R.layout.movie_listitem, null);
TextView textview = (TextView) griView.findViewById(R.id.catname);
textview.setText(movielist.get(position).get("name"));
textview.setSelected(true);
ImageView imageView = (ImageView) griView.findViewById(R.id.catimage);
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels / 3;
int height = metrics.widthPixels / 3;
// System.out.println(movielist.get(position).getMoviepicture());
if (movielist.get(position).get("image").equals("")) {
imageView.setImageResource(R.drawable.blankart);
} else {
Picasso.with(context)
.load(movielist.get(position).get("image"))
.resize(width, height).placeholder(R.drawable.blankart)
.noFade().into(imageView);
}
return griView;
}
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
movielist.clear();
if (charText.length() == 0) {
movielist.addAll(listAdapter);
} else {
for (HashMap<String, String> si : listAdapter) {
if (si.get("name").toLowerCase(Locale.getDefault())
.contains(charText)) {
movielist.add(si);
}
}
}
notifyDataSetChanged();
}
}
make you adapter like this

Related

How to get data from hashmap into adapter from json?

I try to get data from hashmap in to my CustomArrayAdapter, my hashmap had data is key and value pair is type and image according type and i want to get image when i click item gridview.
i had a trouble in my adapter.
Thank advance.
So this is my Adapter.
Adapter_subCategories.java
public class Adapter_subCategories extends BaseAdapter {
LayoutInflater inflater;
Context context;
ArrayList<HashMap<String,String>> wallpaperArrayList;
public Adapter_subCategories(LayoutInflater inflater, Context context, ArrayList<HashMap<String,String>> array) {
this.inflater = inflater;
this.context = context;
this.wallpaperArrayList = array;
}
public Adapter_subCategories(Item_categories_show item_categories_show, ArrayList<HashMap<String, String>> mapArrayList) {
}
public Adapter_subCategories(Item_categories_show item_categories_show, HashMap<String, ArrayList<Integer>> arrayListHashMap) {
}
#Override
public int getCount() {
return wallpaperArrayList.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater==null)
{
inflater = LayoutInflater.from(context);
}
if (convertView==null){
convertView = inflater.inflate(R.layout.list_images_subcategories,null);
}
ImageView imageView = convertView.findViewById(R.id.imageView_subcategories);
HashMap<String,String> maps = new HashMap<String, String>();
maps = wallpaperArrayList.get(position);
// this is my trouble
String type = maps.get("rootType");
String image = maps.get("url");
// maps = wallpaperArrayList.get(position);
imageView.setImageResource(Integer.parseInt(maps.get("url")));
return convertView;
}
}
My hashmap:
for (int j = 0; j<subArray.length() ; j++) {
JSONObject obj = subArray.getJSONObject(j);
JSONObject row = obj.getJSONObject("row");
String url = row.getString("url");
HashMap<String,String> map = new HashMap<String, String>();
map.put(rootType,url);
hashMapArrayList.add(map);
my json : http://www.vnsupa.com/dulieujson/data.json
I Think your problem is in getItem() and getItemId() both of methods. Change both of methods signature like
#Override
public Object getItem(int position) {
return wallpaperArrayList.get(position);//return your object
}
#Override
public long getItemId(int position) {
return position;//return position
}

recyclerview notifyDataSetChanged() is not working

I am trying to update myadapter using notifyDataSetchanged() in updateList().But not working for recyclerview. I can able to see list size in updateList() and its displaying actual result but only reclerview is not updating.This class was designed in MVVM Design Pattern.
public class TasksAdapter extends RecyclerView.Adapter<TasksAdapter.TasksAdapterViewHolder> {
private static final String TAG = TasksAdapter.class.getSimpleName();
static ArrayList<ItemModel> list;
static Context mContext;
private Fragment fragment;
private int row_index = -1;
private SessionManager session;
// SelectColorVM colorVM;
public TasksAdapter(Fragment fragment, Context context, ArrayList<ItemModel> itemList) {
this.mContext = context;
this.list = itemList;
this.fragment = fragment;
session = new SessionManager(fragment.getContext());
}
public void updateList(ArrayList<ItemModel> itemList){
list.clear();
this.list = itemList;
notifyDataSetChanged();
}
#Override
public TasksAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
TasksItemBinding binding =
DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.tasks_item,
parent, false);
return new TasksAdapter.TasksAdapterViewHolder(binding);
}
#Override
public void onBindViewHolder(TasksAdapterViewHolder holder, final int position) {
holder.binding.setViewModel(list.get(position));
if(position == 0 && list.get(position).btnText.get().toString().length() > 0)
holder.binding.btnUpdate.setVisibility(View.VISIBLE);
else
holder.binding.btnUpdate.setVisibility(View.INVISIBLE);
holder.binding.layoutItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// row_index = position;
// notifyDataSetChanged();
Fragment fragment = new DetailFragment();
FragmentManager manager = ((FragmentActivity) mContext).getSupportFragmentManager();
FragmentTransaction fragmentTransaction = manager.beginTransaction();
Bundle bundle = new Bundle();
ItemModel item = list.get(position);
bundle.putSerializable("item", item);
fragment.setArguments(bundle);
fragmentTransaction.replace(R.id.id_frame, fragment, "details");
fragmentTransaction.addToBackStack("details");
fragmentTransaction.commit();
}
});
holder.binding.btnUpdate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
HashMap<String, String> user = session.getUserDetails();
if(list.get(position).btnText.get().equals("Task Start")){
LandingEngine.updateStatusAndSetStartTimeForRequest(fragment, user.get(SessionManager.KEY_CLEANER_ID), list.get(position).sid.get().toString());
} else if(list.get(position).btnText.get().equals("Task Complete")){
LandingEngine.updateStatusAndSetCompleteTimeForRequest(fragment, user.get(SessionManager.KEY_CLEANER_ID), list.get(position).sid.get().toString());
}
}
});
}
#Override
public int getItemCount() {
return list.size();
}
public static class TasksAdapterViewHolder extends RecyclerView.ViewHolder {
TasksItemBinding binding;
public TasksAdapterViewHolder(TasksItemBinding rowbinding) {
super(rowbinding.getRoot());
this.binding = rowbinding;
}
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return position;
}
}
I am calling adapter from my fragment like below
if(list.size() > 0) {
if(adapter == null) {
view.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(fragment.getContext());
view.setLayoutManager(mLayoutManager);
adapter = new TasksAdapter(fragment, fragment.getContext(), list);
view.setAdapter(adapter);
view.addItemDecoration(new DividerItemDecoration(fragment.getContext()));
} else {
adapter.updateList(list);
// view.getAdapter().notifyDataSetChanged();
// view.invalidate();
}
}
Please check this and let me know where i am doing wrong here.Thanks in advance..
Finally its working by calling this method from fragment class once getting API response from the volley library.
I have written following method in ViewModel class.
public void updateList(ArrayList<Class> list){
if(adapter != null) {
adapter.updateList(list);
}
}
And calling this method from fragment class like following.
ArrayList<StatisticsModel> list = (ArrayList) data;
viewModel.updateList(list);
So in the adapter class update method is following.
public void updateList(ArrayList<StatisticsModel> itemList){
list.clear();
// this.list = itemList;
// list.addAll(itemList);
// notifyItemInserted(list.size());
// notifyDataSetChanged();
this.list.addAll(itemList);
// notifyItemInserted(list.size());
notifyDataSetChanged();
}
Don't
public void updateList(ArrayList<ItemModel> itemList)
{
list.clear();
this.list = itemList;
notifyDataSetChanged();
}
Whenever calling this method list.clear(); clear all value.
The clear() method is used to remove all of the elements from a list.
DO
public void updateList(ArrayList<ItemModel> itemList)
{
this.list.addAll(itemList);
notifyDataSetChanged();
}
I have faced the same problem, you are doing mistake in this, change list into itemList:
#Override
public int getItemCount() {
return itemList.size();
}
Try this one
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
view.adapter.notifyDataSetChanged();
}
});
Try to add notifyDataSetChanged() in your Fragment class like...
adapter.notifyDataSetChanged();
try this remove list.clear(); from your updateList method
public void updateList(ArrayList<ItemModel> itemList){
this.list.addAll(itemList);
notifyItemInserted(list.size());
notifyDataSetChanged();
}
notifyItemInserted(int position)
Notify any registered observers that the item reflected at position has been newly inserted.
also try this
list.addAll(your new list);
if (adapter == null) {
view.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(fragment.getContext());
view.setLayoutManager(mLayoutManager);
adapter = new TasksAdapter(fragment, fragment.getContext(), list);
view.setAdapter(adapter);
view.addItemDecoration(new DividerItemDecoration(fragment.getContext()));
} else {
view.notifyItemInserted(list.size());
view.notifyDataSetChanged();
}
have a same issue. In my case there was error message in the logcat RecyclerView: No layout manager attached; skipping layout
which i have fixed with following code
LinearLayoutManager llm = new LinearLayoutManager(this.getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(llm);
then the notifyDataSetChanged start work
I don't know why sometimes we are not creating two instances of the adapter and also we are on the main thread adapter.notifyDataSetChanged() is not working In this case just call the notifyDataSetChanged() method from the Adapter class.
adapter.setNewValue(newlist)
and on AdapterClass
fun setNewValue(newlist:ArrayList<Any>) {
list = newlist
notifyDataSetChanged()
}

return Changed arrayList from adapter android

have gridview for which set adapter called ImageAdapter with parameter of arraylist. Inside the adapter have onclicklistener during which from the arraylist one item is removed and then when i use this line ImageAdapter.notifyDataSetChanged in the gridview item is removed. Now i need the changed arrayList in my activity so how can i get it.
Here's my code:
public class ImageAdapter extends BaseAdapter {
Context context;
ArrayList<String> listCheck = new ArrayList<String>();
ImageAdapter adapter = this;
public ImageAdapter(Context context, ArrayList<String> list) {
this.context = context;
listCheck = list;
}
#Override
public int getCount() {
return listCheck.size();
}
#Override
public Object getItem(int arg0) {
return null;
}
#Override
public long getItemId(int arg0) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder121 holder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(
R.layout.item_gridmain, null);
holder = new ViewHolder121();
holder.imageView = (ImageView) convertView
.findViewById(R.id.img_selected_image);
holder.close = (ImageButton) convertView
.findViewById(R.id.img_btn_cancel);
convertView.setTag(holder);
}
else {
holder = (ViewHolder121) convertView.getTag();
}
holder.close.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg1) {
listCheck.remove(position);
adapter.notifyDataSetChanged();
}
}); Bitmap bm = decodeSampledBitmapFromUri(listCheck.get(position), 220,
220);
holder.imageView.setImageBitmap(bm);
return convertView;
} }
Fragment code:
ImageAdapter mainActivityAdapter = new ImageAdapter(getActivity(), ar1);
gridview_withimage.setAdapter(mainActivityAdapter);
question: How to get changed arraylist from ImageAdapter to called Fragement
How to get changed arraylist from ImageAdapter to called Fragement
Create a method in ImageAdapter which will return ArrayList used as data-source in Adapter:
public ArrayList<String> getModifyList() {
return listCheck;
}
Call getModifyList method in Fragment for getting ArrayList using Adapter object:
gridview_withimage.setAdapter(mainActivityAdapter);
gridview_withimage.postDelayed(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
ArrayList<String> arrNewList=mainActivityAdapter.getModifyList();
}
}, 100);
You can pass Fragment instance to Adapter.
private Fragment fragment;
public ImageAdapter(Context context, ArrayList<String> list,Fragment mfragment) {
this.context = context;
listCheck = list;
this.fragment=mfragment;
}
and Make one public method in your fragment and call using this instance of Fragment fragment;
You can create an interface like this
public interface ImageAdapterListener{
void onListChange(List<String> list);
}
And declare a member in ImageAdapter
private ImageAdapterListener listener;
public setListener(ImageAdapterListener listener){
this.listener = listener}
override the notifyDataSetChanged
#Override
void notifyDataSetChanged{
super.notifyDataSetChanged();
if(listener!= null) listener.onListChange(listCheck);
}
and make your fragment implement that interface.
class Myfragment extends Fragment implements ImageAdapterListener {
ImageAdapter mainActivityAdapter = new ImageAdapter(getActivity(), ar1);
gridview_withimage.setAdapter(mainActivityAdapter);
mainActivityAdapter.setListener(this);
#Override
void onListChange(List<String> list){
//do your stuff
}
}

Getting wrong position after filter listview

My question is when i filter listview with baseadapter than i got perfect result but after click on this item,i can't get related values of that item but i get wrong position item so notifydatasetchenged not work.
'public ConsultationAdpater(Context context, ArrayList<Doctor> doctors) {
this.context = context;
this.doctorList = doctors;
this.mStringFilterList = doctors;
getFilter();
imageLoader = ImageLoader.getInstance();
this.inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// set options for image display
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.activity_indicator)
.showImageForEmptyUri(R.drawable.image_not_available)
.showImageOnFail(R.drawable.image_not_available)
.resetViewBeforeLoading(true).cacheInMemory(true)
.cacheOnDisk(true).considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565).build();
}
#Override
public int getCount() {
return doctorList.size();
}
#Override
public Object getItem(int position) {
return doctorList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.row_consult, parent, false);
holder = new ViewHolder();
holder.image = (ImageView) convertView
.findViewById(R.id.img_row_const);
holder.txtName = (TextView) convertView
.findViewById(R.id.tDtNm_row_const);
holder.txtSpeciality = (TextView) convertView
.findViewById(R.id.tDtPt_row_const);
holder.txtPrice = (TextView) convertView
.findViewById(R.id.tDtPr_row_const);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Doctor doctor = doctorList.get(position);
holder.txtName.setText(doctor.base_user.first_name);
holder.txtSpeciality.setText(doctor.specialization);
holder.txtPrice.setText(doctor.cost_per_minute + "$/min");
if (images[position] == null) {
holder.image.setImageResource(R.drawable.image_not_available);
} else {
imageLoader.displayImage(
"http://37.252.121.94/" + images[position], holder.image,
options);
}
return convertView;
}
public void switchDoctorList(ArrayList<Doctor> doctors, String[] images) {
this.doctorList = doctors;
this.mStringFilterList = doctors;
this.images = images;
this.notifyDataSetChanged();
}
public void switchDoctorList(ArrayList<Doctor> doctors) {
this.doctorList = doctors;
this.mStringFilterList = doctors;
this.notifyDataSetChanged();
}
private static class ViewHolder {
ImageView image;
TextView txtName;
TextView txtSpeciality;
TextView txtPrice;
}
#Override
public Filter getFilter() {
if (valueFilter == null) {
valueFilter = new ValueFilter();
}
return valueFilter;
}
private class ValueFilter extends Filter {
// Invoked in a worker thread to filter the data according to the
// constraint.
#Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
ArrayList<Doctor> filterList = new ArrayList<Doctor>();
for (int i = 0; i < mStringFilterList.size(); i++) {
if ((mStringFilterList.get(i).specialization.toUpperCase())
.contains(constraint.toString().toUpperCase())) {
filterList.add(mStringFilterList.get(i));
}
}
results.count = filterList.size();
results.values = filterList;
} else {
results.count = mStringFilterList.size();
results.values = mStringFilterList;
}
return results;
}
// Invoked in the UI thread to publish the filtering results in the user
// interface.
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
doctorList = (ArrayList<Doctor>) results.values;
notifyDataSetChanged();
}
}
}'
This worked for me
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
//abrirActividadDetallada(position, id);
Object MyObject=(Object) parent.getAdapter().getItem(position);
CustomMethod(MyObject);
}
});
the ideology of filtering in adapters works by replacing the original data list int he adapter with the filtered list. The performFiltering method will tell you what elements in the data list match your filter. But not this list makes the primary data list for your adapter instead of the original data list. So you shoudl keep 2 lists in your adapter.
The original unfiltered list. for reference
The second list which feeds data to the adapter. getView and getItems etc. methods should use this list.
When you do performFiltering use the original unfiltered list to extract matching data elements and save in the second list. That way you will never go wrong.
Sample Example adapter for reference
public class CompanyQuotesResultAdapter extends BaseAdapter{
//original data populated from DB or web service.
private ArrayList<MyDataVO> originalData;
//the data list which the adapter uses for its work
private ArrayList<MyDataVO> data;
private LayoutInflater inflater = null;
private Fragment parentFragment;
private Filter dataFilter;
private int quoteGreenColor = -1;
public CompanyQuotesResultAdapter(Fragment parentFragment){
//set values here
}
public ArrayList<MyDataVO> getData() {
return new ArrayList<MyDataVO>(this.data);
}
public ArrayList<MyDataVO> getOriginalData() {
return new ArrayList<MyDataVO>(this.originalData);
}
public void addDataVOsWithoutNotification(List<MyDataVO> dataVOs){
this.data.addAll(dataVOs);
this.originalData.addAll(dataVOs);
}
public void setData(List<MyDataVO> data) {
this.data = new ArrayList<MyDataVO>(data);
this.originalData = new ArrayList<MyDataVO>(data);
this.notifyDataSetChanged();
}
public boolean isEmpty() {
return this.data.isEmpty();
}
public void clearAll(){
this.originalData.clear();
this.data.clear();
this.notifyDataSetChanged();
}
public void clearAllWithoutNotification(){
this.data.clear();
this.originalData.clear();
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
return data.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public Filter getFilter(){
return dataFilter;
}
//Filtering class
private class ArrayFilter extends Filter {
#Override
protected FilterResults performFiltering(CharSequence prefix) {
FilterResults results = new FilterResults();
if (prefix == null || prefix.length() == 0) {
ArrayList<MyDataVO> list = new ArrayList<MyDataVO>(originalData);
results.values = list;
results.count = list.size();
} else {
String prefixString = prefix.toString().toLowerCase(Locale.ENGLISH);
ArrayList<MyDataVO> values = new ArrayList<MyDataVO>(originalData);
final int count = values.size();
final ArrayList<MyDataVO> newValues = new ArrayList<MyDataVO>();
for (int i = 0; i < count; i++) {
final MyDataVO resultRowVO = values.get(i);
final String valueText = resultRowVO.getCompanyName().toLowerCase(Locale.ENGLISH);
// First match against the whole, non-splitted value
if (valueText.contains(prefixString)) {
newValues.add(resultRowVO);
}else{
final String codeValueText = resultRowVO.getCompanyCode().toLowerCase(Locale.ENGLISH);
if (codeValueText.contains(prefixString)) {
newValues.add(resultRowVO);
}
}
}
results.values = newValues;
results.count = newValues.size();
}
return results;
}
protected void publishResults(CharSequence constraint, FilterResults results) {
data = (ArrayList<MyDataVO>) results.values;
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}

Filter a Listview using a spinner in android

I have this Listview that contains both text and images. Each list item contains four key elements. I want to be able to filter the listview using either of the three TextViews by an option selected from the spinner. This is what I have done so far:
public class LazyVenueAdapter extends BaseAdapter implements Filterable {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
public LazyVenueAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.venue_list_item, null);
TextView title = (TextView) vi.findViewById(R.id.venueName);
TextView location = (TextView) vi.findViewById(R.id.venueLocation);
TextView tags = (TextView) vi.findViewById(R.id.venueTags);
ImageView thumb_image = (ImageView) vi.findViewById(R.id.venueImage);
HashMap<String, String> venue = new HashMap<String, String>();
venue = data.get(position);
// Setting all values in listview
title.setText(venue.get(VenuesFragment.KEY_TITLE));
location.setText(venue.get(VenuesFragment.KEY_LOCATION));
tags.setText(venue.get(VenuesFragment.KEY_TAGS));
imageLoader.DisplayImage(venue.get(VenuesFragment.KEY_THUMB_URL),
thumb_image);
return vi;
}
#Override
public Filter getFilter() {
Filter filter = new Filter() {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
ArrayList<HashMap<String, String>> filteredArrayVenues = new ArrayList<HashMap<String, String>>();
data.clear();
if (constraint == null || constraint.length() == 0) {
results.count = data.size();
results.values = data;
} else {
constraint = constraint.toString();
for (int index = 0; index < data.size(); index++) {
HashMap<String, String> dataVenues = data.get(index);
if (dataVenues.get(VenuesFragment.KEY_TAGS).toString().startsWith(
constraint.toString())) {
filteredArrayVenues.add(dataVenues);
}
}
results.count = filteredArrayVenues.size();
System.out.println(results.count);
results.values = filteredArrayVenues;
Log.e("VALUES", results.values.toString());
}
return results;
}
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
data = (ArrayList<HashMap<String, String>>) results.values;
notifyDataSetChanged();
}
};
return filter;
}
}
The problem that I am having is that when I select an option from the drop down list I get back an empty result set.
You don't receive any results because you delete the information that you are trying to build your results from with data.clear();. If data is now empty (ie data.size() == 0) then this loop will never execute:
for (int index = 0; index < data.size(); index++) {
Removing this line might solve your problem:
data.clear();
You have to filter your ArrayList<HashMap<String, String>> data first, then call notifyDataSetChanged() in your adapter

Categories