Reputation: 385
When I click on List item then I want to open a new activity and display the item name was clicked. I tried much, but clicked item name isn't toast, a new activity open but not display item name, how I fetched clicked item name in new open activity? So how do I do that?
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);
}
});
it toast item name in same activity successfully but I want also display item name in new activity.
I want toast clicked item name in that second activity I tried this way but fail to get solve.
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();
Upvotes: 1
Views: 272
Reputation: 10953
It is your code:
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);
}
});
So, you have to set this on your first activity:
intent.putExtra("item-name", strName);
And in your second Activity:
Intent intent = getIntent();
String name = intent.getExtras().getString("item-name");
Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();
Also I recommend you check your extra before:
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if(extras.containsKey("item-name")){
//asking for your key and do something with it
}
Upvotes: 0
Reputation: 7479
You can send the string via Intent (intent.putExtra()
), or via EventBus, or put it in the SharedPreferences. Choose whichever you like.
string
, int
or something. Upvotes: 2
Reputation: 485
Take at look at this:
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);
}
});
Upvotes: 0
Reputation: 126
Bundle bundle = new Bundle();
String strName = listitem.get(arg2).getOppid();
bundle.putString("msg", strName);
Intent intent =newIntent(getApplicationContext(),Second_activity.class);
intent.putExtras(bundle);
and then in secondActivity's onCreate
String value = getIntent().getExtras().getString(key);
Toast.makeText(Welcome.this, value, Toast.LENGTH_LONG).show();
Upvotes: 0
Reputation: 1234
Modified Your Code
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);
}
});
Add Below code In Second acticity onCreate method
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();
Upvotes: 1
Reputation: 4517
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);
}
});
@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();
}
Upvotes: 0
Reputation: 684
add this to your code
Intent intent = new Intent(getApplicationContext(), Second_activity.class);
intent.putStringExtra("name",strName);
Upvotes: 0
Reputation: 79
Put name into extras and pass it to new activity. In new activity get name from extras and show.
Upvotes: 0
Reputation: 1169
Try this
Toast.makeText(getApplicationContext(), strName, Toast.LENGTH_LONG).show();
Upvotes: 0