AutoCompleteTextView stops listening to onItemClick

帅比萌擦擦* 提交于 2020-01-06 18:53:28

问题


Existing code and xml The code

fromAutoComplete = new AutoComplete(
        this, R.layout.fromautocomplete,
        R.id.fromautocomplete);
fromAutoComplete.setNotifyOnChange(true);
fromAddress = (AutoCompleteTextView) findViewById(R.id.fromAddress);
fromAddress.setAdapter(fromAutoComplete);
fromAddress.setOnItemClickListener(this);
fromAddress.setOnFocusChangeListener(this);
fromAddress.setOnClickListener(this);

The xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fromautocomplete"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:ellipsize="none"
    android:maxLines="2"
    android:minLines="2"
    android:singleLine="false"
    android:text="sample text, a lot of text to encourage wrap text"/>

I need the autocomplete to wrap text, on the listview. So I add a LinearLayout around my autocomplete. This works. The listview on the autocomplete now has wrapped text. but on selection non of my call backs are not called.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
<TextView 
    android:id="@+id/fromautocomplete"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:maxLines="2"
    android:minLines="2"
    android:scrollHorizontally="false"
    android:singleLine="false"
    android:text="sample text, a lot of text to encourage wrap text"/>
 </LinearLayout>

回答1:


Got it working,

Fixes

  1. Gave a id to my LinearLayout
  2. Used the layout id in my onItemClick
  3. Used R.layout.<file> in the constructor

XML (R.layout.fromautocomplete)

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

    <TextView
        android:id="@+id/toautocomplete"

Code

fromAutoComplete = new AutoComplete(this,
         R.layout.fromautocomplete,
                R.id.fromautocomplete);
        fromAutoComplete.setNotifyOnChange(true);

OnItemClick

@Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        if (view.getId() == R.id.fromautocompleteLayout) {



回答2:


You need to get Textview from LinearLayout

@Override
public void onItemClick (AdapterView<?> parent, View view, int position, long id) {

                View v = (TextView)((LinearLayout )view).getChildAt(0);
                String s = ((TextView) v).getText().toString();
               //Do something with string s
}


来源:https://stackoverflow.com/questions/25751648/autocompletetextview-stops-listening-to-onitemclick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!