2012-11-01 3 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; 
    } 

} 

그런 다음 BaseExpandableListAdapter의 아동에 ParentView 과 친구에 사람 이름을 넣어

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> 빈을 작성하여 nameOfThePerson 및 frnds에 대한 속성을 갖는 PersonDetails를 말할 수 있습니다.

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> 확장형 목록보기에 추가하십시오.

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 
} 
관련 문제