Android - Displaying ListView item in a textview
I have managed to setup a page so that a user clicks an item in a listview which then shows an alertdialog box and gives them the option to book or cancel. If they click book it takes them to another screen. What i need is to show the item selected on the next screen. I almost have it but i cant work out where its going wrong. What i was hoping is if someone could help in working out the best way to do this?
My code at present is -
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, final int position, long id)
{
final int selectedPosition = position;
AlertDialog.Builder adb=new AlertDialog.Builder(ListTaxi.this);
adb.setTitle("Taxi Booking");
adb.setMessage("You have chosen = "+lv.getItemAtPosition(position));
adb.setPositiveButton("Book", new DialogInterface.OnClickListener() {
public开发者_JS百科 void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(getApplicationContext(), Booking.class);
intent.putExtra("booking", taxi[selectedPosition]);
startActivity(intent);
}
});
adb.setNegativeButton("Cancel", null);
adb.show();
}
});
And on the booking.java is:
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.booking); TextView tv_taxi = (TextView) findViewById(R.id.tvtaxi); //tv_taxi.setText(taxi);
Intent intent = getIntent();
String booking = "";
if (intent != null) {
Bundle extras = intent.getExtras();
if (extras != null) {
booking = extras.getString("booking");
}
}
}
Original article is at - Android - Change View when alertdialog button is clicked
Thanks Everyone
You just need to set the text on your TextView
to be the value you retrieved from the Bundle
:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.booking);
TextView tv_taxi = (TextView) findViewById(R.id.tvtaxi);
Intent intent = getIntent();
String booking = "";
if (intent != null) {
Bundle extras = intent.getExtras();
if (extras != null) {
booking = extras.getString("booking");
tv_taxi.setText(booking);
}
}
}
精彩评论