简体   繁体   English

自动完成预测后不切换活动

[英]Not switching activities after autocomplete prediction

在此处输入图片说明

When I enter "Joh", I get "John F. Kennedy". 当我输入“ Joh”时,我得到“ John F. Kennedy”。 Now, when I click on "John F. Kennedy", it does not take me to another activity that gives info about John F. Kennedy. 现在,当我单击“ John F. Kennedy”时,并没有带我去进行有关John F. Kennedy的信息的其他活动。 But I have programmed it to do so. 但是我已经对此进行了编程。 What is wrong with my code ? 我的代码有什么问题?

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= {
                    "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((OnItemSelectedListener) this);
    textView.setOnItemClickListener((OnItemClickListener) this);


    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);
            startActivity(i);
            }
    }
    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }
   });

    }
}

The main problem is the following: 主要问题如下:

@Override
public void onItemSelected(AdapterView<?> parent, View view, int index, long id)
{
    int position=0;

    // once the logic reaches this part, the value
    // of "position" will always be 0 because that 
    // is what you are setting it too. Therefore, 
    // the following if statement is skipped.
    if(position == 1) {
        Intent i = new Intent (MainActivity.this, SecondActivity.class);
        startActivity(i);
    }
}

What you want to change it too: 您也想更改它:

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

The putExtra method is used to pass information between MainActivity and SecondActivity . putExtra方法用于在MainActivitySecondActivity之间传递信息。 The first parameter it takes is a String that is used as a key, and the second paramter is the actual value. 它采用的第一个参数是用作键的String ,第二个参数是实际值。

"KEY" can be any value, just remember that you will be using that same key in SecondActivity to get the value. "KEY"可以是任何值,只需记住您将在SecondActivity使用相同的键来获取该值。 For example: 例如:

String presidentClickedOn = getIntent().getStringExtra("KEY");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM