i have an app in which i am displaying data from json into listview,and now want to apply searching functionality on the listview.but whenever i typing something in the editbox
Why are you apply the toString method here:
adapter.getFilter().filter(s.toString());
When the filtering method seems to need chars:
performFiltering(CharSequence constraint)
Try this way,hope this will help you to solve your problem.
Home Fragment
public class HomeActivity extends Fragment {
String countryCode;
ArrayList<ProfileBean> catagery;
EditText search;
AdaptorClass adapter;
public static String Bussinessurl;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View rootView = inflater.inflate(R.layout.homeactivity, container, false);
//Log.i("homeurl", "http://chabu.agicent.com/api/v1/get_business?token=" +Login.USERTOKEN.toString());
search = (EditText) rootView.findViewById(R.id.search);
catagery = new ArrayList<ProfileBean>();
new JSONAsyncTask()
.execute(Login.AllChabu);
ListView listview = (ListView) rootView.findViewById(R.id.list);
adapter = new AdaptorClass(getActivity(), catagery);
listview.setItemsCanFocus(false);
listview.setTextFilterEnabled(true);
listview.setAdapter(adapter);
countryCode = GetCountryZipCode();
Log.i("country code", countryCode);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long id) {
//Toast.makeText(getActivity(),
// catagery.get(position).getcategory_name(), Toast.LENGTH_LONG)
//.show();
position = position + 1;
Log.i("url", Bussinessurl);
Intent i = new Intent(getActivity(), BusinessActivity.class);
startActivity(i);
}
});
search.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable arg0) {
String text = search.getText().toString();
adapter.filter(text);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {
}
});
return rootView;
}
}
Adapter Class
public class AdaptorClass extends BaseAdapter {
private List<ProfileBean> originalData;
private List<ProfileBean> filteredData;
private Context context;
public AdaptorClass(Context context,ArrayList<ProfileBean> originalData) {
this.context=context;
this.originalData = originalData;
filteredData =new List<ProfileBean>();
filteredData.addAll(this.originalData);
}
@Override
public int getCount() {
return filteredData.size();
}
@Override
public Object getItem(int position) {
return filteredData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.row, null);
holder.tvDescription = (TextView) convertView.findViewById(R.id.tvDescriptionn);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvDescription.setText(filteredData.get(position).getcategory_name());
return convertView;
}
class ViewHolder {
public TextView tvDescription;
}
public void filter(String charText) {
filteredData.clear();
if (charText.length() == 0) {
filteredData.addAll(originalData);
} else {
for (ProfileBean bean : originalData) {
if (bean.getcategory_name().toLowerCase().contains(charText.toLowerCase())) {
filteredData.add(bean);
}
}
}
notifyDataSetChanged();
}
}