2
我想在android中實現動態自動完成小部件。我完成了主要功能,並且實現的自動完成功能是針對YouTube視頻搜索。Android動態自動完成 - 小修正 - 附加代碼
當我開始輸入一個或兩個字母時,自動完成功能不起作用。但是當我輸入三個或更多的字母時,它可以正常工作。當我鍵入兩個字母並打回退格時,它也起作用。我不知道代碼有什麼問題。
我已經上傳的代碼here
專家,請指導我。如果你能指出我的代碼出了什麼地方,我將不勝感激。
在這方面的任何幫助,我們都非常感謝。
展望未來, 問候, 羅尼
import org.json.JSONArray;
import org.json.JSONException;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
public class YoutubeAutoComplete extends Activity {
Youtube yt = new Youtube();
CustomAutoComplete myAutoComplete;
ArrayAdapter<String> adapter;
private JSONArray js;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myAutoComplete = (CustomAutoComplete) findViewById(R.id.autocomplete);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line);
myAutoComplete.addTextChangedListener(textWatcher);
myAutoComplete.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
TextWatcher textWatcher = new TextWatcher() {
public void onTextChanged(final CharSequence s, int start, int before,
int count) {
Thread t = new Thread() {
public void run() {
try {
js = yt.GetSuggestions(s.toString()).getJSONArray(1);
messageHandler.sendEmptyMessage(0);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
t.start();
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
}
};
private Handler messageHandler = new Handler() {
public void handleMessage(Message msg) {
adapter.clear();
for (int i = 0; i < js.length(); i++) {
try {
adapter.add(js.getJSONArray(i).getString(0));
System.out.println(js.getJSONArray(i).getString(0));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
adapter.notifyDataSetChanged();
}
};
}
如果直接在問題中提供的代碼中的相關片段更有效。人們不想在orde中下載任意文件來幫助你。 – 2010-10-21 16:47:30
嗨,我編輯並添加了源代碼。 – user264953 2010-10-21 18:22:22
你好,現在所需的源代碼在這裏。有人可以看看它,並找出什麼給了我上面提到的問題。 – user264953 2010-10-22 05:16:30