Я использую какой-то пример для 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 extends 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>