0

我已将姓名和朋友存储在数据库中。

例如 ,

person A,and his friends B,C,D 
person Z,and his friends K,L,M 
person Q,and his friends P,O,N 

如何从数据库中检索值并显示在可扩展的列表视图中。

即人 A,Z,Q 应该是组名,对应的朋友应该是子名 ....

提前致谢

4

2 回答 2

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 中的 Child

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();
}
于 2012-11-01T07:54:12.223 回答
1

1> 创建一个 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 查询从数据库中获取详细信息并将这些 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> 将其放入可展开的列表视图中。

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
}
于 2012-11-01T08:27:14.383 回答