使用数据库条目填充ExpendableListView

时间:2011-12-13 11:39:24

标签: android database android-listview expandablelistview

我正在使用一些示例 ExpendableList ,我被困在项目的最后。

我不会粘贴整个代码,因为没有人会读到它。有些部分可能有问题。

我认为适配器有问题。当我为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类的片段

public class 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>

1 个答案:

答案 0 :(得分:0)

我在ExpendableListAdapter类中缺少一些方法,但通常现在可以正常工作。然而,正如佩皮所说,这不是最有效的方式,所以请自担风险。

这是我遵循的例子 http://techdroid.kbeanie.com/2010/09/expandablelistview-on-android.html