0
我正在使用一些示例到ExpendableList,我被困在項目的最後。用數據庫條目填充ExpendableListView
我不會粘貼整個代碼,因爲沒有人會閱讀它。有些部分可能會出現問題。
適配器我認爲有問題。當我爲ListViev設置它時,我得到一個blan頁面。沒有例外,一切似乎都在起作用。活動開始,但列表爲空。
片段我的活動類
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 ...... WTF?!?!人......你的方法很糟糕。 SQL查詢,數據解析..在UI線程?!?!所以..要做到這一點。 1.創建一個包含可展開列表和ExpandableListAdapter的Activity。 2.創建數組或其他一些結構來保存列表項。 3.創建從數據庫中讀取和解析數據的線程。 4.創建界面並在活動中實現它。 5.顯示進度對話框。當線程完成數據加載和解析時,將數據結構(數組,散列等)傳遞給活動的接口方法。 6.創建適配器並填充列表視圖。 –
感謝反饋。我是一名新手,但我認爲,這種加載數據的方式對於創建列表來說太慢了嗎?儘管如此,這並不能解釋爲什麼我會得到空白的活動。我得到的僅僅是這個數據庫中的5個條目,所以它不是速度問題。當然,將來我需要重建這個。這是我學習東西的方式 –
以調試模式運行。在適配器內部放置斷點並檢查是否有問題。 –