我正在尝试使用mergeAdapter在视图寻呼机中创建列表视图。基本上我想要的是从我的数据库中获取一些日期并在一个组中保留相同的日期(带有某种标题) 。
所以我创建了一个游标的Arraylist,它将存储与唯一日期相对应的数据,然后尝试使用合并适配器合并它们。
但是列表没有正确创建。相反,它显示两个日期之间的差距,并且布局中没有内容。只是标题和空白。而且日期不会被分组到唯一的日期。
dbObj是我的数据库对象,其中包含表。
Databases dbObj; // my database class
SQLiteDatabase ourDatabase;
ArrayList<SimpleCursorAdapter> alladapters;
ArrayList<Cursor> incomeCursor;
MergeAdapter listAdapter;
这里的视图在ViewPager中膨胀。
public Object instantiateItem(View collection, int position)
{
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = null;
switch (position)
{
case 0:
ListView daily = new ListView(getActivity()
.getApplicationContext());
dates.clear();
alladapters.clear();
listAdapter.notifyDataSetInvalidated();
getDates();
buildAdapters();
daily.setCacheColorHint(0);
dbObj.open();
ourDatabase = dbObj.getDatabase();
setMergeAdapter();
daily.setAdapter(listAdapter);
view = daily;
((ViewPager) collection).addView(view, 0);
dbObj.close();
break;
}
return view;
这些是我的getdates和buildadapters方法
private void getDates() throws SQLException
{
dbObj.open();
ourDatabase = dbObj.getDatabase();
String[] columns = { Databases.KEY_ROWID, Databases.KEY_DATE };
Cursor c = ourDatabase.query(true, Databases.DATABASE_TABLE_INCOME,
columns, null, null, null, null, Databases.KEY_DATE, null);
int i = 0;
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
{
dates.add(c.getString(c.getColumnIndex(Databases.KEY_DATE)));
i++;
}
dbObj.close();
}
private void buildAdapters() throws SQLException
{
dbObj.open();
ourDatabase = dbObj.getDatabase();
Cursor c;
String[] columns = { Databases.KEY_ROWID, Databases.KEY_DATE,
Databases.KEY_AMOUNT, Databases.KEY_CATEGORY,
Databases.KEY_DESCRIPTION };
// Log.d("Size of Date ", msg)
for (int i = 0; i < dates.size(); i++)
{
incomeCursor.add(ourDatabase.query(Databases.DATABASE_TABLE_INCOME,
columns, Databases.KEY_DATE + "=" + dates.get(i), null,
null, null, null));
alladapters.add(new SimpleCursorAdapter(getActivity()
.getApplicationContext(), R.layout.listlayout, incomeCursor
.get(i), new String[] { Databases.KEY_AMOUNT,
Databases.KEY_CATEGORY, Databases.KEY_DESCRIPTION },
new int[] { R.id.ListAmount, R.id.ListCategory,
R.id.ListDescript },
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER));
}
dbObj.close();
}
setmerger适配器方法
private void setMergeAdapter()
{
// TODO Auto-generated method stub
TextView tv = new TextView(getActivity().getApplicationContext());
for (int i = 0; i < alladapters.size(); i++)
{
tv.setText(dates.get(i));
listAdapter.addView(tv);
listAdapter.addAdapter(alladapters.get(i));
}
}