fedilab-Android-App/app/src/main/java/app/fedilab/android/drawers/TagsSearchAdapter.java

133 lines
3.9 KiB
Java
Raw Normal View History

2019-05-18 11:10:30 +02:00
package app.fedilab.android.drawers;
2017-10-09 18:35:43 +02:00
/* Copyright 2017 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2017-10-09 18:35:43 +02:00
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
2019-05-18 11:10:30 +02:00
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
2017-10-09 18:35:43 +02:00
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
2019-05-18 11:10:30 +02:00
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
2017-10-09 18:35:43 +02:00
* see <http://www.gnu.org/licenses>. */
import android.content.Context;
2019-06-11 19:38:26 +02:00
import androidx.annotation.NonNull;
2017-10-09 18:35:43 +02:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.R;
2017-10-09 18:35:43 +02:00
/**
* Created by Thomas on 09/10/2017.
* Adapter for tags when searching
*/
public class TagsSearchAdapter extends ArrayAdapter<String> implements Filterable {
private List<String> tags, tempTags, suggestions ;
private LayoutInflater layoutInflater;
public TagsSearchAdapter(Context context, List<String> tags){
super(context, android.R.layout.simple_list_item_1, tags);
this.tags = tags;
this.tempTags = new ArrayList<>(tags);
this.suggestions = new ArrayList<>(tags);
layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return tags.size();
}
@Override
public String getItem(int position) {
return tags.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
2017-11-23 18:16:47 +01:00
@NonNull
2017-10-09 18:35:43 +02:00
@Override
2018-05-11 15:26:27 +02:00
public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
2017-10-09 18:35:43 +02:00
final String tag = tags.get(position);
final ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.drawer_tag_search, parent, false);
2017-10-09 18:35:43 +02:00
holder = new ViewHolder();
2017-11-23 18:16:47 +01:00
holder.tag_name = convertView.findViewById(R.id.tag_name);
2017-10-09 18:35:43 +02:00
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tag_name.setText(String.format("#%s", tag));
2017-10-09 18:35:43 +02:00
return convertView;
}
2017-11-23 18:16:47 +01:00
@NonNull
2017-10-09 18:35:43 +02:00
@Override
public Filter getFilter() {
return searchFilter;
}
private Filter searchFilter = new Filter() {
@Override
public CharSequence convertResultToString(Object resultValue) {
String tag = (String) resultValue;
return "#" + tag;
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
if (constraint != null) {
suggestions.clear();
2018-01-13 10:41:56 +01:00
suggestions.addAll(tempTags);
2017-10-09 18:35:43 +02:00
FilterResults filterResults = new FilterResults();
filterResults.values = suggestions;
filterResults.count = suggestions.size();
return filterResults;
} else {
return new FilterResults();
}
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
ArrayList<String> c = (ArrayList<String>) results.values;
if (results.count > 0) {
clear();
2018-01-13 10:41:56 +01:00
addAll(c);
notifyDataSetChanged();
2017-10-09 18:35:43 +02:00
} else{
clear();
notifyDataSetChanged();
}
}
};
private class ViewHolder {
TextView tag_name;
}
}