在我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
}
});
}
}
。我已经为此编写了代码,但它似乎没有用。请帮忙。
<?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>
的Manifest.xml:
{{1}}
答案 0 :(得分: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),即“比尔克林顿”,则将“Rahul”作为意图的额外内容。
根据您的新问题进行编辑。请执行以下操作:
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);
}
}
});
答案 1 :(得分:0)
Rahul,你出错了,
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);
}
}
答案 2 :(得分:0)
在您的清单文件中添加此代码。标签上方。
<activity
android:name="com.mavenmaverick.autocomplete_test.SecondActivity"
android:label="@string/app_name" >
</activity>