android 自动完成文本视图不显示任何下拉项

tktrz96b  于 2022-12-25  发布在  Android
关注(0)|答案(7)|浏览(166)

我的XML:

<AutoCompleteTextView
        android:id="@+id/searchAutoCompleteTextView_feed"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:completionThreshold="2"
        android:hint="@string/search" />

我的java代码:

AutoCompleteTextView eT = (AutoCompleteTextView)findViewById(R.id.searchAutoCompleteTextView_feed);
eT.addTextChangedListener(this);
String[] sa = new String[]{"apple", "mango", "banana", "apple mango", "mango banana"};
ArrayAdapter<String> aAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, sa);
eT.setAdapter(aAdapter);

这根本不起作用...我的意思是它就像一个编辑文本视图一样工作。我错在哪里了?
完整代码:

public class FeedListViewActivity extends ListActivity implements TextWatcher{

    private AutoCompleteTextView eT;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.feed);

        eT = (AutoCompleteTextView) findViewById(R.id.searchAutoCompleteTextView_feed);
        eT.addTextChangedListener(this);

                    Thread thread = new Thread(null, loadMoreListItems);
                    thread.start();
    }

    private Runnable returnRes = new Runnable() {
        public void run() {

            //code for other purposes
        }
    };

    private Runnable loadMoreListItems = new Runnable() {
        public void run() {
            getProductNames();

            // Done! now continue on the UI thread
            runOnUiThread(returnRes);
        }
    };

    protected void getProductNames() {

            String[] sa = new String[]{"apple", "mango", "banana", "apple mango", "mango banana"};

            ArrayAdapter<String> aAdapter = new ArrayAdapter<String>(getApplicationContext(),
                    android.R.layout.simple_dropdown_item_1line, sa);
            eT.setAdapter(aAdapter);

    }

    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }

    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub

    }
}
n3h0vuf2

n3h0vuf21#

我只是看到你的另一个问题之前看到这一个。我是挣扎与自动完成了一段时间,我几乎恢复到您的新实现下载所有的关键字,直到我终于得到它的工作。我所做的是;

//In the onCreate
//The suggestArray is just a static array with a few keywords
this.suggestAdapter = new ArrayAdapter<String>(this, this.suggestionsView, suggestArray);
//The setNotifyOnChange informs all views attached to the adapter to update themselves 
//if the adapter is changed
this.suggestAdapter.setNotifyOnChange(true);

在我的文本观察器的onTextChanged方法中,我使用asynctask获得建议

//suggestsThread is an AsyncTask object
suggestsThread.cancel(true);
suggestsThread = new WertAgentThread();
suggestsThread.execute(s.toString());

然后在AsyncTask的onPostExecute中更新自动完成文本视图

//suggestions is the result of the http request with the suggestions
this.suggestAdapter = new ArrayAdapter<String>(this, R.layout.suggestions, suggestions);
this.suggestions.setAdapter(this.suggestAdapter);
//notifydatasetchanged forces the dropdown to be shown.
this.suggestAdapter.notifyDataSetChanged();

有关详细信息,请参见设置更改通知和通知数据集更改

d6kp6zgx

d6kp6zgx2#

这是我的项目的一个片段。2我认为当你从服务中获得数据后,你所要做的就是:
1.清除以前的数据。
1.清除以前的适配器值。
1.然后使用add()或addAll()方法向数据列表中添加值。
1.通过调用适配器上的notifyDataSetChanged()通知数据发生了变化。

@Override
public void onGetPatient(List<PatientSearchModel> patientSearchModelList) {

//here we got the raw data traverse it to get the filtered names data for the suggestions

stringArrayListPatients.clear();
stringArrayAdapterPatient.clear();
for (PatientSearchModel patientSearchModel:patientSearchModelList){

    if (patientSearchModel.getFullName()!=null){

        stringArrayListPatients.add(patientSearchModel.getFullName());

    }

}

//update the array adapter for patient search
stringArrayAdapterPatient.addAll(stringArrayListPatients);
stringArrayAdapterPatient.notifyDataSetChanged();

}
但在此之前,请确保您已经将适配器附加到自动完成文本视图,如果不这样做的话,如下所示:

ArrayAdapter<String> stringArrayAdapterPatient= new ArrayAdapter<String>(getActivity(),android.support.v7.appcompat.R.layout.select_dialog_item_material,stringArrayListPatients);

completeTextViewPatient.setAdapter(stringArrayAdapterPatient);
isr3a4wc

isr3a4wc3#

使用adapter.notifyDataSetChanged()方法通知列表中的更改,如果该方法不起作用,则可以像autoCompleteTextView.showDropDown()一样手动显示DropDown

4xy9mtcn

4xy9mtcn4#

AutoCompleteTextView eT = (AutoCompleteTextView)findViewById(R.id.searchAutoCompleteTextView_feed);
 //   eT.addTextChangedListener(this);
    String[] sa = new String[]{"apple", "mango", "banana", "apple mango", "mango banana"};
    ArrayAdapter<String> aAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, sa);
    eT.setAdapter(aAdapter);

它的工作只是对et.addtext行进行注解...

wnvonmuf

wnvonmuf5#

更新适配器并立即通知更改show dropDown后,唯一可行的解决方案是再次重置AutoCompleteTextView文本,Kotlin示例:

with(autoCompleteTextView) {
       text = text
  // Place cursor to end   
}

Java类似于:

autoCompleteTextView.setText(autoCompleteTextView.getText());
// Place cursor to end
pkwftd7m

pkwftd7m6#

自动完成文本视图。Invalidate()将执行此操作。

lsmepo6l

lsmepo6l7#

如果有人使用自定义对象数组列表,并面临这个问题,请检查您的模型类,看看您是否覆盖了toString中的正确变量。如果您还没有覆盖,请覆盖toString。

public class MyModalClass {
    public int id;
    public String path;

    @Override
    public String toString() { //include this in your model and return what you need in your drop down
        return path;
    }
}

相关问题