mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-12 08:58:51 -04:00
article suggestion: Changed implementation to use filter
Cleaner, and does not block ui thread while retrieving article titles from zim file
This commit is contained in:
parent
45c480b9b5
commit
fe6bb7c6a8
@ -11,6 +11,7 @@ import java.util.Arrays;
|
|||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.ActivityNotFoundException;
|
import android.content.ActivityNotFoundException;
|
||||||
import android.content.ComponentName;
|
import android.content.ComponentName;
|
||||||
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
@ -30,6 +31,8 @@ import android.webkit.WebView;
|
|||||||
import android.webkit.WebViewClient;
|
import android.webkit.WebViewClient;
|
||||||
import android.widget.ArrayAdapter;
|
import android.widget.ArrayAdapter;
|
||||||
import android.widget.AutoCompleteTextView;
|
import android.widget.AutoCompleteTextView;
|
||||||
|
import android.widget.Filter;
|
||||||
|
import android.widget.Filterable;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.TextView.OnEditorActionListener;
|
import android.widget.TextView.OnEditorActionListener;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
@ -45,6 +48,65 @@ public class KiwixMobileActivity extends Activity {
|
|||||||
private static final String PREFS_KIWIX_MOBILE = "kiwix-mobile";
|
private static final String PREFS_KIWIX_MOBILE = "kiwix-mobile";
|
||||||
|
|
||||||
|
|
||||||
|
public class AutoCompleteAdapter extends ArrayAdapter<String> implements Filterable {
|
||||||
|
private ArrayList<String> mData;
|
||||||
|
|
||||||
|
public AutoCompleteAdapter(Context context, int textViewResourceId) {
|
||||||
|
super(context, textViewResourceId);
|
||||||
|
mData = new ArrayList<String>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getCount() {
|
||||||
|
return mData.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getItem(int index) {
|
||||||
|
return mData.get(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Filter getFilter() {
|
||||||
|
Filter myFilter = new Filter() {
|
||||||
|
@Override
|
||||||
|
protected FilterResults performFiltering(CharSequence constraint) {
|
||||||
|
FilterResults filterResults = new FilterResults();
|
||||||
|
ArrayList<String> data = new ArrayList<String>();
|
||||||
|
if(constraint != null) {
|
||||||
|
// A class that queries a web API, parses the data and returns an ArrayList<Style>
|
||||||
|
try {
|
||||||
|
ZimContentProvider.searchSuggestions(constraint.toString(), 20);
|
||||||
|
String suggestion;
|
||||||
|
|
||||||
|
data.clear();
|
||||||
|
while ((suggestion = ZimContentProvider.getNextSuggestion())!=null) {
|
||||||
|
data.add(suggestion);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch(Exception e) {}
|
||||||
|
// Now assign the values and count to the FilterResults object
|
||||||
|
filterResults.values = data;
|
||||||
|
filterResults.count = data.size();
|
||||||
|
}
|
||||||
|
return filterResults;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
@Override
|
||||||
|
protected void publishResults(CharSequence contraint, FilterResults results) {
|
||||||
|
if(results != null && results.count > 0) {
|
||||||
|
notifyDataSetChanged();
|
||||||
|
mData = (ArrayList<String>)results.values;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
notifyDataSetInvalidated();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return myFilter;
|
||||||
|
}
|
||||||
|
}
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
@ -59,10 +121,9 @@ public class KiwixMobileActivity extends Activity {
|
|||||||
|
|
||||||
// Get a reference to the AutoCompleteTextView in the layout
|
// Get a reference to the AutoCompleteTextView in the layout
|
||||||
AutoCompleteTextView articleSearchtextView = (AutoCompleteTextView) findViewById(R.id.articleSearchTextView);
|
AutoCompleteTextView articleSearchtextView = (AutoCompleteTextView) findViewById(R.id.articleSearchTextView);
|
||||||
ArrayList<String> emptyList = new ArrayList<String>();
|
|
||||||
// Create the adapter and set it to the AutoCompleteTextView
|
// Create the adapter and set it to the AutoCompleteTextView
|
||||||
adapter =
|
adapter = new AutoCompleteAdapter(this, android.R.layout.simple_list_item_1);
|
||||||
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, emptyList);
|
|
||||||
articleSearchtextView.setAdapter(adapter);
|
articleSearchtextView.setAdapter(adapter);
|
||||||
articleSearchtextView.setOnEditorActionListener(new OnEditorActionListener() {
|
articleSearchtextView.setOnEditorActionListener(new OnEditorActionListener() {
|
||||||
|
|
||||||
@ -87,30 +148,6 @@ public class KiwixMobileActivity extends Activity {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}});
|
}});
|
||||||
articleSearchtextView.addTextChangedListener(new TextWatcher()
|
|
||||||
{
|
|
||||||
public void afterTextChanged(Editable s)
|
|
||||||
{
|
|
||||||
// Abstract Method of TextWatcher Interface.
|
|
||||||
}
|
|
||||||
public void beforeTextChanged(CharSequence s,
|
|
||||||
int start, int count, int after)
|
|
||||||
{
|
|
||||||
// Abstract Method of TextWatcher Interface.
|
|
||||||
}
|
|
||||||
public void onTextChanged(CharSequence s,
|
|
||||||
int start, int before, int count)
|
|
||||||
{
|
|
||||||
AutoCompleteTextView articleSearchtextView = (AutoCompleteTextView) findViewById(R.id.articleSearchTextView);
|
|
||||||
Log.d("zimgap", "Adapter:"+adapter.getCount());
|
|
||||||
adapter.clear();
|
|
||||||
ZimContentProvider.searchSuggestions(s.toString(), 20);
|
|
||||||
String suggestion;
|
|
||||||
while ((suggestion = ZimContentProvider.getNextSuggestion())!=null) {
|
|
||||||
adapter.add(suggestion);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
// js includes will not happen unless we enable JS
|
// js includes will not happen unless we enable JS
|
||||||
|
Loading…
x
Reference in New Issue
Block a user