Android ExpandableListActivity和SimpleCursorTreeAdapter?

Android ExpandableListActivity和SimpleCursorTreeAdapter?,第1张

概述我正在为 Android编写一个简单的应用程序. 我有2个表 – 一个叫做“grous”,另一个叫做“group_items”. 我想使用可扩展列表来显示两个表中的数据. 最好的方法是什么?是否可以使用SimpleCursorTreeAdapter映射数据?我找不到任何有用的例子. 我看到示例使用ArrayAdapter创建可扩展列表,所以我应该先将数据转换为数组,然后创建一个可扩展列表,还是直接 我正在为 Android编写一个简单的应用程序.

我有2个表 – 一个叫做“grous”,另一个叫做“group_items”.

我想使用可扩展列表来显示两个表中的数据.

最好的方法是什么?是否可以使用SimpleCursorTreeAdapter映射数据?我找不到任何有用的例子.

我看到示例使用ArrayAdapter创建可扩展列表,所以我应该先将数据转换为数组,然后创建一个可扩展列表,还是直接执行?

我不需要一个完整的工作示例 – 只是一个建议,什么是正确和最有效的方式来做到这一点.

Leonti

解决方法@H_419_17@ 我发现最简单的解决方案是使用SimpleCursorTreeAdapter.
这里是代码示例(重要部分):
public class ExercisesList extends ExpandableListActivity {private ExcercisesDbAdapter mDbHelper; // your db adapterprivate Cursor mGroupsCursor; // cursor for List of groups (List top nodes)private int mGroupIDColumnIndex;private Myexpandablelistadapter mAdapter;@OverrIDepublic voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        mDbHelper = new ExcercisesDbAdapter(this);        mDbHelper.open();        fillData();}private voID fillData() {        mGroupsCursor = mDbHelper.fetchAllGroups(); // fills cursor with List of your top nodes - groups         startManagingCursor(mGroupsCursor);        // Cache the ID column index        mGroupIDColumnIndex = mGroupsCursor                        .getColumnIndexOrThrow(ExcercisesDbAdapter.KEY_ROWID);        // Set up our adapter        mAdapter = new Myexpandablelistadapter(mGroupsCursor,this,androID.R.layout.simple_expandable_List_item_1,R.layout.exercise_List_row,new String[] { ExcercisesDbAdapter.KEY_Title },// group Title for group layouts                        new int[] { androID.R.ID.text1 },// exercise Title for child layouts                        new int[] { R.ID.exercise_Title });        setlistadapter(mAdapter);}// extending SimpleCursorTreeAdapterpublic class Myexpandablelistadapter extends SimpleCursorTreeAdapter {        public Myexpandablelistadapter(Cursor cursor,Context context,int groupLayout,int childLayout,String[] groupFrom,int[] groupTo,String[] childrenFrom,int[] childrenTo) {                super(context,cursor,groupLayout,groupFrom,groupTo,childLayout,childrenFrom,childrenTo);        }        // returns cursor with subitems for given group cursor        @OverrIDe        protected Cursor getChildrenCursor(Cursor groupCursor) {                Cursor exercisesCursor = mDbHelper                                .fetchExcercisesForGroup(groupCursor                                                .getLong(mGroupIDColumnIndex));                startManagingCursor(exercisesCursor);                return exercisesCursor;        }        // I needed to process click on click of the button on child item        public VIEw getChildVIEw(final int groupposition,final int childposition,boolean isLastChild,VIEw convertVIEw,VIEwGroup parent) {                VIEw rowVIEw = super.getChildVIEw(groupposition,childposition,isLastChild,convertVIEw,parent);                button details = (button) rowVIEw.findVIEwByID(R.ID.vIEw_button);                details.setonClickListener(new OnClickListener() {                        public voID onClick(VIEw v) {                                Cursor exerciseCursor = getChild(groupposition,childposition);                                Long exerciseID = exerciseCursor.getLong(exerciseCursor.getColumnIndex(ExcercisesDbAdapter.KEY_ROWID));                                Intent i = new Intent(ExercisesList.this,ExerciseVIEw.class);                                i.putExtra(ExcercisesDbAdapter.KEY_ROWID,exerciseID);                                startActivity(i);                        }                });                return rowVIEw;        }}}

希望会有用的;)

总结

以上是内存溢出为你收集整理的Android ExpandableListActivity和SimpleCursorTreeAdapter?全部内容,希望文章能够帮你解决Android ExpandableListActivity和SimpleCursorTreeAdapter?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1131341.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存