I am having some problems with an alertdialog box. I have a listview containing some strings and when i click the box, it shows an alert dialog box giving the option to book or cancel (its a taxi app). I am trying to get it so the name of the item chosen shows in the alert dialog box. But everytime i try it comes up showing random letters and numbers. Ill post my code as it might make it easier to understand:
Code below –
public class TaxiMain extends ListActivity {
/** Called when the activity is first created.
* @return */
class Taxi {
private String taxiName;
private String taxiAddress;
public String getName() {
return taxiName;
}
public void setName(String name) {
taxiName = name;
}
public String getAddress() {
return taxiAddress;
}
public void setAddress(String address) {
taxiAddress = address;
}
public Taxi(String name, String address) {
taxiName = name;
taxiAddress = address;
}
}
public class TaxiAdapter extends ArrayAdapter<Taxi> {
private ArrayList<Taxi> items;
private TaxiViewHolder taxiHolder;
private class TaxiViewHolder {
TextView name;
TextView address;
}
public TaxiAdapter(Context context, int tvResId, ArrayList<Taxi> items) {
super(context, tvResId, items);
this.items = items;
}
@Override
public View getView(int pos, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.taxi_list_item, null);
taxiHolder = new TaxiViewHolder();
taxiHolder.name = (TextView)v.findViewById(R.id.taxi_name);
taxiHolder.address = (TextView)v.findViewById(R.id.taxi_address);
v.setTag(taxiHolder);
} else taxiHolder = (TaxiViewHolder)v.getTag();
Taxi taxi = items.get(pos);
if (taxi != null) {
taxiHolder.name.setText(taxi.getName());
taxiHolder.address.setText(taxi.getAddress());
}
return v;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String[] taxiNames = getResources().getStringArray(R.array.taxi_name_array);
final String[] taxiAddresses = getResources().getStringArray(R.array.taxi_address_array);
ArrayList<Taxi> taxiList = new ArrayList<Taxi>();
for (int i = 0; i < taxiNames.length; i++) {
taxiList.add(new Taxi(taxiNames[i], taxiAddresses[i]));
}
setListAdapter(new TaxiAdapter(this, R.layout.taxi_list_item, taxiList));
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
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(TaxiMain.this);
adb.setTitle("Taxi Booking");
adb.setMessage("You Have Selected: "+taxiNames);
adb.setPositiveButton("Book", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(TaxiMain.this, Booking.class);
intent.putExtra("booking", taxiNames[selectedPosition]);
intent.putExtra("address", taxiAddresses[selectedPosition]);
startActivity(intent);
}
});
adb.setNegativeButton("Cancel", null);
adb.show();
}
});
You’ll see at the bottom of the code, theres the line that wont work properly. –
adb.setMessage("You Have Selected: "+taxiNames);
If anyone can help into seeing why this wont show would be of great help.
Thanks
When you type
+taxiNamesyou’re performingtaxiNames.toString().taxiNamesis an array containing a number of items. You need to just change it to+ taxiNames[position]. Or, to keep it in line with your other Taxi objects, you could also use+ taxiList.get(position).getName().EDIT: Out of curiosity, why are you setting another
final intforselectedPosition? You already have thefinal int positionpassed in on the method call.