如何在新打开时敬酒点击的项目名称 activity
How to toast clicked item name in new open activity
当我点击列表项目时,我想打开一个新的 activity 并显示项目名称被点击。我尝试了很多,但点击的项目名称不是吐司,一个新的activity打开但不显示项目名称,我如何在新的打开activity中获取点击的项目名称?那我该怎么做呢?
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String strName = listitem.get(arg2).getOppid();
Toast.makeText(Welcome.this, strName, Toast.LENGTH_LONG)
.show();
Intent intent = new Intent(getApplicationContext(), Second_activity.class);
startActivity(intent);
}
});
它成功地在同一个 activity 中烘烤项目名称,但我还想在新的 activity 中显示项目名称。
我想吐司在那一秒点击项目名称activity我试过这种方法但没能解决。
public class Second_activity extends Activity {
ListView lv;
ArrayList<Services>listitem;
String title,name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_activity);
Intent i = getIntent();
name= i.getStringExtra("name");
Toast.makeText(Second_activity.this, name, Toast.LENGTH_LONG).show();
您可以通过 Intent (intent.putExtra()
) 或通过 EventBus 发送字符串,或将其放在 SharedPreferences 中。选择你喜欢的。
- 如果您不需要发送任何复杂的数据,Intent 会做得很好,只是
一个简单的
string
、int
之类的。
- 如果您需要在以下时间访问该数据,则 SharedPreferences 很有意义
您下次访问您的应用程序,这意味着应该保存数据。
- 如果您希望发送您自己的自定义对象,请使用 EventBus
包含存储在其中的各种数据。
试试这个
Toast.makeText(getApplicationContext(), strName, Toast.LENGTH_LONG).show();
将name放入extras并传递给new activity。在新 activity 中从临时演员中获取名称并显示。
将此添加到您的代码中
Intent intent = new Intent(getApplicationContext(), Second_activity.class);
intent.putStringExtra("name",strName);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String strName = listitem.get(arg2).getName();
Toast.makeText(Welcome.this, strName, Toast.LENGTH_LONG)
.show();
Intent intent = new Intent(this, Second_activity.class);
intent.putExtra("string", strName);
startActivity(intent);
}
});
你的第二个activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_activity);
Intent intent = getIntent();
String name= intent.getExtras().getString("string");
Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();
}
Bundle bundle = new Bundle();
String strName = listitem.get(arg2).getOppid();
bundle.putString("msg", strName);
Intent intent =newIntent(getApplicationContext(),Second_activity.class);
intent.putExtras(bundle);
然后在secondActivity的onCreate
String value = getIntent().getExtras().getString(key);
Toast.makeText(Welcome.this, value, Toast.LENGTH_LONG).show();
修改了您的代码
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String strName = listitem.get(arg2).getOppid();
Toast.makeText(Welcome.this, strName, Toast.LENGTH_LONG)
.show();
Intent intent = new Intent(getApplicationContext(), Second_activity.class);
//add below line to take clicked item to next activity
intent.putExtra("ITEMCLICKED",strName);
startActivity(intent);
}
});
在第二个activity的onCreate方法中添加下面的代码
String mClickedItem;
//get the inten from the previous activity
Intent intent=getIntent();
//intent.hasExtra("ITEMCLICKED") to check intent has the value which we have set in previous activity
if(intent.hasExtra("ITEMCLICKED")){
mClickedItem = intent.getStringExtra("ITEMCLICKED");
}
Toast.makeText(Welcome.this, mClickedItem,Toast.LENGTH_LONG)
.show();
看看这个:
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String strName = listitem.get(arg2).getOppid();
Toast.makeText(Welcome.this, strName, Toast.LENGTH_LONG)
.show();
Intent intent = new Intent(getApplicationContext(), Second_activity.class);
intent.putExtra("name", strName);//add this line
startActivity(intent);
}
});
这是您的代码:
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String strName = listitem.get(arg2).getOppid();
Toast.makeText(Welcome.this, strName, Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(), Second_activity.class);
// you need to set the string here
startActivity(intent);
}
});
因此,您必须在第一个 activity:
上进行设置
intent.putExtra("item-name", strName);
在你的第二个 Activity:
Intent intent = getIntent();
String name = intent.getExtras().getString("item-name");
Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();
此外,我建议您先检查一下您的额外费用:
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if(extras.containsKey("item-name")){
//asking for your key and do something with it
}
当我点击列表项目时,我想打开一个新的 activity 并显示项目名称被点击。我尝试了很多,但点击的项目名称不是吐司,一个新的activity打开但不显示项目名称,我如何在新的打开activity中获取点击的项目名称?那我该怎么做呢?
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String strName = listitem.get(arg2).getOppid();
Toast.makeText(Welcome.this, strName, Toast.LENGTH_LONG)
.show();
Intent intent = new Intent(getApplicationContext(), Second_activity.class);
startActivity(intent);
}
});
它成功地在同一个 activity 中烘烤项目名称,但我还想在新的 activity 中显示项目名称。
我想吐司在那一秒点击项目名称activity我试过这种方法但没能解决。
public class Second_activity extends Activity {
ListView lv;
ArrayList<Services>listitem;
String title,name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_activity);
Intent i = getIntent();
name= i.getStringExtra("name");
Toast.makeText(Second_activity.this, name, Toast.LENGTH_LONG).show();
您可以通过 Intent (intent.putExtra()
) 或通过 EventBus 发送字符串,或将其放在 SharedPreferences 中。选择你喜欢的。
- 如果您不需要发送任何复杂的数据,Intent 会做得很好,只是
一个简单的
string
、int
之类的。 - 如果您需要在以下时间访问该数据,则 SharedPreferences 很有意义 您下次访问您的应用程序,这意味着应该保存数据。
- 如果您希望发送您自己的自定义对象,请使用 EventBus 包含存储在其中的各种数据。
试试这个
Toast.makeText(getApplicationContext(), strName, Toast.LENGTH_LONG).show();
将name放入extras并传递给new activity。在新 activity 中从临时演员中获取名称并显示。
将此添加到您的代码中
Intent intent = new Intent(getApplicationContext(), Second_activity.class);
intent.putStringExtra("name",strName);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String strName = listitem.get(arg2).getName();
Toast.makeText(Welcome.this, strName, Toast.LENGTH_LONG)
.show();
Intent intent = new Intent(this, Second_activity.class);
intent.putExtra("string", strName);
startActivity(intent);
}
});
你的第二个activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_activity);
Intent intent = getIntent();
String name= intent.getExtras().getString("string");
Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();
}
Bundle bundle = new Bundle();
String strName = listitem.get(arg2).getOppid();
bundle.putString("msg", strName);
Intent intent =newIntent(getApplicationContext(),Second_activity.class);
intent.putExtras(bundle);
然后在secondActivity的onCreate
String value = getIntent().getExtras().getString(key);
Toast.makeText(Welcome.this, value, Toast.LENGTH_LONG).show();
修改了您的代码
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String strName = listitem.get(arg2).getOppid();
Toast.makeText(Welcome.this, strName, Toast.LENGTH_LONG)
.show();
Intent intent = new Intent(getApplicationContext(), Second_activity.class);
//add below line to take clicked item to next activity
intent.putExtra("ITEMCLICKED",strName);
startActivity(intent);
}
});
在第二个activity的onCreate方法中添加下面的代码
String mClickedItem;
//get the inten from the previous activity
Intent intent=getIntent();
//intent.hasExtra("ITEMCLICKED") to check intent has the value which we have set in previous activity
if(intent.hasExtra("ITEMCLICKED")){
mClickedItem = intent.getStringExtra("ITEMCLICKED");
}
Toast.makeText(Welcome.this, mClickedItem,Toast.LENGTH_LONG)
.show();
看看这个:
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String strName = listitem.get(arg2).getOppid();
Toast.makeText(Welcome.this, strName, Toast.LENGTH_LONG)
.show();
Intent intent = new Intent(getApplicationContext(), Second_activity.class);
intent.putExtra("name", strName);//add this line
startActivity(intent);
}
});
这是您的代码:
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String strName = listitem.get(arg2).getOppid();
Toast.makeText(Welcome.this, strName, Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(), Second_activity.class);
// you need to set the string here
startActivity(intent);
}
});
因此,您必须在第一个 activity:
上进行设置intent.putExtra("item-name", strName);
在你的第二个 Activity:
Intent intent = getIntent();
String name = intent.getExtras().getString("item-name");
Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();
此外,我建议您先检查一下您的额外费用:
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if(extras.containsKey("item-name")){
//asking for your key and do something with it
}