Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java AutoCompleteTextView不响应对其ArrayAdapter的更改_Java_Android_Filtering_Android Arrayadapter_Autocompletetextview - Fatal编程技术网

Java AutoCompleteTextView不响应对其ArrayAdapter的更改

Java AutoCompleteTextView不响应对其ArrayAdapter的更改,java,android,filtering,android-arrayadapter,autocompletetextview,Java,Android,Filtering,Android Arrayadapter,Autocompletetextview,ArrayList似乎填充得很好,但无论我使用什么方法,我似乎都无法让适配器填充数据。我已尝试添加到ArrayList,也添加到ArrayAdapter。无论哪种方式,我都无法在AutoCompleteTextView级别或ArrayAdapter本身获得响应(当然AutoCompleteTextView没有做任何事情)。有人能看出来是怎么回事吗 public class MainActivity extends Activity implements TextWatcher { // priv

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();
}
public类MainActivity扩展活动实现TextWatcher{
//私有自动完成视图自动完成;
公共字符串标记=新字符串(“MAINACTIVITY”);
公共阵列适配器自动完成适配器;
公共自动完成文本视图自动完成;
公共输入流输入流;
公开名单数据;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
数据=新的ArrayList();
autoCompleteAdapter=new ArrayAdapter(这是android.R.layout.simple\u下拉列表\u项目\u 1行,数据);
setNotifyOnChange(true);
autoComplete=(AutoCompleteTextView)findviewbyd(R.id.acsayt);
自动完成.setHint(R.string.search\u hint);
自动完成。设置阈值(2);
autoComplete.addTextChangedListener(此);
设置适配器(自动完成适配器);
}
//支持TextWatcher接口方法
公共无效后文本已更改(可编辑){
}
更改前文本之前的公共void(字符序列s、int start、int count、int after){
}
public void onTextChanged(字符序列、int start、int before、int count){
Log.d(标记“我检测到文本更改”+s.toString());
data.clear();
queryWebService();
}
私有void queryWebService(){
新线程(newrunnable()){
公开募捐{
Log.d(标记“派生线程”);
//这里的代码用于设置http连接、查询Web服务。。。
//解析JSON响应并将项目添加到适配器
试一试{
JSONArray jArray=新的JSONArray(resultString);
int length=jArray.length();
int countedvalue,capturedvalue;
Log.d(标签,“响应有”+长度+”项);
int i=0;
while(i
}

LogCat显示data.size()中的预期值数,但autoCompleteAdapter.getCount()中的值数为零

ArrayList的填充似乎很好,但不管怎样 我使用的方法似乎无法让适配器填充数据

这与
autocomplettextview
的工作原理背道而驰。当用户开始在输入框中输入字符时,
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();
}
public void onTextChanged(字符序列、int start、int before、int count){
if(autoComplete.getThreashold()
您需要更新主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);
    }
});
//执行queryWebService方法中的http请求,以及何时更新数据:
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
autocompletedapter.clear();
//添加数据
for(int i=0;i

查看上述代码是否有效。

尝试缩小问题范围。你已经发布了无止境的未格式化代码。我删去了一堆东西来关注问题区域。谢谢你的回复。这是一个很大的帮助。我按照您的建议将autocompletedapter.add()调用移动到UI线程上,创建了一个CustomAutoCompleteView,只做了一些小的修改,现在有了第一个可用的UI。感谢您在这里的支持-它带来了不同:)