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

Error while showing file from a url in google docs using android web view
marshmallow permissions dialog ALLOW button not working why?
Unable to add image in a istview from the database
how to get imagelink(url) from listview onItemClickListener
on capturing image from camera the app is crashing in kitkat android
Does FragmentStatePagerAdapter save fragment state on orientation change?
Cannot resolve symbol default_web_client_id in Firebase's Android Codelab
Taking picture without camera preview not working in lolipop and above version
What could a function look like that receives three locations and three radius and outputs one coordinate showing the overlapping?
Adding Firebase Analytics and Google Analytics in Android App
Android - The Fragment is getting refreshed only when the orientation is changed
Bluetooth headset can't receive the last voice
list only updates when I scroll - android listview
Android, using Zxing into a webview, into back button, shows blank activity
Cannot delete sqlite row using a contextual menu and listview in Android
Android FireBase error when uploading picture

Categories

HOME
notepad++
ecmascript-6
pagination
pyqt
interface
knitr
sublimetext
bookmarks
android-json
azure-container-service
little-proxy
pcl-crypto
java.util.logging
activecollab
line
x-cart
code-generation
symbol
gruntfile
scsi
production
symbols
breadth-first-search
ag
cgbitmapcontext
mongoid6
direct3d11
interbase
fat
jquery-multiselect
entity-relationship-model
rpgle
browser-sync
testcomplete
google-earth
constexpr
userdefaults
dotspatial
quickfixj
login-script
getlasterror
amazon-ses
p4merge
unordered-map
setup.py
kill
numberpicker
laravel-elixir
skylink
preloader
chronicle-map
congestion-control
outputcache
http-request
procfile
httr
openal
gitlist
yii2-api
mschart
google-cse
aquamacs
sundials
cup
mouseleave
javafx-css
testrail
stax
myspace
uicolor
mta
x-tag
simevents
qiime
nservicebus5
nss
yajsw
invoke-sqlcmd
phpgrid
rasterize
vhosts
concurrentmodification
dynatree
themoviedb-api
wiredep
android-launcher
text-search
finalbuilder
amiga
dronekit-android
dnssec
cosine-similarity
psr-4
xml-namespaces
personalization
reflector
renderer
authlogic
menubar
hibernate-entitymanager
nevron
apache-abdera
google-maps-mobile
ikimagebrowserview
umra
unattended-processing
qglwidget
diagrams
interprocess
flvplayback
contentpresenter
great-circle

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