android


Unable to filter listview in fragment


I am working on a project that has only one activity and multiple fragments. One of the fragment has a lisView that is populated using a Custom Adapter and Model.
i have used searchview (toolbar menu) to get the query and filter the contents in listview.
for some reason filter is not working.
public class CustomAdapater extends BaseAdapter implements Filterable {
private final String taglaunch = "TAGLAUNCH";
ArrayList<SingleRow> list;
Context context;
String[] menuTitles;
ArrayList<SingleRow>filteredData;
Filter filter;
CustomAdapater(Context context){
this.context=context;
list = new ArrayList<SingleRow>();
Resources resources = context.getResources();
menuTitles = resources.getStringArray(R.array.Banks);
int[] menuIcons = {R.mipmap.ic_allahabadbank_logo,R.mipmap.ic_andhrabank_logo,R.mipmap.ic_axisbank_logo,R.mipmap.ic_bankofbaroda_logo,
R.mipmap.ic_bankofindia_logo,R.mipmap.ic_bankofmaharashtra_logo, R.mipmap.ic_bharatiyamahilabank_logo,R.mipmap.ic_canarabank_logo
,R.mipmap.ic_capitallocalareabank_logo,R.mipmap.ic_centralbankofindia_logo};
for(int i = 0; i<menuIcons.length; i++){
list.add(new SingleRow(menuTitles[i],menuIcons[i]));
}
filteredData = new ArrayList<SingleRow>();
filteredData.addAll(list);
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d(taglaunch,"Am i called post filter");
View row = convertView;
ViewHolder ViewHolder = null;
if(row==null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.singlerow,parent,false);
ViewHolder = new ViewHolder(row);
row.setTag(ViewHolder);
}
else{
ViewHolder = (ViewHolder) row.getTag();
}
SingleRow temp = list.get(position);
ViewHolder.BankHeaderTitle.setText(temp.menu);
ViewHolder.linkNotification.setText( context.getResources().getString(R.string.Click_here));
ViewHolder.bankLogo.setImageResource(temp.menuIcons);
return row;
}
#Override
public Filter getFilter() {
if(filter==null){
filter = new MyFilter();
}
return filter;
}
class SingleRow{
String menu;
int menuIcons;
SingleRow(String menu,int menuIcons){
this.menu = menu;
this.menuIcons= menuIcons;
}
}
class ViewHolder{
TextView BankHeaderTitle;
TextView linkNotification;
ImageView bankLogo;
ViewHolder(View v){
BankHeaderTitle = (TextView) v.findViewById(R.id.BankHeaderTitle);
linkNotification = (TextView) v.findViewById(R.id.linkNotification);
bankLogo = (ImageView) v.findViewById(R.id.bankLogo);
}
}
public class MyFilter extends Filter{
#Override
protected FilterResults performFiltering(CharSequence constraint) {
//creating filter results object which needs to returned.
FilterResults results = new FilterResults();
ArrayList<SingleRow> filterResults = new ArrayList<SingleRow>();
String searchResult;
if(constraint!=null&&constraint.length()>0)
searchResult = constraint.toString().toLowerCase();
else{
results.count = filteredData.size();
results.values= filteredData;
return results;
}
int count = filteredData.size();
String filterableString;
for (int i = 0; i < count; i++) {
filterableString = filteredData.get(i).menu;
if (filterableString.toLowerCase().contains(searchResult)) {
Log.d(taglaunch,"adding " +filterableString);
SingleRow singleRow = new SingleRow(filteredData.get(i).menu,filteredData.get(i).menuIcons);
filterResults.add(singleRow);
}
}
results.count = filterResults.size();
results.values= filterResults;
return results;
}
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results.count == 0)
notifyDataSetInvalidated();
else {
list = (ArrayList<SingleRow>) results.values;
Log.d(taglaunch, "Publishing " + list.get(0).menu);
new CustomAdapater(context).notifyDataSetChanged();
}
}
}
}
and my fragment is
public class Fd_Fragment extends Fragment implements SearchView.OnQueryTextListener {
private final String taglaunch = "TAGLAUNCH";
ListView listview;
SearchView searchView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
// setRetainInstance(true); //Will ignore onDestroy Method (Nested Fragments no need this if parent have it)
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_search) {
Log.d(taglaunch, "Search Icon Clicked from fragment");
return false;
}
return super.onOptionsItemSelected(item);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(taglaunch, "FD Fragment Created");
return inflater.inflate(R.layout.fd_fragment_layout, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
listview = (ListView) getActivity().findViewById(R.id.banksListView);
listview.setOnItemClickListener(new SettingListeners());
listview.setAdapter(new CustomAdapater(getActivity()));
if (savedInstanceState != null) {
((Appdrawer) getActivity()).setNavIcon();
}
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setOnQueryTextListener(this);
}
public void onResume() {
super.onResume();
String temp;
String interestType = getArguments().getString(((Appdrawer)getActivity()).interestType);
if(interestType.equals(((Appdrawer) getActivity()).fd)) {
temp = getString(R.string.fdLabel);
}
else{
temp = getString(R.string.rdLabel);
}
((Appdrawer) getActivity())
.setActionBarTitle(temp);
Toolbar mToolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((Appdrawer) getActivity()).onSupportNavigateUp();
}
});
}
public class SettingListeners implements AdapterView.OnItemClickListener{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Fragment fragment = new WebViewClass();
String selected = ((TextView) view.findViewById(R.id.BankHeaderTitle)).getText().toString();
Log.d(taglaunch,"List item clicked");
Bundle args = new Bundle();
args.putString(((Appdrawer) getActivity()).BankName, selected);
args.putString(((Appdrawer) getActivity()).interestType, getArguments().getString(((Appdrawer) getActivity()).interestType));
fragment.setArguments(args);
Log.d(taglaunch,"clicked item"+selected);
getFragmentManager().beginTransaction().replace(R.id.app_bar, fragment).addToBackStack(null).commit();
}
}
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
CustomAdapater adapter = new CustomAdapater(getActivity());
if (adapter != null && newText != null) {
adapter.getFilter().filter(newText);
return true;
}
return false;
}
}
In publishResults function call notifyDataSetChanged() without creating new Adapter

Related Links

Android- lollipop preview Nexus 5 is not starting in Genymotion emulator
Running code on the service event queue
Change margins inside a custom view
go to next activity without crash
Viewpager dynamic height for all fragment (Like google calendar)
Automatically resize images depending on display size
How to override doubletap of PhotoView
Concurrent connections with a single AndroidHttpClient instance
Compress Bitmap to ASTC runtime on Android
How to reference Item in ListView when there are more than 1 items?
How to add a new Android API/SDK to IntelliJ project?
text colors for one TextView using two Linkify's and weblinks clickable
Sliding Menu without moving header
Unable create layout with image and text for listvieitem
Send SMS to multiple contacts
Unable to return group list in xmpp with asmack version asmack-android-8-4.0.6.jar

Categories

HOME
microsoftgraph
hyperlink
gdb
developer-tools
twig
package
xbee
hugo
lanczos
teechart
jquery-ui-sortable
vimdiff
ycsb
selectize.js
keytool
fileserver
sigma.js
perlbrew
opentk
gpio
licensing
realm-mobile-platform
dotspatial
powershell-ise
mustache
contacts
vue-chartjs
indy10
processwire
openalpr
dm-script
xdocreport
cin
mcustomscrollbar
naming
microsoft-metro
knime
jide
proget
gitlist
serde
identification
activeperl
convex-optimization
reindex
stdmap
boxing
filehandle
oozie-coordinator
cmp
py2neo
objectscript
gets
autosar
scaffolding
cifilter
android-dateutils
http-status-code-401
kango-framework
piracy-prevention
design-principles
crouton-os
libvlc
directdraw
doctype
dronekit-android
git-ftp
nsurlsessiondatatask
node-serialport
shopizer
grouping-sets
jbake
rmysql
create.js
lnk
tinyxml
maven-scm
clickbank
drupal-nodes
ecslidingviewcontroller
eclipse-kepler
artemis
mantle
device-width
pdfviewer
sentestingkit
jquery-blockui
isa-swizzling
wiimote
user-tracking
winmain
listactivity
clrstoredprocedure
instantiationexception
facebook-iframe
globals
dsl-tools
windows-identity
wspbuilder
idatareader

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App