0

I have a list view of Hotels and I want when the user clicks a long click on an item a dialog appears with the information of the clicked hotel retrieved from the SQLite database (Location, Address, Phone number...etc.). I retrieve data from the database in a List. In the dialog message when I convert the list toString() the result has extra square brackets, example: Phone: [02239348], what should I do? Is there another way?

Here is the code from DataSource.java which retrieves the location from database:

 public List<Hotel> getHotelLocation(String hotelName) {
    List<Hotel> hotels = new ArrayList<Hotel>(); 
    Cursor cursor = database.query(MySQLiteHelper.TABLE_Hotel, 
            hotelsLoc, hotelsNames[0] + " like " + "'" + hotelName + "'", null, null, null, null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        Hotel hotel = cursorToHotel(cursor);
        hotels.add(hotel);
        cursor.moveToNext();
    }
    // Make sure to close the cursor
    cursor.close();
    return hotels;
}

private Hotel cursorToHotel(Cursor cursor) {
    // TODO Auto-generated method stub
    Hotel hotelname = new Hotel();
        hotelname.setName(cursor.getString(0));
    return hotelname;
}

Here is the alert dialog:

public boolean onItemLongClick(AdapterView<?> parent, View view, int position,
        long id) {
    // TODO Auto-generated method stub
    String hotelName = (String) ((TextView)parent.getChildAt(position)).getText();
    List<Hotel> location = datasource.getHotelLocation(hotelName);
    String loc = "Location: " + location.toString();
    List<Hotel> address = datasource.getHotelAddress(hotelName);
    String add = "Address: " + address.toString();
    List<Hotel> rating = datasource.getHotelRating(hotelName);
    String rat = "Rating: " + rating.toString();
    List<Hotel> phone = datasource.getHotelPhone(hotelName);
    String phoneN = "Phone: " + phone.toString();
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Information about " + hotelName);
    builder.setMessage(loc + "\n" + add + "\n" + rat + "\n" + phoneN + "\n");
            builder.setCancelable(true);
    builder.show();
    return false;
}
3
  • Can you please modify on my code? I don't know which parts to modify Commented Apr 24, 2012 at 20:42
  • Can you please post the source for your Hotel class? I'd like to see what the getHotelPhone method does. Commented Apr 24, 2012 at 20:44
  • public List<Hotel> getHotelPhone(String hotelName) { List<Hotel> hotels = new ArrayList<Hotel>(); Cursor cursor = database.query(MySQLiteHelper.TABLE_Hotel, hotelsPhone, hotelsNames[0] + " like " + "'" + hotelName + "'", null, null, null, null); cursor.moveToFirst(); while (!cursor.isAfterLast()) { Hotel hotel = cursorToHotel(cursor); hotels.add(hotel); cursor.moveToNext(); } // Make sure to close the cursor cursor.close(); return hotels; } Commented Apr 24, 2012 at 20:45

2 Answers 2

1

You can either join the lists into a String with TextUtils.join(", ", theList) or you can simply use the first item of your list (theList.get(0)) if that is enough.

List<Hotel> phone = datasource.getHotelPhone(hotelName);
String phoneN = "Phone: " + TextUtils.join(", ", phone);

if phone has multiple entries it should print "Phone: 12343, 3424323, 19393".

Sign up to request clarification or add additional context in comments.

Comments

0

You are using the toString() method for a List and the default implementation for that List probably uses the "[" elements "]".

Do you want to print the first element of the List? Just override the toString() at your Hotel class them you can use

phone.get(0).toString()

Regards

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.