在AutoCompleteTextView中选择选项时不切换到另一个Activity

问题描述 投票:1回答:3

在我在AutoCompleteTextView学习Android的过程中,我成功地做了一个AutoCompleteTextView给出了预测。现在,我想通过点击预测从一个Activity切换到另一个。就像输入Ric,我预测Richard Nixon和点击'理查德尼克松'它带我到另一个Activity。我已为此编写代码,但它似乎不起作用。请帮忙。

package com.mavenmaverick.autocomplete_test;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;
import android.view.inputmethod.InputMethodManager;


public class MainActivity extends Activity {

String[] presidents= {"Rahul",
                    "John F. Kennedy",
                    "Lyndon B. Johnson",
                    "Richard Nixon",
                    "Gerald Ford",
                    "Jimmy Carter",
                    "Ronald Reagan",
                    "George H. W. Bush",
                    "Bill Clinton",
                    "George W. Bush",
                    "Barack Obama"
                    };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, presidents);

    textView.setThreshold(3);
    textView.setAdapter(adapter);

        textView.setOnItemSelectedListener(new OnItemSelectedListener()
        {   
        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                   int index, long id)
        {
        int position=0;
        if(position == 1){
            Intent i = new Intent (MainActivity.this, SecondActivity.class);
            i.putExtra("KEY", presidents[index]);
            startActivity(i);
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }
});         
}
}

的Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mavenmaverick.autocomplete_test"
    android:versionCode="1"
    android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.mavenmaverick.autocomplete_test.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>
java android autocompletetextview
3个回答
1
投票

我不知道你是否已经解决了你的问题,但如果你没有解决问题,那么你可以采取以下措施来解决问题。您应该使用OnItemClickListener()而不是OnItemSelectedListener()。我在AdapterView中找不到这两者之间的区别,但我可以说在调试代码时从未调用它(OnItemSelectedListener())。这是替代品:

textView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int index, long id) {
            if(index == 1){
                Intent i = new Intent (MainActivity.this, SecondActivity.class);
                i.putExtra("KEY", presidents[index]);
                startActivity(i);
            }
        }
    });

还要记住onItemClick()中的索引是单击项目的位置。因此,如果您单击下拉列表中的第一个项目(index = 0),例如“Bill Clinton”,则将“Rahul”作为Intent的额外内容。

根据您的新问题编辑。做如下:

textView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int index, long id) {
        String president = ((TextView) view).getText().toString();
        Intent intent = null;

        if (president.equals("John F. Kennedy"))
            intent = new Intent (MainActivity.this, KennedyActivity.class);
        if (president.equals("Lyndon B. Johnson"))
            intent = new Intent (MainActivity.this, JohnsonActivity.class);
        if (president.equals("Richard Nixon"))
            intent = new Intent (MainActivity.this, NixonActivity.class);
        if (president.equals("Gerald Ford"))
            intent = new Intent (MainActivity.this, FordActivity.class);
        if (president.equals("Jimmy Carter"))
            intent = new Intent (MainActivity.this, CarterActivity.class);
        if (president.equals("Ronald Reagan"))
            intent = new Intent (MainActivity.this, ReaganActivity.class);
        if (president.equals("George H. W. Bush"))
            intent = new Intent (MainActivity.this, SrBushActivity.class);
        if (president.equals("Bill Clinton"))
            intent = new Intent (MainActivity.this, ClintonActivity.class);
        if (president.equals("George W. Bush"))
            intent = new Intent (MainActivity.this, JrBushActivity.class);
        if (president.equals("Barack Obama"))
            intent = new Intent (MainActivity.this, ObamaActivity.class);

        if (intent != null) {
            intent.putExtra("KEY", president);
            startActivity(intent);
        }
    }
});

0
投票

拉胡尔在这里你错了,

int position = 0;
if(position == 1) { ...

将您的职位初始化为0将永远不会将您的条件设置为真,请参阅您的代码

 @Override
    public void onItemSelected(AdapterView<?> parent, View view,
               int index, long id)
    {
    int position=0; // Setting position to 0 always and your condition will never be true
    if(position == 1){
        Intent i = new Intent (MainActivity.this, SecondActivity.class);
        i.putExtra("KEY", presidents[index]);
        startActivity(i);
        }
    }

0
投票

在Manifest文件中添加此代码。以上标签。

<activity
    android:name="com.mavenmaverick.autocomplete_test.SecondActivity"
    android:label="@string/app_name" >

</activity>
© www.soinside.com 2019 - 2024. All rights reserved.