如何点击列表视图中的项目打开另一个活动。例如,如果我点击法国它应该显示吐司并打开我的france.java活动。这只是为了测试和体验,我还在学习如何正确布局或?格式代码。
public class weapons extends Activity {
ListView list;
String[] web = {
"Brazil",
"France",
"Japan",
"USA",
"Russia"
} ;
Integer[] imageId = {
R.drawable.brazil,
R.drawable.france,
R.drawable.japan,
R.drawable.usa,
R.drawable.russia,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.weapons);
CustomList adapter = new
CustomList(weapons.this, web, imageId);
list=(ListView)findViewById(R.id.list22);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(weapons.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
}
});
}
}
感谢。
答案 0 :(得分:0)
使用Intent启动Activity 在onItemClick
中使用以下代码Intent intent = new Intent(this, YourNextActivity.class);
startActivity(intent);
答案 1 :(得分:0)
您可以在onItemClick
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(weapons.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
Intent intent = new Intent(weapons.this, NewActivity.class);
startActivity(intent);
}
答案 2 :(得分:0)
看看这个。
mlistView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text Game, Help, Home
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, Activity.class);
startActivity(intent);
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
答案 3 :(得分:0)
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String clicked=(String) parent.getItemAtPosition(position);
switch(clicked){
case "Brazil":
Toast.makeText(weapons.this, "You Clicked at "+clicked, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, Brazil.class);
startActivity(intent);
break;
case "France":
Toast.makeText(weapons.this, "You Clicked at "+clicked, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, France.class);
startActivity(intent);
default:
break;}
}
});
答案 4 :(得分:0)
使用以下代码:
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
TextView tv = (TextView) arg1.findViewById(R.id.txt);
String str = tv.getText().toString().trim();
if (str.equals(web[0])) {
Intent int0 = new Intent(weapons.this, Brazil.class);
startActivity(int0);
}else if(str.equals(web[1])) {
Intent int1 = new Intent(weapons.this, France.class);
startActivity(int1);
}else if(str.equals(web[2])) {
Intent int2 = new Intent(weapons.this, Japan.class);
startActivity(int2);
}else if(str.equals(web[3])) {
Intent int3 = new Intent(weapons.this, USA.class);
startActivity(int3);
}else if(str.equals(web[4])) {
Intent int1 = new Intent(weapons.this, Russia.class);
startActivity(int1);
}
}
答案 5 :(得分:0)
使用此代码
Toast.makeText(this, "your message", Toast.LENGTH_LONG).show();
(new Handler())
.postDelayed(
new Runnable() {
public void run() {
Intent intent = new Intent(this, Activity.class);
startActivity(intent);
}
}, timeInMillisecondTheToastIsShowingFor);