Android 用数据库条目填充ExpandableListView
我正在使用一些示例来创建消耗品列表,我被困在项目的最后 我不会粘贴整个代码,因为没有人会读它。有些零件可能有问题 我想适配器有点问题。当我把它设置为ListViev时,我得到了一个乏味的页面。没有例外,一切似乎都在运转。活动开始,但列表为空 我的活动课片段 布局Android 用数据库条目填充ExpandableListView,android,database,android-listview,expandablelistview,Android,Database,Android Listview,Expandablelistview,我正在使用一些示例来创建消耗品列表,我被困在项目的最后 我不会粘贴整个代码,因为没有人会读它。有些零件可能有问题 我想适配器有点问题。当我把它设置为ListViev时,我得到了一个乏味的页面。没有例外,一切似乎都在运转。活动开始,但列表为空 我的活动课片段 布局 我在ExpandableListAdapter类中丢失了一些方法,但通常它现在可以工作了。然而,正如Pepi所说,这并不是最有效的方法,所以使用它要自担风险 这是我遵循的例子 天哪……WTF?!?!老兄,你的方法太差劲了。SQL查询,数
我在ExpandableListAdapter类中丢失了一些方法,但通常它现在可以工作了。然而,正如Pepi所说,这并不是最有效的方法,所以使用它要自担风险 这是我遵循的例子
天哪……WTF?!?!老兄,你的方法太差劲了。SQL查询,数据解析..在UI线程中?!?!所以…试着这样做。1.创建一个包含可展开列表和可展开列表适配器的活动。2.创建数组或其他一些结构来保存列表项。3.创建从数据库读取和解析数据的线程。4.创建接口并在活动中实现它。5.显示进度对话框。当线程完成数据加载和解析后,将数据结构数组、哈希等传递给活动的接口方法。6.创建适配器并填充列表视图。感谢您的反馈。我是新手,但我想,这种加载数据的方式对于创建列表来说太慢了,对吧?但这并不能解释为什么我会得到一个空白的活动。我在这个数据库里只找到了5个条目,所以这不是速度问题。当然,将来我需要重建这个。这是我学习在调试模式下运行的方式。将断点放在适配器内部,并检查出了什么问题。。
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);
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>