对话与SQLite数据库的数据 [英] Dialog with data from SQLite database

查看:122
本文介绍了对话与SQLite数据库的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的酒店列表视图,当用户点击会出现一个对话框,从SQLite数据库(地点,地址,电话号码...等获取的点击酒店的信息项长时间点击我想要的。 )。
我从列表中的数据库中检索数据。
在该对话框中的消息时,我转换列表中的toString()的结果有多余的方括号,例如:电话:[02239348],我该怎么办?有另一种方式?

下面是DataSource.java的code从数据库中检索位置:

 公开名单<酒店及GT; getHotelLocation(字符串hotelName){
    清单<酒店及GT;酒店=新的ArrayList<酒店及GT;();
    光标光标= database.query(MySQLiteHelper.TABLE_Hotel,
            hotelsLoc,hotelsNames [0] +像+'+ hotelName +',NULL,NULL,NULL,NULL);
    cursor.moveToFirst();
    而(!cursor.isAfterLast()){
        酒店Hotel = cursorToHotel(光标);
        hotels.add(酒店);
        cursor.moveToNext();
    }
    //确保关闭游标
    cursor.close();
    返回酒店;
}私人酒店cursorToHotel(光标光标){
    // TODO自动生成方法存根
    酒店hotelname =新酒店();
        hotelname.setName(cursor.getString(0));
    返回hotelname;
}

下面是警告对话框:

 公共布尔onItemLongClick(适配器视图<>母公司,观景,INT位置,
        长ID){
    // TODO自动生成方法存根
    字符串hotelName =(字符串)((的TextView)parent.getChildAt(位置))的getText()。
    清单<酒店及GT;位置= datasource.getHotelLocation(hotelName);
    串LOC =位置:+ location.toString();
    清单<酒店及GT;地址= datasource.getHotelAddress(hotelName);
    字符串添加=地址:+ address.toString();
    清单<酒店及GT;等级= datasource.getHotelRating(hotelName);
    字符串鼠=评价:+ rating.toString();
    清单<酒店及GT;手机= datasource.getHotelPhone(hotelName);
    字符串phoneN =电话:+ phone.toString();
    AlertDialog.Builder建设者=新AlertDialog.Builder(本);
    builder.setTitle(信息约+ hotelName);
    builder.setMessage(LOC +\\ n+加+\\ n+鼠+\\ n+ phoneN +\\ n);
            builder.setCancelable(真);
    builder.show();
    返回false;
}


解决方案

您可以加入列表成为一个字符串 TextUtils.join(,的thelist)或者你可以简单地使用列表的第一项( theList.get(0)),如果这是不够的。

 列表<酒店及GT;手机= datasource.getHotelPhone(hotelName);
字符串phoneN =电话:+ TextUtils.join(,,电话);

如果手机有多个条目,它应该打印电话:12343,3424323,19393。

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

解决方案

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".

这篇关于对话与SQLite数据库的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆