【问题标题】:AutoCompleteTextView not responding to changes to its ArrayAdapterAutoCompleteTextView 不响应对其 ArrayAdapter 的更改
【发布时间】:2013-02-28 19:06:32
【问题描述】:

ArrayList 似乎填充得很好,但无论我使用什么方法,我似乎都无法让适配器填充数据。我尝试添加到ArrayList,也添加到ArrayAdapter。无论哪种方式,我都无法在AutoCompleteTextView 级别或ArrayAdapter 本身获得响应(当然AutoCompleteTextView 什么也不做)。有人能看出什么问题吗?

public class MainActivity extends Activity implements TextWatcher {
// private AutoCompleteView autoComplete; 
public String TAG = new String("MAINACTIVITY");
public ArrayAdapter<String> autoCompleteAdapter;
public AutoCompleteTextView autoComplete;
public InputStream inputStream;
public List<String> data;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    data = new ArrayList<String>();
    autoCompleteAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, data);
    autoCompleteAdapter.setNotifyOnChange(true);
    autoComplete = (AutoCompleteTextView) findViewById(R.id.acsayt);
    autoComplete.setHint(R.string.search_hint);
    autoComplete.setThreshold(2);
    autoComplete.addTextChangedListener(this);
    autoComplete.setAdapter(autoCompleteAdapter);
}

// uphold TextWatcher interface methods
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

public void onTextChanged(CharSequence s, int start, int before, int count) {
    Log.d(TAG, "I detected a text change " + s.toString());
    data.clear();
    queryWebService();
}

private void queryWebService() {
    new Thread(new Runnable() {
        public void run() {
            Log.d(TAG, "spawned thread");

            //  Code in here to set up http connection, query webservice ...

            // parse the JSON response & add items to adapter
            try {
                JSONArray jArray = new JSONArray(resultString);
                int length = jArray.length();
                int countedValues, capturedValues;
                Log.d(TAG, "response had " + length + " items");
                int i = 0;
                while (i < length) {
                    JSONObject internalObject = jArray.getJSONObject(i);
                    String vehicleName = internalObject.getString("name").toString();
                    Log.d(TAG, "vehicle name is " + vehicleName);
                    try {
                        data.add(vehicleName);  
                        autoCompleteAdapter.add(vehicleName);   // not working
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    countedValues = data.size();    // correctly reports 20 values
                    capturedValues = autoCompleteAdapter.getCount();    //  is zero
                    Log.d(TAG, "array list holds " + countedValues + " values");
                    Log.d(TAG, "array adapter holds " + capturedValues + " values");
                    i++;
                }
            } catch (Exception e) {
                Log.d(TAG, "JSON manipulation err: " + e.toString());
            }
        }
    }).start();
}

}

LogCat 显示来自 data.size() 的值的预期数量,但来自 autoCompleteAdapter.getCount() 的值为零。

【问题讨论】:

  • 尝试缩小您的问题范围。您发布了源源不断的无格式代码。
  • 我去掉了一堆东西,专注于问题领域。

标签: java android filtering android-arrayadapter autocompletetextview


【解决方案1】:

ArrayList 似乎填充得很好,但无论如何 我使用的方法,我似乎无法让适配器填充数据

你违背了AutoCompleTextView 的工作方式。当用户开始在输入框中输入字符时,AutoCompleteTextView 将过滤适配器,当这种情况发生时,它将显示下拉列表,其中包含设法通过过滤请求的值。现在您已经设置 AutoCompleteTextView 以在用户每次输入字符时创建一个线程,问题是 AutoCompleteTextview 永远不会看到这些值,因为它请求适配器在适配器实际填充正确之前进行过滤价值观。试试这样的:

public static class BlockingAutoCompleteTextView extends
        AutoCompleteTextView {

    public BlockingAutoCompleteTextView(Context context) {
        super(context);
    }

    @Override
    protected void performFiltering(CharSequence text, int keyCode) {           
        // nothing, block the default auto complete behavior
    }

}

并获取数据:

public void onTextChanged(CharSequence s, int start, int before, int count) {
    if (autoComplete.getThreashold() < s.length()) {
        return;
    } 
    queryWebService();
}

您需要在主 UI 线程上更新数据并使用适配器的方法:

// do the http requests you have in the queryWebService method and when it's time to update the data:
runOnUiThread(new Runnable() {
        @Override
    public void run() {
        autoCompleteAdapter.clear();
        // add the data
                for (int i = 0; i < length; i++) {
                     // do json stuff and add the data
                     autoCompleteAdapter.add(theNewItem);                    
                } 
                // trigger a filter on the AutoCompleteTextView to show the popup with the results 
                autoCompleteAdapter.getFilter().filter(s, autoComplete);
    }
});

看看上面的代码是否有效。

【讨论】:

  • 感谢您的回复。这是一个很大的帮助。我按照您的建议将 autoCompleteAdapter.add() 调用移到 UI 线程上,并创建了一个带有一些小的修改的 CustomAutoCompleteView,现在有了第一个工作 UI。感谢您在这里的支持 - 它有所作为:)
猜你喜欢
  • 1970-01-01
  • 2015-03-10
  • 1970-01-01
  • 2016-05-08
  • 2011-06-08
  • 2012-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多