質問

In my progress of learning AutoCompleteTextView in Android, I successfully made an AutoCompleteTextView giving predictions. Now, I want to switch from one Activity to another by clicking a prediction. Like by typing Ric, I am predicted about Richard Nixon and by clicking 'Richard Nixon' it takes me to another Activity. I have written code for that but it doesn't seem to work. Please help.

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

    }
});         
}
}

Autocomplete

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>
役に立ちましたか?

解決

I don't know if you've resolved your problem, but if you haven't, here is what you can do to make it work. You should use OnItemClickListener() instead of OnItemSelectedListener(). I couldn't find the difference between those two in AdapterView, but I can say that it (OnItemSelectedListener()) was never invoked while debugging your code. Here is the replacement:

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

Also keep in mind that index in onItemClick() is the position of item clicked. So if you click the first item from the drop down list (index=0) which is, say, "Bill Clinton", you put "Rahul" as extra for the Intent.

EDIT according to your new question. Do as follows:

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

他のヒント

Rahul here you go wrong,

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

initializing your position to 0 will never set your condition true, See your code

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

Add this code in your Manifest file. Above tag.

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

</activity>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top