我的应用程序中有一个 sqlite 数据库。我想用它制作一个可扩展的列表视图。
我已经确定了我应该采取的方法。
尝试了很多方法来找到相同的教程,但找不到一个使用本地数据库填充可扩展列表的教程。
Android 网站上有一个教程,他们使用手机中的联系人详细信息填充可扩展列表。他们这样做是从内容提供商
ExpandableList2.java http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList2.html
所以我的问题是我是否还应该为我自己的应用程序内部的数据库创建一个内容提供程序?
这是唯一的方法还是还有其他更好的方法?
我创建了一个项目来尝试可扩展列表视图和数据库,希望这有帮助
final class ExpAdapter extends CursorTreeAdapter {
LayoutInflater mInflator;
public ExpAdapter(Cursor cursor, Context context) {
super(cursor, context);
mInflator = LayoutInflater.from(context);
}
@Override
protected void bindChildView(View view, Context context, Cursor cursor,
boolean isLastChild) {
TextView tvChild = (TextView) view.findViewById(android.R.id.text1);
tvChild.setText(cursor.getString(cursor
.getColumnIndex(DBHelper.COL_TABLE_CHILD_NAME)));
}
@Override
protected void bindGroupView(View view, Context context, Cursor cursor,
boolean isExpanded) {
TextView tvGrp = (TextView) view.findViewById(android.R.id.text1);
tvGrp.setText(cursor.getString(cursor
.getColumnIndex(DBHelper.COL_TABLE_MAIN_NAME)));
}
@Override
protected Cursor getChildrenCursor(Cursor groupCursor) {
int groupId = groupCursor.getInt(groupCursor
.getColumnIndex(DBHelper.COL_ID));
return aDBHelper.getChildCursor(groupId);
}
@Override
protected View newChildView(Context context, Cursor cursor,
boolean isLastChild, ViewGroup parent) {
View mView = mInflator.inflate(
android.R.layout.simple_expandable_list_item_1, null);
TextView tvChild = (TextView) mView
.findViewById(android.R.id.text1);
tvChild.setText(cursor.getString(cursor
.getColumnIndex(DBHelper.COL_TABLE_CHILD_NAME)));
return mView;
}
@Override
protected View newGroupView(Context context, Cursor cursor,
boolean isExpanded, ViewGroup parent) {
View mView = mInflator.inflate(
android.R.layout.simple_expandable_list_item_1, null);
TextView tvGrp = (TextView) mView.findViewById(android.R.id.text1);
tvGrp.setText(cursor.getString(cursor
.getColumnIndex(DBHelper.COL_TABLE_MAIN_NAME)));
return mView;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)