自动完成 - 带有侦听器的Textview

时间:2012-05-26 12:52:54

标签: android autocompletetextview

我的问题非常基本,我的经验也是如此!抱歉帖子的长度!

我正在尝试使用监听器创建AutoCompleteTextView,以便在选择项目时可以执行“填充”操作。 AutoCompleteTextView工作正常,只要我输入2个字符,然后从列表中选择一个项目。 但应用程序从不执行侦听器代码。

我的所有代码,xml和日志跟踪都在这里,那么我缺少的那个简单的部分是什么? 我搜索了所有问题,这似乎是一个常见的问题,但是(很少)答案都不适用于我。

任何帮助将不胜感激。

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");
}
}

R.layout中的MAIN.xml

<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" />

R.layout中的DESTINATION_ITEM.xml

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

R.values中的STRINGS.xml

<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>

日志输出

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

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

1 个答案:

答案 0 :(得分:2)

您为AutoCompleteTextView设置了错误的侦听器。您必须设置addTextChangedListener()并在addTextChangedListener的覆盖方法中进行处理。