Marvin
Marvin

Reputation: 55

Autocomplete-Textview with listener

My question is very basic, as is my experience! Sorry about the length of the post!

I am trying to create an AutoCompleteTextView with a listener so that I can do "stuff" when an item is selected. The AutoCompleteTextView works fine, in so far as I can type in 2 characters and then select an item from the list which appears. But the application never executes the listener code.

All my code, xml and log trace is here, so what is the simple piece that I am missing? I've searched all the questions and it seems to be a common problem but none of the (very few) answers work for me.

Any help will be appreciated.

SOURCE

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;

public class A_New_StartActivity extends Activity {

private final String LOG_TAG = "CJM";
private static String result = "No result";
private static final String[] myitems =  {
"Item 1",
"Item 2",
"Item 3",
"Item 4",
"Item 5"
};
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Log.d(LOG_TAG, "Entered onCreate");

    ArrayAdapter<String> itemAdapter = new ArrayAdapter<String>(this, R.layout.destination_item, myitems);
    AutoCompleteTextView itemView = (AutoCompleteTextView) findViewById(R.id.autocomplete_items);
    itemView.setAdapter(itemAdapter);

    itemView.setOnItemSelectedListener(new OnItemSelectedListener() { 
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
            result = "Selected";
            Log.d(LOG_TAG, "Showing Result 1");
            TextView result_view = (TextView) findViewById(R.id.result_view); 
            result_view.setText(result);
            View scroll_visibility = findViewById(R.id.result_scroll); 
            scroll_visibility.setVisibility(View.VISIBLE); 
            View result_visibility = findViewById(R.id.result_view); 
            result_visibility.setVisibility(View.VISIBLE); 
        } 
        public void onNothingSelected(AdapterView<?> parent) { 
            Log.d(LOG_TAG, "In Nothing Selected");
            result = "Nothing";
            Log.d(LOG_TAG, "Showing Result 2");
            TextView result_view = (TextView) findViewById(R.id.result_view); 
            result_view.setText(result);
            View scroll_visibility = findViewById(R.id.result_scroll); 
            scroll_visibility.setVisibility(View.VISIBLE); 
            View result_visibility = findViewById(R.id.result_view); 
            result_visibility.setVisibility(View.VISIBLE); 
        }
    }); 
    Log.d(LOG_TAG, "completed onCreate");
}
}

MAIN.xml in R.layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5dp" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dp" >
</TextView>

<TextView 
    android:id="@+id/item_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:text="@string/request_item"
    android:visibility="visible" />

<AutoCompleteTextView 
    android:id="@+id/autocomplete_items"
    android:completionThreshold="2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:visibility="visible" />

<ScrollView
    android:id="@+id/result_scroll"
    android:layout_width="220dp"
    android:layout_height="290dp"
    android:layout_marginTop="10dp" 
    android:layout_marginLeft="50dp"
    android:visibility="invisible" >

    <TextView
        android:id="@+id/result_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:maxLines="5"
        android:scrollbars="vertical"
        android:text="@string/result"
        android:visibility="invisible" />
</ScrollView>

<Button
    android:id="@+id/reset_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp" 
    android:layout_marginLeft="125dp"
    android:text="@string/button_text" />

DESTINATION_ITEM.xml in R.layout

<TextView 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

STRINGS.xml in R.values

<resources>

<string name="app_name">A_New_Start</string>
<string name="request_item">Please enter the first 2 letters of the item</string>
<string name="result">Initialised</string>
<string name="button_text">OK</string>

LOG output

05-26 10:19:05.550: D/CJM(269): Entered onCreate

05-26 10:19:05.570: D/CJM(269): completed onCreate

Upvotes: 2

Views: 3386

Answers (1)

Lalit Poptani
Lalit Poptani

Reputation: 67286

You are setting wrong Listener for AutoCompleteTextView. You have to set addTextChangedListener() and do the processing in the override methods of addTextChangedListener.

Upvotes: 2

Related Questions