2012-04-24 54 views
0

我有一個列表視圖的酒店,我想當用戶點擊一個項目長按一個對話框出現與點擊酒店信息從SQLite數據庫檢索(位置,地址,電話數...等)。 我從列表中的數據庫檢索數據。 在將列表轉換爲toString()時的對話框消息中,結果有多餘的方括號,例如:Phone:[02239348],我應該怎麼做?有另一種方法嗎?與SQLite數據庫中的數據對話框

下面是從DataSource.java代碼來檢索數據庫,從位置:

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; 
} 

這裏是警告對話框:

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; 
} 
+0

能否請您修改我的密碼?我不知道要修改哪些部分 – MahaK 2012-04-24 20:42:51

+0

您可以發佈酒店類的來源嗎?我想看看getHotelPhone方法的功能。 – twaddington 2012-04-24 20:44:31

+0

public列表 getHotelPhone(String hotelName){ \t \t列表 hotels = new ArrayList (); \t \t光標光標= database.query(MySQLiteHelper.TABLE_Hotel, \t \t \t \t hotelsPhone,hotelsNames [0] + 「喜歡」 + 「 '」 + hotelName + 「'」,NULL,NULL,NULL,NULL); \t \t cursor.moveToFirst(); \t \t而{ \t \t \t酒店賓館= cursorToHotel(光標)(cursor.isAfterLast()!); \t \t \t hotels.add(hotel); \t \t \t cursor.moveToNext(); \t \t} \t \t //確保關閉遊標 \t \t cursor.close(); \t \t返回酒店; \t} – MahaK 2012-04-24 20:45:48

回答

1

您可以加入列表與TextUtils.join(", ", theList)一個String或者只需使用列表中的第一項(theList.get(0))即可。

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

如果手機有多個條目,它應該打印"Phone: 12343, 3424323, 19393"

+0

非常感謝!它工作得很好:) – MahaK 2012-04-24 20:49:55

0

對List使用toString()方法,該List的默認實現可能使用「[」elements「]」。

是否要打印列表的第一個元素?只是覆蓋在你的酒店類的toString()他們,你可以使用

phone.get(0).toString() 

問候