ExpendableList에 약간의 샘플을 사용하고 있습니다. 프로젝트의 끝에 막혔습니다.데이터베이스 항목이있는 ExpendableListView 채우기
아무도 읽을 수 없으므로 전체 코드를 붙여 넣지는 않겠습니다. 뭔가 잘못 될 수있는 부분이 있습니다.
어댑터에라고 생각합니다. ListViev로 설정할 때 블랭크 페이지가 나타납니다. 아무 예외도 없다, 모두는 작동하고있는 것처럼 보인다. 활동이 시작되지만 목록이 비어 있습니다.
adapter = new ExpandableListAdapter(this, new ArrayList<String>(),
new ArrayList<ArrayList<Expense>>());
//database cursor
Cursor cursor = db.SQLDb.rawQuery("SELECT "+db.EXPENSE_CATEGORY+","+db.EXPENSE_DATE+
", SUM("+db.EXPENSE_VALUE+") 'SUM' , STRFTIME('%m', "+db.EXPENSE_DATE+") \"MONTH\", STRFTIME('%Y', "+db.EXPENSE_DATE+
") \"YEAR\" FROM "+db.TABLE_EXPENSES+ " GROUP BY "+db.EXPENSE_CATEGORY+ ", MONTH, YEAR ORDER BY MONTH DESC, YEAR DESC" , null);
//geting values from table
if (cursor.moveToFirst()) {
do{
String category = cursor.getString(cursor.getColumnIndex(db.EXPENSE_CATEGORY));
double sum = cursor.getFloat(cursor.getColumnIndex("SUM"));
String date = cursor.getString(cursor.getColumnIndex(db.EXPENSE_DATE));
String month = cursor.getString(cursor.getColumnIndex("MONTH")) + " " +cursor.getString(cursor.getColumnIndex("YEAR")); ;
//adding new object to adapter
adapter.addExpense(new Expense(category, sum, date, month));
}while (cursor.moveToNext());
}
//seting the adapter for ListView
listView.setAdapter(adapter);
조각 ExpendalbeListAdapter 클래스의 내 활동 클래스의
조각이
공용 클래스 ExpandableListAdapter이 BaseExpandableListAdapter를 확장 {
private Context context;
private ArrayList<String> groups;
private ArrayList<ArrayList<Expense>> children;
public ExpandableListAdapter(Context context, ArrayList<String> groups, ArrayList<ArrayList<Expense>> children) {
this.context = context;
this.groups = groups;
this.children = children;
}
public void addExpense(Expense expense) {
if (!groups.contains(expense.getMonth())) {
groups.add(expense.getMonth());
}
int index = groups.indexOf(expense.getMonth());
if (children.size() < index + 1) {
children.add(new ArrayList<Expense>());
}
children.get(index).add(expense);
}
// Return a child view. You can load your custom layout here.
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
Expense expense = (Expense) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.summary_child, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.tvChild);
tv.setText(" " + expense.getCategory());
return convertView;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String group = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.summary_group, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.tvGroup);
tv.setText(group);
return convertView;
}
비용 클래스
public class Expense {
private String category;
private double value;
private String date;
private String month;
public Expense(String category, double value, String date, String month){
this.category = category;
this.value = value;
this.date = date;
this.month = month;
}
}
레이아웃
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ExpandableListView android:id="@+id/listView"
android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="none"></ExpandableListView>
</LinearLayout>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="45dip"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView android:id="@+id/ImageView01" android:src="@drawable/icon"
android:layout_height="40dip" android:layout_width="40dip" android:layout_marginLeft="40dip"></ImageView>
<TextView android:id="@+id/tvGroup" android:layout_width="fill_parent"
android:layout_height="45dip" android:text="Groups" android:gravity="center_vertical|right"
android:paddingLeft="5dip" android:paddingRight="5dip"
android:textColor="#ffffffff" android:textStyle="bold"
android:textSize="17dip"></TextView>
</LinearLayout>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="45dip"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView android:id="@+id/ImageView01" android:src="@drawable/icon"
android:layout_height="40dip" android:layout_width="40dip" android:layout_marginLeft="40dip"></ImageView>
<TextView android:layout_width="fill_parent"
android:layout_height="45dip" android:paddingLeft="5dip"
android:paddingRight="5dip" android:textStyle="bold" android:textSize="17dip"
android:gravity="center_vertical" android:id="@+id/tvChild"
android:text="Children" android:textColor="#ffCCCC22"></TextView>
</LinearLayout>
OMG를 따라?!?! 남자 ... 너의 접근 방식이 짜증나. SQL 쿼리, 데이터 구문 분석 .. UI 스레드?!?! 그러니이 일을하십시오. 1. 확장 가능한 목록과 ExpandableListAdapter를 보유하는 활동을 만듭니다. 2. 목록 항목을 포함 할 배열 또는 다른 구조를 만듭니다. 3. 데이터베이스에서 데이터를 읽고 구문 분석하는 스레드를 만듭니다. 4. 인터페이스를 작성하고 액티비티에 구현하십시오. 5. 진행률 표시 대화 상자. 데이터로드 및 구문 분석으로 스레드가 완료되면 데이터 구조 (배열, 해시 등)를 활동의 인터페이스 메소드에 전달하십시오. 6. 어댑터를 작성하고 목록보기를 채우십시오. –
fedback에 감사드립니다. 나는 신참이지만, 데이터를로드하는 이런 방식으로 목록을 만드는 것이 너무 느립니다. 아직도 빈 활동을하는 이유는 설명하지 못합니다. 내가 얻은 것은이 데이터베이스에 5 개의 항목이 있기 때문에 속도 문제 일 수는 없습니다. 물론, 미래에 나는 이것을 재건 할 필요가있을 것이다. 이것은 내가 배운 방식입니다 –
디버그 모드로 실행하십시오. 어댑터 내부에 중단 점을 넣고 잘못된 점을 확인하십시오. –