質問

So I had it working... and then it broke, and I don't know why. I've been pulling my hair out trying to figure out what's gone wrong.

Here is the structure of the code:

locale = (AutoCompleteTextView) findViewById(locationText);
locale.setThreshold(3);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, locationSuggestions);
adapter.setNotifyOnChange(true);
locale.setAdapter(adapter);

locale.addTextChangedListener(new TextWatcher(){

    @Override
    public void afterTextChanged(Editable s) {

    }
        @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {    
    }
        @Override
    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        if(!s.toString().contains(","))
        {
            String loc = s.toString().replace(" ", "%20");
            new JsonTask(loc).execute();
                adapter.notifyDataSetChanged();
            locale.setAdapter(adapter);     
        }   
    }       
});

I'm just at a total loss as to what to do. I started off from this tutorial here:

http://www.gaanza.com/blog/android-autocompletetextview-mysql/

I tried doing this with his custom AutoCompleteTextView class, but something to do with the way he was filtering results just didn't work.

Here's the thing: my JsonTask appropriately updates my String[] locationSuggestions. The data is there. The textbox is filled with an initial location pulled from a geocoder earlier in the code. When you erase the suggestion, nothing pops up until you go under the threshold and then type up to 3 characters. On the third character, the drop down appears for a split second and then disappears. If you continue to type it does not show up again. If you erase the string and then enter a new set of characters, the drop down will pop up briefly again and then disappear.

I honestly can't remember what I changed that broke it. It was working fine, and then I went to edit some of the layout and it stopped working. I changed the layout back and even still, nothing. This is what my xml looks like:

<AutoCompleteTextView
        android:id="@+id/locationText"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_column="0"
        android:layout_columnSpan="3"
        android:layout_gravity="fill_horizontal|center_vertical"
        android:layout_margin="10dp"
        android:layout_marginBottom="40dp"
        android:layout_row="2"
        android:drawableLeft="@drawable/locationicon"
        android:ems="10"
        android:imeActionLabel="actionGo"
        android:singleLine="true" >

    </AutoCompleteTextView>

And for completeness' sake, here is my AsyncTask:

public class JsonTask extends AsyncTask{

private String query;
@Override
protected Void doInBackground(Object... arg0) {

    JSONParser jsonParser = new JSONParser();

    JSONArray array;
    try {
        array=jsonParser.getJSONFromUrl(url + query);

        JSONObject citystate = new JSONObject();

        String city;
        String state;

        for(int i = 0; i<5; i++)
        {
            citystate = array.getJSONObject(i);
            state = citystate.getString("State");
            city = citystate.getString("City");
            locationSuggestions[i] = city + ", " + state;
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();}

    return null;
}   
public JsonTask(String text)
{
    query = text;
}
}
役に立ちましたか?

解決

In JsonTask.onPostExecute I added a small for loop:

for(int i = 0; i < 5; i++)
{
adapter.insert(locationSuggestions[i], i);
}

While the arraylist was in fact updating with the proper data, I wasn't updating the adapter with the arraylist contents, so the data in the adapter was all null, hence no drop down data to display.

Duh!

他のヒント

Remove

adapter.notifyDataSetChanged();
locale.setAdapter(adapter);   

from onTextChanged().

Now override onPostExecute() in JsonTask and put adapter.notifyDataSetChanged() in that method.

Don't call setAdapter() again.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top