GVKun编程网logo

AutoCompleteTextView不响应对其ArrayAdapter的更改(autocomplete不起作用)

20

如果您对AutoCompleteTextView不响应对其ArrayAdapter的更改感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于AutoCompleteTextVie

如果您对AutoCompleteTextView不响应对其ArrayAdapter的更改感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于AutoCompleteTextView不响应对其ArrayAdapter的更改的详细内容,我们还将为您解答autocomplete不起作用的相关问题,并且为您提供关于ABCAutoCompleteTextView UITextView 的子类、Android - AutoCompleteTextView、Android AutoCompleteTextView、android AutoCompleteTextView .addTextChangedListener问题的有价值信息。

本文目录一览:

AutoCompleteTextView不响应对其ArrayAdapter的更改(autocomplete不起作用)

AutoCompleteTextView不响应对其ArrayAdapter的更改(autocomplete不起作用)

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. */@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()中显示为零。

答案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);    }});

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

ABCAutoCompleteTextView UITextView 的子类

ABCAutoCompleteTextView UITextView 的子类

ABCAutoCompleteTextView 介绍

ABCAutoCompleteTextView 是一个 UITextView 的子类,自动补全标签和用户名。

ABCAutoCompleteTextView 官网

https://github.com/AdamBCo/ABCAutoCompleteTextView

Android - AutoCompleteTextView

Android - AutoCompleteTextView

XML代码:

<?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/linearLayout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center_horizontal"
            android:orientation="vertical" >

            <AutoCompleteTextView
                android:id="@+id/autoCompleteTextView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                 >

                <requestFocus />
            </AutoCompleteTextView>

        </LinearLayout>

Java代码:

package com.demo;

import android.app.Activity;
import android.os.Bundle;

import android.view.View;
import android.widget.AdapterView;
import android.widget.AutoCompleteTextView;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.widget.TextView;

public class ActivityBasicActivity extends Activity {
    /** 当第一次被创建时调用 */
	String[] str = {
			"rollen", "rollenholt", "rollenren", "roll","Google","百度","腾讯","腾讯游戏"
	};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);//创建一个线性布局管理器
        //this.getWindow().setBackgroundDrawableResource(R.color.mycolor);//设置背景颜色
        setContentView(R.layout.main);  //显示该视图
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,str);
        AutoCompleteTextView textview = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
        textview.setThreshold(1);//设置用户输入的字符数才显示下拉菜单
        textview.setAdapter(adapter);
    }
   
}


Android AutoCompleteTextView

Android AutoCompleteTextView

AutoCompleteTextView是一个可编辑的文本视图显示自动完成建议当用户键入。建议列表显示在一个下拉菜单,用户可以从中选择一项,以完成输入。建议列表是从一个数据适配器获取的数据。它有三个重要的方法clearListSelection():清除选中的列表项、dismissDropDown():如果存在关闭下拉菜单、getAdapter():获取适配器

completionThreshold:它的值决定了你在AutoCompleteTextView至少输入几个字符,它才会具有自动提示的功能。另,默认最多提示20条。

 dropDownAnchor:它的值是一个ViewID,指定后,AutoCompleteTextView会在这个View下弹出自动提示。

 dropDownSelector:应该是设置自动提示的背景色之类的,没有尝试过,有待进一步考证。

 dropDownWidth:设置自动提示列表的宽度。

clearListSelection();//清除选中的列表项
dismissDropDown();//如果存在关闭下拉菜单
getAdapter();//获取适配器


案例代码

1.布局文件autocompletetextview.xml如下

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <AutoCompleteTextView
         android:id="@+id/antoCom_btn_id"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:completionHint="输入补全提示标题"
         android:completionThreshold="1"
        />
    <TextView
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="@string/autoText_text"
        />
    </LinearLayout>

 2、java文件AutoCompleteTextViewDemo.java

package com.dream.app.start.autocompletetextview;
import com.dream.app.start.R;

public class AutoCompleteTextViewDemo extends PublicClass {
	String  [] str = {"abc","abcd","abd","asd","asw","wse","wsq"};
	//定义数组

//    String[] province = getResources().getStringArray(R.array.province);

	private AutoCompleteTextView myauto = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.autocomplete);
		
		//定义数组
//	    String[] province = getResources().getStringArray(R.array.province);
		
		 //定义数组适配器
         ArrayAdapter<String>   autoStr = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,str);
		
         //找到自动完成组件
         myauto = (AutoCompleteTextView)findViewById(R.id.antoCom_btn_id);
		
         //为其设置适配器
         myauto.setAdapter(autoStr);
	}

}

3.执行效果:

 

 

 

 

android AutoCompleteTextView .addTextChangedListener问题

android AutoCompleteTextView .addTextChangedListener问题

下面的问题怎么解决;谢谢

private AutoCompleteTextView userNameView;

userNameView.addTextChangedListener(new TextWatcher() {

            
            @Override  //这段是起什么作用;里面从参数怎么理解呀;谢谢
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
                
            }
            
            @Override          //这段是起什么作用;里面从参数怎么理解呀;谢谢
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // TODO Auto-generated method stub
                
            }
            
            @Override     //这段是起什么作用;里面从参数怎么理解呀;谢谢
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                
            }
        })

今天关于AutoCompleteTextView不响应对其ArrayAdapter的更改autocomplete不起作用的介绍到此结束,谢谢您的阅读,有关ABCAutoCompleteTextView UITextView 的子类、Android - AutoCompleteTextView、Android AutoCompleteTextView、android AutoCompleteTextView .addTextChangedListener问题等更多相关知识的信息可以在本站进行查询。

本文标签: