私は解決策を得ています。私は以下のようにコードを作成し、私の仕事をします。私はちょうど私たちのアクティビティonCreate()メソッドでオートコンプリートをセットアップしました。
私の場合、私は@
文字を使って検索すべきテキストの始まりと、ルックアップトークンの終わりを決定するためのスペースを識別しています。
public class MultiAutoCompleteTextViewDemo extends AppCompatActivity
{
MultiAutoCompleteTextView inputEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
inputEditText = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1);
inputEditText.setTokenizer(new UsernameTokenizer());
inputEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int cursor = start;
if (cursor >= s.length()) cursor = s.length()-1;
if (isValidToken(s, cursor)){
String token = getToken(s, start);
new LinkedinSkillAsyncTask((Activity) getApplicationContext()).execute(token);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
private boolean isValidToken(CharSequence text, int cursor){
for (int i=cursor; i>=0; i--){
if (text.charAt(i) == '@') return true;
if (text.charAt(i) == ' ') return false;
}
return false;
}
private String getToken(CharSequence text, int cursor){
int start=findTokenStart(text, cursor);
int end=findTokenEnd(text, cursor);
return text.subSequence(start, end).toString();
}
private int findTokenStart(CharSequence text, int cursor) {
int i = cursor;
while (i > 0 && text.charAt(i - 1) != '@') { i--; }
return i;
}
private int findTokenEnd(CharSequence text, int cursor) {
int i = cursor;
int len = text.length();
while (i < len && text.charAt(i) != ' ' && text.charAt(i) != ',' && text.charAt(i) != '.' ) {
i++;
}
return i;
}
public class UsernameTokenizer implements MultiAutoCompleteTextView.Tokenizer {
@Override public CharSequence terminateToken(CharSequence text) {
int i = text.length();
while (i > 0 && text.charAt(i - 1) == ' ') { i--; }
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 + " ";
}
}
@Override public int findTokenStart(CharSequence text, int cursor) {
int i = cursor;
while (i > 0 && text.charAt(i - 1) != '@') {
i--;
}
if (i < 1 || text.charAt(i - 1) != '@') {
return cursor;
}
return i;
}
@Override 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 class LinkedinSkillAsyncTask extends AsyncTask<String, String, String> {
private Activity context;
public String data;
public List<String> suggest;
public ArrayAdapter<String> aAdapter;
public LinkedinSkillAsyncTask(Activity cntxt) {
context = cntxt;
}
@Override protected String doInBackground(String... key) {
String newText = key[0];
newText = newText.trim();
newText = newText.replace(" ", "+");
try {
HttpClient hClient = new DefaultHttpClient();
HttpGet hGet = new HttpGet("http://www.linkedin.com/ta/skill?query="+newText);
ResponseHandler<String> rHandler = new BasicResponseHandler();
data = hClient.execute(hGet, rHandler);
suggest = new ArrayList<String>();
JSONObject jobj = new JSONObject(data);
JSONArray jArray = jobj.getJSONArray("resultList");
for (int i = 0; i < jArray.length(); i++) {
String SuggestKey = jArray.getJSONObject(i).getString("displayName");
suggest.add(SuggestKey);
}
} catch (Exception e) {
Log.w("Error", e.getMessage());
}
context.runOnUiThread(new Runnable() {
public void run() {
MultiAutoCompleteTextView inputEditText = (MultiAutoCompleteTextView) context.findViewById(R.id.multiAutoCompleteTextView1);
aAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_dropdown_item_1line, suggest);
inputEditText.setAdapter(aAdapter);
aAdapter.notifyDataSetChanged();
}
});
return null;
}
}
}
詳しくはread this tutorialです。たとえば、GitHubデモプロジェクトを確認してください。