gpt4 book ai didi

android从数据库中分离表格的行

转载 作者:搜寻专家 更新时间:2023-10-30 23:47:25 24 4
gpt4 key购买 nike

我正在尝试制作一个简单的 android 应用程序,它从数据库中获取用户信息,我想在它们之间用行分隔这些信息。

像这样:

enter image description here

这是我的代码:

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

最佳答案

你是说你想要这样的东西吗:

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
}

如果我误解了你的问题,请告诉我。

更新

从您的示例来看,您可能想要使用 personList。如果你想要这个,试试:

  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,具有私有(private) id,name 和 email

更新

public class Person {
private String name, id, email;
//create the name,id, email setter - getter or just make those variables public
}

Person 类应该是这样的:

如果您不明白或我想念您,请随时询问。

关于android从数据库中分离表格的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26715262/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com