我已将名字和朋友存储在数据库中。
例如,
person A,and his friends B,C,D
person Z,and his friends K,L,M
person Q,and his friends P,O,N
如何从数据库中检索值并在可展开的列表视图中显示。
我,e.Person A,Z,Q应该是群组名称,相应的朋友应该是孩子的名字....
提前致谢
答案 0 :(得分:1)
为您的人创建包装
public class Person {
String name;
String[] friends;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getFriends() {
return friends;
}
public void setFriends(String[] friends) {
this.friends = friends;
}
}
然后将人名输入您的ParentView 和BaseExpandableListAdapter中的一个孩子的朋友
public class ExpandableListAdapter extends BaseExpandableListAdapter {
List<Person>persons; // fill persons
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
persons.get(groupPosition).getName();
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
persons.get(groupPosition).getFriends();
}
答案 1 :(得分:1)
1&GT;创建一个bean,让我们说PersonDetails具有nameOfThePerson属性和frnds的数组。
public class PersonDetails {
private String nameOfThePerson;
private String[] frnds;
public String nameOfThePerson() {
return nameOfThePerson;
}
public void setName(String nameOfThePerson) {
this.nameOfThePerson= nameOfThePerson;
}
public String[] getFrnds() {
return frnds;
}
public void setFrnds(String[] frnds) {
this.frnds= frnds;
}
}
2 - ;使用sqlite查询从db获取详细信息,并将这些bean存储在arraylist中。
public ArrayList<PersonDetails> getPersonDetails(Context context) {
ArrayList<PersonDetails> lPersonDetailList = new ArrayList<PersonDetails>();
PersonDetails lPersonDetails;
try {
dbName = context.openOrCreateDatabase(Constant.databaseName, Context.MODE_WORLD_WRITEABLE, null);
String query = "SELECT * FROM "NAME OF UR TABLE";
Cursor cursor = dbName.rawQuery(query, null);
cursor.moveToFirst();
if (cursor != null) {
if (cursor.isFirst()) {
do {
lPersonDetails= new PersonDetails();
/*use gettersetters to feed in details to the lPersonDetails object.*/
lPersonDetailList .add(lPersonDetails);
} while (cursor.moveToNext());
}
}
cursor.close();
dbName.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
return lPersonDetailList ;
}
3&GT;将它放在可扩展列表视图中。
public class ExpandableListAdapter extends BaseExpandableListAdapter {
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// get the name of the person from the lPersonDetails object in arraylist position wise
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// get the name of the frnds from the lPersonDetails object in arraylist position wise from the string array
}