Listview 单击列表视图的项目时,如何转到其他活动?
我在应用程序的第一页有搜索栏,当我写搜索栏时打开搜索辅助(帮助列表视图),我想在单击打开的列表视图的项目时转到应用程序的其他活动 谢谢你的帮助Listview 单击列表视图的项目时,如何转到其他活动?,listview,android-intent,android-listview,android,Listview,Android Intent,Android Listview,Android,我在应用程序的第一页有搜索栏,当我写搜索栏时打开搜索辅助(帮助列表视图),我想在单击打开的列表视图的项目时转到应用程序的其他活动 谢谢你的帮助 import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class MainActivity extends Activity {
List<HashMap<String,String>> adp;
// Array of strings storing country names
String[] countries = new String[] {
"India",
"Pakistan",
"Sri Lanka",
"China",
"Bangladesh",
"Nepal",
"Afghanistan",
"North Korea",
"South Korea",
"Japan"
};
// Array of integers points to images stored in /res/drawable-ldpi/
int[] flags = new int[]{
R.drawable.india,
R.drawable.pakistan,
R.drawable.srilanka,
R.drawable.china,
R.drawable.bangladesh,
R.drawable.nepal,
R.drawable.afghanistan,
R.drawable.nkorea,
R.drawable.skorea,
R.drawable.japan
};
// Array of strings to store currencies
String[] currency = new String[]{
"Indian Rupee",
"Pakistani Rupee",
"Sri Lankan Rupee",
"Renminbi",
"Bangladeshi Taka",
"Nepalese Rupee",
"Afghani",
"North Korean Won",
"South Korean Won",
"Japanese Yen"
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Each row in the list stores country name, currency and flag
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<10;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("txt", countries[i]);
hm.put("flag", Integer.toString(flags[i]) );
hm.put("cur", currency[i]);
aList.add(hm);
}
// Keys used in Hashmap
String[] from = { "flag","txt"};
// Ids of views in listview_layout
int[] to = { R.id.flag,R.id.txt};
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.autocomplete_layout, from, to);
// Getting a reference to CustomAutoCompleteTextView of activity_main.xml layout file
CustomAutoCompleteTextView autoComplete = ( CustomAutoCompleteTextView) findViewById(R.id.autocomplete);
/** Defining an itemclick event listener for the autocompletetextview */
OnItemClickListener itemClickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
}
};
/** Setting the itemclick event listener */
autoComplete.setOnItemClickListener(itemClickListener);
/** Setting the adapter to the listView */
autoComplete.setAdapter(adapter);
}
/** A callback method, which is executed when this activity is about to be killed
* This is used to save the current state of the activity
* ( eg : Configuration changes : portrait -> landscape )
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
TextView tvCurrency = (TextView) findViewById(R.id.tv_currency) ;
outState.putString("currency", tvCurrency.getText().toString());
super.onSaveInstanceState(outState);
}
/** A callback method, which is executed when the activity is recreated
* ( eg : Configuration changes : portrait -> landscape )
*/
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
TextView tvCurrency = (TextView) findViewById(R.id.tv_currency) ;
tvCurrency.setText(savedInstanceState.getString("currency"));
super.onRestoreInstanceState(savedInstanceState);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
import java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入android.app.Activity;
导入android.os.Bundle;
导入android.view.Menu;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.AdapterView.OnItemClickListener;
导入android.widget.simpledapter;
导入android.widget.TextView;
公共类MainActivity扩展了活动{
列出adp;
//存储国家名称的字符串数组
字符串[]国家/地区=新字符串[]{
“印度”,
“巴基斯坦”,
“斯里兰卡”,
“中国”,
“孟加拉国”,
“尼泊尔”,
“阿富汗”,
“朝鲜”,
“韩国”,
“日本”
};
//整数数组指向存储在/res/drawable ldpi中的图像/
int[]标志=新的int[]{
R.drawable.印度,
R.drawable.巴基斯坦,
R.drawable.srilanka,
R.drawable.china,
R.drawable.孟加拉国,
R.drawable.尼泊尔,
R.drawable.阿富汗,
R.drawable.nkorea,
R.drawable.skorea,
日本
};
//存储货币的字符串数组
字符串[]货币=新字符串[]{
“印度卢比”,
“巴基斯坦卢比”,
“斯里兰卡卢比”,
“人民币”,
“孟加拉国塔卡”,
“尼泊尔卢比”,
“阿富汗人”,
“朝鲜赢了”,
“韩元”,
“日元”
};
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//列表中的每一行存储国家名称、货币和国旗
列表列表=新的ArrayList();
对于(inti=0;i,首先在AndroidManifest中声明要启动的活动
<application
...
<activity
android:name="com.hello.world.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.hello.world.MyNextActivity"/>
...
</application>
在您当前的活动中
countries.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
if (position == 0) {
Intent india = new Intent(MainActivity.this, newActivityIndia.class);
startActivity(india);
}
else if (position == 1) {
Intent pakistan = new Intent(MainActivity.this, newActivityPakistan.class);
startActivity(pakistan);
}
else if (position == 2) {
Intent shriLanka = new Intent(MainActivity.this, newActivityShriLanka.class);
startActivity(shriLanka);
}
else if (position == 3) {
Intent china= new Intent(MainActivity.this, newActivityChina.class);
startActivity(china);
}
else if (position == 4) {
Intent bangladesh= new Intent(MainActivity.this, newActivityBangladesh.class);
startActivity(bangladesh);
}
else if (position == 5) {
Intent nepal= new Intent(MainActivity.this, newActivityNepal.class);
startActivity(nepal);
}
else if (position == 6) {
Intent afghanistan = new Intent(MainActivity.this, newActivityAfghanistan.class);
startActivity(afghanistan);
}
else if (position == 7) {
Intent northKorea= new Intent(MainActivity.this, newActivityNorthKorea.class);
startActivity(northKorea);
}
else if (position == 8) {
Intent southKorea= new Intent(MainActivity.this, newActivitySouthKorea.class);
startActivity(southKorea);
}
else if (position == 9) {
Intent japan= new Intent(MainActivity.this, newActivityJapan.class);
startActivity(japan);
}
}
};
countries.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView AdapterView,视图视图,整型位置,长id){
如果(位置==0){
意向印度=新意向(MainActivity.this,newActivityIndia.class);
星触觉(印度);
}
否则如果(位置==1){
意向巴基斯坦=新意向(MainActivity.this,newActivityPakistan.class);
startActivity(巴基斯坦);
}
否则如果(位置==2){
Intent shriLanka=新Intent(MainActivity.this,newActivityShriLanka.class);
星触觉(斯里兰卡);
}
否则如果(位置==3){
意向中国=新意向(MainActivity.this,newActivityChina.class);
星触觉(中国);
}
否则如果(位置==4){
意向孟加拉=新意向(MainActivity.this,newActivityBangladesh.class);
startActivity(孟加拉国);
}
否则如果(位置==5){
意向尼泊尔=新意向(MainActivity.this、newActivityNepal.class);
startActivity(尼泊尔);
}
否则如果(位置==6){
意向阿富汗=新意向(MainActivity.this,newActivityAfghanistan.class);
startActivity(阿富汗);
}
否则如果(位置==7){
Intent northKorea=新Intent(MainActivity.this,newActivityNorthKorea.class);
星际触觉(朝鲜);
}
否则如果(位置==8){
Intent southKorea=新Intent(MainActivity.this、newActivitySouthKorea.class);
startActivity(韩国);
}
否则如果(位置==9){
意向日本=新意向(MainActivity.this,newActivityJapan.class);
星触觉(日本);
}
}
};
例如,您可以使用switch语句
adapter= new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, arrayCountry);
listView.setAdapter(adapter);
getResources().getStringArray(R.array.array_country);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
switch (position) {
case 0:
startActivity(new Intent(MainActivity.this, ActivityAustralia.class));break;
case 1:
startActivity(new Intent(MainActivity.this, ActivityJapan.class));break;
case 2:
startActivity(new Intent(MainActivity.this, ActivityChina.class));break;
}
}
});
listView.setAdapter(adapter);
}
adapter=newArrayAdapter(MainActivity.this,android.R.layout.simple\u list\u item\u 1,arrayCountry);
setAdapter(适配器);
getResources().getStringArray(R.array.array_country);
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
公共虚线单击(AdapterView AdapterView,视图视图,内部位置,长l){
开关(位置){
案例0:
startActivity(新意图(MainActivity.this,ActivityAustralia.class));中断;
案例1:
startActivity(新意图(MainActivity.this,ActivityJapan.class));break;
案例2:
startActivity(新意图(MainActivity.this,ActivityChina.class));中断;
}
}
});
setAdapter(适配器);
}
hi和tanks for answer但是我有几个活动,我想在单击每个项目时转到不同的活动请给我答案您可以使用switch
或if
语句在OnItemClickListener
中决定哪个活动将开始。然后,在替换M时重复给定的代码yNextActivity
对于您拥有的每个不同的活动
如果(itemClicked.equals(“India”){Intent Intent Intent=new Intent(MainActivity.this,TestPage.class);startActivity(Intent);}添加一些解释,说明此答案如何帮助解决当前问题
countries.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
if (position == 0) {
Intent india = new Intent(MainActivity.this, newActivityIndia.class);
startActivity(india);
}
else if (position == 1) {
Intent pakistan = new Intent(MainActivity.this, newActivityPakistan.class);
startActivity(pakistan);
}
else if (position == 2) {
Intent shriLanka = new Intent(MainActivity.this, newActivityShriLanka.class);
startActivity(shriLanka);
}
else if (position == 3) {
Intent china= new Intent(MainActivity.this, newActivityChina.class);
startActivity(china);
}
else if (position == 4) {
Intent bangladesh= new Intent(MainActivity.this, newActivityBangladesh.class);
startActivity(bangladesh);
}
else if (position == 5) {
Intent nepal= new Intent(MainActivity.this, newActivityNepal.class);
startActivity(nepal);
}
else if (position == 6) {
Intent afghanistan = new Intent(MainActivity.this, newActivityAfghanistan.class);
startActivity(afghanistan);
}
else if (position == 7) {
Intent northKorea= new Intent(MainActivity.this, newActivityNorthKorea.class);
startActivity(northKorea);
}
else if (position == 8) {
Intent southKorea= new Intent(MainActivity.this, newActivitySouthKorea.class);
startActivity(southKorea);
}
else if (position == 9) {
Intent japan= new Intent(MainActivity.this, newActivityJapan.class);
startActivity(japan);
}
}
};
adapter= new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, arrayCountry);
listView.setAdapter(adapter);
getResources().getStringArray(R.array.array_country);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
switch (position) {
case 0:
startActivity(new Intent(MainActivity.this, ActivityAustralia.class));break;
case 1:
startActivity(new Intent(MainActivity.this, ActivityJapan.class));break;
case 2:
startActivity(new Intent(MainActivity.this, ActivityChina.class));break;
}
}
});
listView.setAdapter(adapter);
}