Android 自动完成不';不能正确填写
昨天我问了一个关于下一个代码的问题。我现在的问题是方法ontextchanged中的autocomplete。例如,当我写东西时,我想搜索“David Guetta”,如果我写“David”,应用程序不会搜索任何东西,直到我写更多的东西或删除一些东西,例如,“Davi”(删除最后一个d),或在最后一个d中写一些新的东西,如“David”或“David G” 这是代码Android 自动完成不';不能正确填写,android,autocomplete,textwatcher,Android,Autocomplete,Textwatcher,昨天我问了一个关于下一个代码的问题。我现在的问题是方法ontextchanged中的autocomplete。例如,当我写东西时,我想搜索“David Guetta”,如果我写“David”,应用程序不会搜索任何东西,直到我写更多的东西或删除一些东西,例如,“Davi”(删除最后一个d),或在最后一个d中写一些新的东西,如“David”或“David G” 这是代码 package victor.martin.loc4sq; import java.util.List; import java
package victor.martin.loc4sq;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
public class SearchMusic extends Activity {
AutoCompleteTextView autoCompleteSongs;
String searchTerms;
String[] arrayArtist = new String[64];
String[] arrayTrack = new String[64];
ArrayAdapter<String> adapter;
List<String> songs;
List<String> lArtist;
List<String> lTrack;
ArrayAdapter<String> list;
// Declare and initialize the timer
Timer t = new Timer();
boolean bothsearchs = false; // Controlamos que haya busqueda por artista y
// pista si uno no existe.
int listsize = 0; // iterator
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_music);
autoCompleteSongs = (AutoCompleteTextView) findViewById(R.id.autoCompletePetition);
list = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line);
// autoCompleteSongs.setThreshold(1);
// autoCompleteSongs.addTextChangedListener(this);
// autoCompleteSongs.setAdapter(new
// ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,
// item));
autoCompleteSongs.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (s.length() <= 3) {
runOnUiThread(new Runnable() {
public void run() {
TextView text = (TextView) findViewById(R.id.petitionTextView);
text.setText("");
}
});
}
if (s.length() > 3) {
searchTerms = s.toString();
searchTerms = searchTerms.replace(" ", "+");
// Cancel the Timer and all scheduled tasks
t.cancel();
t.purge();
t = new Timer();
// Set the schedule function and rate
t.schedule(new TimerTask() {
@Override
public void run() {
AsyncHttpClient client = new AsyncHttpClient();
// Buscamos por pista
client.get(
"http://ws.spotify.com/search/1/track.json?q="
+ searchTerms + "*", null,
new JsonHttpResponseHandler() {
public void onSuccess(JSONObject data) {
try {
JSONObject info = data.getJSONObject("info");
int num_results = info.getInt("num_results");
int limit = info.getInt("limit"); // This will be the size of the array
JSONArray tracks = data.getJSONArray("tracks");
// It is not needed, but is well to know the size of the list
listsize = ( num_results > limit) ? limit : num_results;
for(int i = 0; i < listsize; i++){
JSONObject singletrack = (JSONObject) tracks.getJSONObject(i);
String title = singletrack.getString("name");
JSONArray artist = singletrack.getJSONArray("artists");
String allartists = new String();
for(int j = 0; j < artist.length(); j++){
JSONObject singleartist = (JSONObject) artist.getJSONObject(j);
allartists += singleartist.getString("name");
if(j < artist.length() - 1){
allartists += ", ";
}
}
String reg = "<font color=#64c7eb>"
+ title
+ "</font> <font color=#272527>"
+ allartists
+ "</font></br>";
list.add(reg);
title = null;
allartists = null;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Throwable arg0) {
}
});
list.notifyDataSetChanged();
runOnUiThread(new Runnable() {
public void run() {
TextView text = (TextView) findViewById(R.id.petitionTextView);
String content = "";
for(int i = 0; i < listsize; i++){
content += list.getItem(i);
}
text.setText(Html.fromHtml(content));
}
});
}
}, 4000);
// Set how long before to start calling the TimerTask (in
// milliseconds)
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.search_music, menu);
return true;
}
}
包victor.martin.loc4sq;
导入java.util.List;
导入java.util.Timer;
导入java.util.TimerTask;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.app.Activity;
导入android.os.Bundle;
导入android.text.Editable;
导入android.text.Html;
导入android.text.TextWatcher;
导入android.view.Menu;
导入android.widget.ArrayAdapter;
导入android.widget.AutoCompleteTextView;
导入android.widget.TextView;
导入com.loopj.android.http.AsyncHttpClient;
导入com.loopj.android.http.JsonHttpResponseHandler;
公共课堂音乐拓展活动{
自动完成文本视图自动完成歌曲;
字符串搜索词;
String[]arrayArtist=新字符串[64];
String[]arrayTrack=新字符串[64];
阵列适配器;
列出歌曲;
名单保护者;
列表框;
阵列适配器列表;
//声明并初始化计时器
定时器t=新定时器();
布尔值bothsearchs=false;//Controlamos que haya busqueda por artista y
//这是不存在的。
int listsize=0;//迭代器
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u search\u music);
autoCompleteSongs=(AutoCompleteTextView)findViewById(R.id.AutoCompleteTepetion);
列表=新阵列适配器(此,
android.R.layout.simple\u下拉菜单\u项目\u 1line);
//自动完成歌曲。设置阈值(1);
//autoCompleteSongs.addTextChangedListener(此);
//autoCompleteSongs.setAdapter(新)
//ArrayAdapter(这是android.R.layout.simple下拉菜单项行,
//项目);;
autoCompleteSongs.addTextChangedListener(新的TextWatcher(){
public void onTextChanged(字符序列,int start,int before,
整数计数){
如果(s.长度()3){
searchTerms=s.toString();
searchTerms=searchTerms.replace(“,“+”);
//取消计时器和所有计划任务
t、 取消();
t、 清除();
t=新定时器();
//设置调度功能和速率
t、 附表(新TimerTask(){
@凌驾
公开募捐{
AsyncHttpClient=新的AsyncHttpClient();
//皮斯塔巴士酒店
client.get(
"http://ws.spotify.com/search/1/track.json?q="
+searchTerms+“*”,null,
新的JsonHttpResponseHandler(){
成功时的公共void(JSONObject数据){
试一试{
JSONObject info=data.getJSONObject(“info”);
int num_results=info.getInt(“num_results”);
int limit=info.getInt(“limit”);//这将是数组的大小
JSONArray tracks=data.getJSONArray(“tracks”);
//不需要它,但最好知道列表的大小
listsize=(num\u结果>限制)?限制:num\u结果;
for(int i=0;i”;
列表。添加(reg);
title=null;
allartists=null;
}
}捕获(JSONException e){
e、 printStackTrace();
}
}
@凌驾