我想将ArrayList转换为逗号分隔的String,这是我的代码。我在本地数据库中保存纬度,经度和日期时间并单独获取它们。
添加到本地数据库:
db = new DatabaseHandler(context);
// db.open();
/**
* CRUD Operations
* */
// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
db.addLocation(new Location2(latitude, longitude, city + "," + state, strDate));
db.close();
并单独获取:
public List<String> getLattitude(){
List<String> labels = new ArrayList<String>();
// Select All Query
String selectQuery = " SELECT " + LATTITUDE + " FROM "+ TABLE_CONTACTS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
labels.add(cursor.getString(0));
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db.close();
// returning lables
return labels;
}
public List<String> getLongitude(){
List<String> labels = new ArrayList<String>();
// Select All Query
String selectQuery = " SELECT " + LONGITUDE + " FROM "+ TABLE_CONTACTS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
labels.add(cursor.getString(0));
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db.close();
// returning lables
return labels;
}
public List<String> getAllDate(){
List<String> labels = new ArrayList<String>();
// Select All Query
String selectQuery = " SELECT " + DATE + " FROM "+ TABLE_CONTACTS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
labels.add(cursor.getString(0));
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db.close();
// returning lables
return labels;
}
从本地db获取数据:
List<String> data = new ArrayList<>();
List<String> data2 = new ArrayList<>();
List<String> data3 = new ArrayList<String>();
String lattitute5 = String.valueOf(db.getAllLabels());
Log.d("database lattitude",lattitute5);
data.add(lattitute5);
Log.d("database to list lattitude", lattitute5);
String longitude5 = String.valueOf(db.getAllLabels2());
Log.d("database to list Longitude", longitude5);
data2.add(longitude5);
String Date3 = String.valueOf(db.getAllLabels3());
Log.d("database to list date",Date3);
data3.add(Date3);
Latti = TextUtils.join(",", data);
Log.d("lattitu", Latti);
Longi = TextUtils.join(",", data2);
Log.d("longiii", Longi);
Date = TextUtils.join(",", data3);
Log.d("dattttt", Date);
logcat的:
database to list lattitude﹕ [23.1069556, 23.1069556]
database to list Longitude﹕ [72.605684, 72.605684]
database to list date﹕ [20150810233842, 20150810233846]
longiii﹕ [72.605684],[72.605684, 72.605684]
lattitu﹕ [23.1069556],[23.1069556, 23.1069556]
dattttt﹕ [20150810233842],[20150810233842, 20150810233846]
我想要类似的东西:
longiii : 72.605684,72.605684,72.605684
lattitu : 23.1069556,23.1069556,23.1069556
dattttt : 20150810233842,20150810233842,20150810233846
我该怎么做?
答案 0 :(得分:2)
这两种方法很有用
public static String listToString(List<String> list)
{
return TextUtils.join(",", list);
}
public static List<String> stringToList(String string)
{
if(string==null)
return new ArrayList<String>();
else
return new ArrayList<String>(Arrays.asList(TextUtils.split(string, ",")));
}
答案 1 :(得分:1)
首先将ArrayList
转换为数组:String myNewArr[] = mylist.toArray();
然后你可以通过简单的for循环加入它们:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < myNewArr.length; i++)
sb.append(myNewArr[i]);
String str = sb.toString();
或者您可以使用Apache Commons StringUtils
class加入:
String str = StringUtils.join(myNewArr, ',');
答案 2 :(得分:1)
试试这个......
public String getLattitude(){
String labels = "";
// Select All Query
String selectQuery = " SELECT " + LATTITUDE + " FROM "+ TABLE_CONTACTS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.getCount()>0) {
cursor.moveToFirst();
for(int i=0;i<cursor.getCount();i++){
labels+=cursor.getString(0);
if(i<(cursor.getCount()-1)){
labels+=",";
}
cursor.moveToNext();
}
}
// closing connection
cursor.close();
db.close();
// returning lables
return labels;
}