我正在尝试创建一个简单的Android应用程序,从数据库中获取用户信息,我想在它们之间按行分隔这些信息。
像这样:
这是我的代码:
public String getData() {
// TODO Auto-generated method stub
String[] columns = new String[] { KEY_ID2, KEY_NAME, KEY_EMAIL};
Cursor c = ourDbase.query(TABLE_SCORE, columns, null, null, null, null, null + " DESC");
String result ="";
int iRow = c.getColumnIndex(KEY_ID);
int iName = c.getColumnIndex(KEY_NAME);
int iEmail= c.getColumnIndex(KEY_EMAIL);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + c.getString(iEmail) + " " + c.getString(iName) + " -" + c.getString(iRow)+ "\n";
}
return result;
}
答案 0 :(得分:1)
你的意思是你想要的东西:
1 Jason j@yahoo.com-2 Mark m@yahoo.com-3 Freya f@yahoo.com
您可以执行以下操作:
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + c.getString(iEmail) + " " + c.getString(iName) + " -" + c.getString(iRow)+ "\n";
result+="-"; //add "-" for each person
}
请告诉我,如果我错过了解你的问题。
<强>更新强>
从您的示例来看,您可能希望使用List
person
。如果你想要这个,试试:
public List<Person> getData() {
// TODO Auto-generated method stub
String[] columns = new String[] { KEY_ID2, KEY_NAME, KEY_EMAIL};
Cursor c = ourDbase.query(TABLE_SCORE, columns, null, null, null, null, null + " DESC");
List<Person> people = new ArrayList<Person>();
int iRow = c.getColumnIndex(KEY_ID);
int iName = c.getColumnIndex(KEY_NAME);
int iEmail= c.getColumnIndex(KEY_EMAIL);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
Person p = new Person();
p.setEmail(c.getString(iEmail));
//set other info, like id, name
people.add(person);
}
return people;
}
其中person
是新的public class
,其中包含私人id,name and email
。
<强>更新强>
public class Person {
private String name, id, email;
//create the name,id, email setter - getter or just make those variables public
}
这就是类Person
应该是这样的:
随意问你是否理解,或者我想念你。