2012-11-01 120 views
0

我已將名稱和朋友存儲在數據庫中。如何在ExpandableListView中顯示SQLite查詢的詳細信息?

例如,

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應是一個組的名稱和相應的朋友應該是孩子名字....

在此先感謝

回答

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>創建一個bean可以讓PersonDetails擁有屬性nameOfThePerson和數組。

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 
}