【问题标题】:Filling ExpendableListView with database entries用数据库条目填充 ExpendableListView
【发布时间】:2011-12-13 11:39:24
【问题描述】:

我正在使用一些示例来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 类的片段

公共类 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>

【问题讨论】:

  • 天啊...WTF?!?!伙计……你的方法很糟糕。 SQL 查询,数据解析..在 UI 线程中?!?!所以..尝试这样做。 1. 创建一个包含可扩展列表和 ExpandableListAdapter 的 Activity。 2. 创建数组或其他结构来保存列表项。 3. 创建线程,从数据库中读取和解析数据。 4. 创建接口并在activity中实现。 5. 显示进度对话框。当线程完成数据加载和解析后,将数据结构(数组、哈希等)传递给活动的接口方法。 6. 创建适配器并填充列表视图。
  • 感谢您的反馈。我是菜鸟,但我认为,这种加载数据的方式对于创建列表来说太慢了,对吧?仍然没有解释为什么我得到一个空白活动。我得到的只是这个数据库中的 5 个条目,所以这不是速度问题。当然,将来我需要重建它。这就是我学习事物的方式
  • 在调试模式下运行。将断点放在适配器内部并检查错误..

标签: android database android-listview expandablelistview


【解决方案1】:

我在 ExpendableListAdapter 类中缺少一些方法,但现在通常可以使用。但是,正如 Pepi 所说,这不是最有效的方法,因此使用风险自负。

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

【讨论】:

    猜你喜欢
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 2015-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多