13

我的 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);

这根本不起作用......我的意思是它就像一个 EditTextView 一样工作。我哪里错了??

完整代码:

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

    }
}
4

6 回答 6

21

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

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

在我的 textwatcher 的 onTextChanged 方法中,我得到了使用 asynctask 的建议

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

然后在 AsyncTask 的 onPostExecute 中更新 autocompletetextview

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

有关详细信息,请参阅setNotifyOnChangenotifyDataSetChanged

于 2012-05-30T11:06:56.167 回答
3

这是我项目的一个片段。我认为在您从服务中获取数据后,您所要做的就是:

  1. 清除您以前的数据。
  2. 清除以前的适配器值。
  3. 然后使用 add() 或 addAll() 方法将值添加到您的数据列表中。
  4. 通过在适配器上调用 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);
于 2017-01-23T12:43:56.223 回答
1

使用adapter.notifyDataSetChanged()方法通知列表中的更改,如果这不起作用,那么您可以手动显示 DropDownautoCompleteTextView.showDropDown()

于 2021-03-15T14:14:19.503 回答
0
    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 行......

于 2012-05-25T11:29:28.773 回答
0

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

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

Java类似于:

autoCompleteTextView.setText(autoCompleteTextView.getText());
// Place cursor to end  
于 2020-01-16T14:32:24.887 回答
0

AutoCompleteTextView.Invalidate() 会做到这一点。

于 2020-05-06T08:10:11.450 回答