Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add autocomplete functionality #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package me.gujun.android.taggroup.demo;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Filter;

import me.gujun.android.taggroup.TagGroup;
import me.gujun.android.taggroup.demo.db.TagsManager;
Expand All @@ -22,6 +25,7 @@ protected void onCreate(Bundle savedInstanceState) {
String[] tags = mTagsManager.getTags();

mTagGroup = (TagGroup) findViewById(R.id.tag_group);
mTagGroup.setAutoCompleteAdapter(new TagAutoCompleteAdapter(this));
mTagGroup.setTags(tags);
}

Expand Down Expand Up @@ -49,4 +53,39 @@ public void onBackPressed() {
mTagsManager.updateTags(mTagGroup.getTags());
super.onBackPressed();
}

private class TagAutoCompleteAdapter extends ArrayAdapter<String> {
public TagAutoCompleteAdapter(Context context) {
super(context, R.layout.autocomplete_row);
}

@Override
public Filter getFilter() {
return new Filter() {
@Override
protected Filter.FilterResults performFiltering(CharSequence constraint) {
Filter.FilterResults result = new Filter.FilterResults();
result.count = 0;
if(constraint!=null) {
String[] data = mTagsManager.getAutoCompleteData(TagEditorActivity.this, constraint.toString());
result.values = data;
result.count = data.length;
}
return result;
}

@Override
protected void publishResults(CharSequence constraint, Filter.FilterResults results) {
clear();
if (results.count > 0) {
for (String s : (String[]) results.values) {
add(s);
}
}
notifyDataSetChanged();
}
};
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import me.gujun.android.taggroup.demo.R;

/**
* Manage the tags from SQLite database.
Expand Down Expand Up @@ -65,4 +71,17 @@ public void clearTags() {
db.delete(TagsTable.TABLE_NAME, null, null);
db.close();
}
public String[] getAutoCompleteData(Context context,String _query) {
//Return countries
String[] data = context.getResources().getStringArray(R.array.countries_array);
String query = _query.toLowerCase();
ArrayList<String> filtredData = new ArrayList<>();

for (String d : data) {
if(d.toLowerCase().startsWith(query))
filtredData.add(d);
}

return filtredData.toArray(new String[filtredData.size()]);
}
}
12 changes: 12 additions & 0 deletions demo/src/main/res/layout/autocomplete_row.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="@android:style/TextAppearance.Small"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package me.gujun.android.taggroup;

import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.widget.MultiAutoCompleteTextView;

/**
* http://stackoverflow.com/questions/3482981/how-to-replace-the-comma-with-a-space-when-i-use-the-multiautocompletetextview
*/
public class SpaceTokenizer implements MultiAutoCompleteTextView.Tokenizer {

public int findTokenStart(CharSequence text, int cursor) {
int i = cursor;

while (i > 0 && text.charAt(i - 1) != ' ') {
i--;
}
while (i < cursor && text.charAt(i) == ' ') {
i++;
}

return i;
}

public int findTokenEnd(CharSequence text, int cursor) {
int i = cursor;
int len = text.length();

while (i < len) {
if (text.charAt(i) == ' ') {
return i;
} else {
i++;
}
}

return len;
}

public CharSequence terminateToken(CharSequence text) {
int i = text.length();

while (i > 0 && text.charAt(i - 1) == ' ') {
i--;
}

if (i > 0 && text.charAt(i - 1) == ' ') {
return text;
} else {
if (text instanceof Spanned) {
SpannableString sp = new SpannableString(text + " ");
TextUtils.copySpansFrom((Spanned) text, 0, text.length(),
Object.class, sp, 0);
return sp;
} else {
return text + " ";
}
}
}
}
Loading