1

昨天我做了一个关于下一个代码的问题。我现在的问题是 ontextchanged 方法中的自动完成。例如,当我写东西时,我想搜索“David Guetta”,如果我写“David”,应用程序不会搜索任何内容,直到您写更多内容或删除某些内容,例如“Davi”(删除最后一个 d ),或者在最后写一些新的东西,比如“David”或“David G”。

这是代码。

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;
    }

}

这就是结果。第一张图片,我进行了搜索,没有任何反应。第二张图片,我删除了一个字符并且搜索有效。:S

首次搜索

第二次搜索

4

0 回答 0