2014-04-05 25 views
1

當我輸入內容時,我會得到一個自動完成預測。就像我輸入「Joh」時,我會得到「John F. Kennedy」。現在,如果用戶點擊「John F. Kennedy」,它將他帶入一個活動,爲他提供關於John F. Kennedy的信息。我該怎麼做才能得到這個?如何使自動填充預測可點擊?如何使自動填充預測可點擊?

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

} 

} 

Autocomplete_Test

回答

0

你可以做嘗試下面的代碼,

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

而且你有一個選擇,設置點擊監聽,當用戶在下拉列表中實際選擇一個項目使用setOnItemSelectedListener。

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

編輯

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

等等...

+0

我發佈了我的源代碼。你會好好闡述你的答案嗎? – CodeWalker

+0

@RahulShaw,檢查我的編輯,通過上面的代碼獲取位置,然後檢查條件,如果pos是1,然後將意圖傳遞到第二個屏幕,如果pos是2,那麼再次是新屏幕。 – InnocentKiller

+0

@RahulShaw,檢查我的編輯。它會幫助你。 – InnocentKiller

0

使用此代碼。

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

      } 
     }); 

}