2

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

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

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

任何帮助将不胜感激。

来源

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
4

1 回答 1

2

您为 AutoCompleteTextView 设置了错误的侦听器。您必须addTextChangedListener()addTextChangedListener.

于 2012-05-26T13:12:03.513 回答