How do I make the autocomplete prediction clickable?

三世轮回 提交于 2019-12-11 12:42:51

问题


When I enter something, I get an autocomplete prediction. Like when I enter "Joh", I get "John F. Kennedy". Now, if the user clicks on "John F. Kennedy", it takes him into an activity that gives him info about John F. Kennedy. What should I do to get this ? How do I make the autocomplete prediction clickable ?

package com.mavenmaverick.autocomplete_test;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;


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

}

}


回答1:


Use this code.

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"
                    };
private AutoCompleteTextView myAutoComplete;

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

        myAutoComplete = (AutoCompleteTextView) findViewById(R.id.myautocomplete);
        myAutoComplete.addTextChangedListener(this);
        myAutoComplete.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, presidents));



myAutoComplete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(getBaseContext(), InforActivity.class);
                    intent.putExtra("president_name", obj.toString());
                    startActivity(intent);
                    finish();

            }
        });

}



回答2:


You can do try below code,

autoCompleteTextview.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
        //... your stuff
    }
})

And you have a one more option, to set the click listener when the user actually selects an item in the dropdown list using setOnItemSelectedListener.

autoCompleteTextview.setOnSelectedListener(new OnItemClickListener() {
    @Override
    public void onItemSelected (AdapterView<?> parent, View view, int position, long id) {
        //... your stuff
    }
});

Edit

if(position == 1){
Intent i = new Intent (MainActivity.this, johncanedy.class);
startActivity(i);
}else if (position == 2){
Intent i = new Intent (MainActivity.this, lyndon.class);
startActivity(i);
} 

and so on...



来源:https://stackoverflow.com/questions/22877325/how-do-i-make-the-autocomplete-prediction-clickable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!