java–AutoCompleteTextView没有响应其ArrayAdapter的更改

java–AutoCompleteTextView没有响应其ArrayAdapter的更改,第1张

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

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

@H_502_4@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. */@OverrIDepublic 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 methodspublic 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()为零.

解决方法:

The ArrayList appears to be populating just fine, but no matter what
approach I use, I can’t seem to get the adapter to populate with data

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

@H_502_4@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 }}

并获取数据:

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

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

@H_502_4@// 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); }});

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

总结

以上是内存溢出为你收集整理的java – AutoCompleteTextView没有响应其ArrayAdapter的更改全部内容,希望文章能够帮你解决java – AutoCompleteTextView没有响应其ArrayAdapter的更改所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1097222.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存