AutoCompleteTextView不响应变更其ArrayAdapter [英] AutoCompleteTextView not responding to changes to its ArrayAdapter

查看:265
本文介绍了AutoCompleteTextView不响应变更其ArrayAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的ArrayList 似乎填充就好了,但是不管我用什么办法,我似乎无法去填充数据适配器。我曾尝试添加到的ArrayList ,也给 ArrayAdapter 。无论哪种方式,我无法得到的响应的 AutoCompleteTextView 的水平,还是在 ArrayAdapter 本身当然的了(和 AutoCompleteTextView 是什么都不做)。任何人都可以看看有什么错?

 公共类MainActivity扩展活动实现TextWatcher {
//私人AutoCompleteView自动完成;
公共字符串变量=新的String(MAINACTIVITY);
公共ArrayAdapter<字符串> autoCompleteAdapter;
公共AutoCompleteTextView自动完成;
公众的InputStream的InputStream;
公开名单<字符串>数据;

/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);

    数据=新的ArrayList<字符串>();
    autoCompleteAdapter =新的ArrayAdapter<字符串>(这一点,android.R.layout.simple_dropdown_item_1line,数据);
    autoCompleteAdapter.setNotifyOnChange(真正的);
    自动完成=(AutoCompleteTextView)findViewById(R.id.acsayt);
    autoComplete.setHint(R.string.search_hint);
    autoComplete.setThreshold(2);
    autoComplete.addTextChangedListener(本);
    autoComplete.setAdapter(autoCompleteAdapter);
}

//秉承TextWatcher接口方法
公共无效afterTextChanged(编辑S){
}
公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,之后INT){
}

公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
    Log.d(TAG,我发现一个文本改变+ s.toString());
    data.clear();
    queryWebService();
}

私人无效queryWebService(){
    新主题(新的Runnable(){
        公共无效的run(){
            Log.d(TAG,衍生的线程);

            // code在这里建立HTTP连接,查询,web服务...

            //解析JSON响应和放大器;添加项目到适配器
            尝试 {
                JSONArray jArray =新JSONArray(resultString);
                INT长度= jArray.length();
                INT countedValues​​,capturedValues​​;
                Log.d(TAG,回应了+长度+项目);
                INT I = 0;
                而(I<长度){
                    的JSONObject internalObject = jArray.getJSONObject(ⅰ);
                    。字符串vehicleName = internalObject.getString(姓名)的toString();
                    Log.d(TAG,车名是+ vehicleName);
                    尝试 {
                        data.add(vehicleName);
                        autoCompleteAdapter.add(vehicleName); //不工作
                    }赶上(例外五){
                        e.printStackTrace();
                    }
                    countedValues​​ = data.size(); //正确报告20值
                    capturedValues​​ = autoCompleteAdapter.getCount(); //为零
                    Log.d(TAG,数组列表包含+ countedValues​​ +值);
                    Log.d(TAG,阵列适配器举办的+ capturedValues​​ +值);
                    我++;
                }
            }赶上(例外五){
                Log.d(TAG,JSON操作错误:+ e.toString());
            }
        }
    })。开始();
}
 

}

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

解决方案
  

在ArrayList中似乎被填充得很好,但不管是什么   方法我用,我似乎无法获取适配器来填充数据

您打算对如何使用 AutoCompleTextView 的作品。当用户开始进入输入的字符框的 AutoCompleteTextView 将过滤适配器,这时它会显示下拉与设法通过过滤器请求的值。现在你已经设置了 AutoCompleteTextView 每个用户输入一个字符的时间,使一个线程,问题是, AutoCompleteTextview 将再也看不到这些值,因为它要求适配器居然被填充了正确的价值观适配器之前进行过滤。尝试是这样的:

 公共静态类BlockingAutoCompleteTextView扩展
        AutoCompleteTextView {

    公共BlockingAutoCompleteTextView(上下文的背景下){
        超(上下文);
    }

    @覆盖
    保护无效performFiltering(CharSequence的文字,诠释键code){
        //没什么,阻止默认的自动完成行为
    }

}
 

和获取数据:

 公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
    如果(autoComplete.getThreashold()&其中; s.length()){
        返回;
    }
    queryWebService();
}
 

您需要使用转接器的方法来更新数据的主UI线程,也:

  //你有在queryWebService方法和HTTP请求时,它的时间来更新数据:
runOnUiThread(新的Runnable(){
        @覆盖
    公共无效的run(){
        autoCompleteAdapter.clear();
        //添加数据
                的for(int i = 0; I<长度;我++){
                     //做JSON的东西,并添加数据
                     autoCompleteAdapter.add(theNewItem);
                }
                //触发过滤器上的AutoCompleteTextView显示的结果弹出
                。autoCompleteAdapter.getFilter()过滤器(S,自动完成);
    }
});
 

请参阅如果code以上的作品。

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. I have tried adding to the ArrayList, also to the ArrayAdapter. Either way I am unable to get a response at the AutoCompleteTextView level, or in the ArrayAdapter itself(and of course the AutoCompleteTextView is doing nothing). Can anybody see what's wrong?

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 shows the expected number of values from data.size(), but zero from 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

You're going against how the AutoCompleTextView works. When the user starts entering characters in the input box the AutoCompleteTextView will filter the adapter and when that happens it will show the drop down with the values which managed to pass the filter request. Now you've setup the AutoCompleteTextView to make a thread each time the user enters a character, the problem is that the AutoCompleteTextview will never see those values as it requests the adapter to filter before the adapter actually gets populated with the right values. try something like this:

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
    }

}

and to get the data:

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

You need to update the data on the main UI thread and also using the adapter's methods:

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

See if the code above works.

这篇关于AutoCompleteTextView不响应变更其ArrayAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆