Note that there are some explanatory texts on larger screens.

plurals
  1. POAutocomplete doesn't filled correctly
    primarykey
    data
    text
    <p>yesterday I do a question about the next code. My problem now, is with the autocomplete in the method ontextchanged. When I write something, for example, I want to search "David Guetta", if I write "David", the app doesn't search anything until y write something more or delete something, example, "Davi" (deleting the last d), or writing something new in the last like "David " or "David G".</p> <p>This is the code.</p> <pre><code>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&lt;String&gt; adapter; List&lt;String&gt; songs; List&lt;String&gt; lArtist; List&lt;String&gt; lTrack; ArrayAdapter&lt;String&gt; 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&lt;String&gt;(this, android.R.layout.simple_dropdown_item_1line); // autoCompleteSongs.setThreshold(1); // autoCompleteSongs.addTextChangedListener(this); // autoCompleteSongs.setAdapter(new // ArrayAdapter&lt;String&gt;(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() &lt;= 3) { runOnUiThread(new Runnable() { public void run() { TextView text = (TextView) findViewById(R.id.petitionTextView); text.setText(""); } }); } if (s.length() &gt; 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 &gt; limit) ? limit : num_results; for(int i = 0; i &lt; 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 &lt; artist.length(); j++){ JSONObject singleartist = (JSONObject) artist.getJSONObject(j); allartists += singleartist.getString("name"); if(j &lt; artist.length() - 1){ allartists += ", "; } } String reg = "&lt;font color=#64c7eb&gt;" + title + "&lt;/font&gt; &lt;font color=#272527&gt;" + allartists + "&lt;/font&gt;&lt;/br&gt;"; 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 &lt; 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; } } </code></pre> <p>This is the result. The first image, I do a search and nothing happends. The second image, I delete one caracter and the search works. :S</p> <p><img src="https://i.stack.imgur.com/LkI0o.png" alt="First Search"></p> <p><img src="https://i.stack.imgur.com/p7fvx.png" alt="Second Search"></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload