android - 将 ListView 更改为 ExpandableListView

标签 android android-layout listview expandablelistview

我的 Activity 中有一个 ListView,但现在我想将其更改为 ExpandableListView。我是否需要完全更改我的 ListView ,或者我可以将 expandableListView 添加到我现有的 ListView 中?

最佳答案

这是我使用的示例。您可以更改它并使用它。

// ListView 代码

public class FevList extends ExpandableListActivity {
        String title;
        String url;
        SQLiteDatabase database;
        DbUtil db;
        HashMap<String, String> map = new HashMap<String, String>();
        ArrayList<HashMap<String, String>> subjectList = new ArrayList<HashMap<String, String>>();


        @SuppressWarnings("unchecked")
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            try {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.exlist);
                db = new DbUtil();
                database = db.openConnection(this);
                // Thread for getting values of title from DataBase.
                SimpleExpandableListAdapter expListAdapter = new SimpleExpandableListAdapter(
                        this, createGroupList(), R.layout.group_row,
                        new String[] { "subject" }, new int[] { R.id.row_name },
                        createChildList(), R.layout.child_row,
                        new String[] { "title" }, new int[] { R.id.grp_child });
                setListAdapter(expListAdapter);
                registerForContextMenu(getExpandableListView());
            } catch (Exception e) {
                e.printStackTrace();
            }

        }



 // code for adapter



/* Creating the Hashmap for the row */
                ArrayList<HashMap<String, String>> result = new ArrayList<HashMap<String, String>>();

                @SuppressWarnings("unchecked")
                private List createGroupList() {

    // write your code here.

                    return (List) result;

                }

                /* creatin the HashMap for the children */

                @SuppressWarnings("unchecked")
                private List createChildList() {

    // write your code here.
                    return result;
                }

                public void onContentChanged() {

                    System.out.println("onContentChanged");

                    super.onContentChanged();

                }

                /* This function is called on each child click */

                public boolean onChildClick(ExpandableListView parent, View v,
                        int groupPosition, int childPosition, long id) {

    // write your code here.

                    return true;

                }

                /* This function is called on expansion of the group */

                public void onGroupExpand(int groupPosition) {

                    try {

                        System.out.println("Group exapanding Listener => groupPosition = "
                                + groupPosition);

                    } catch (Exception e) {

                        System.out.println(" groupPosition Errrr +++ " + e.getMessage());

                    }

                }
            }

关于android - 将 ListView 更改为 ExpandableListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18510291/

相关文章:

c++ - 将项目添加到 ListView?

android - Android 中的日期格式

android - 如何在同一个布局中使用多个 GLSurfaceView 组件?

android - android L 上的 Logcat 在拔下并重新连接后未清除

android - 检查类文件中的条件后如何动态加载线性布局?

java - 在 Android Activity 中保存/重新加载数据,可能使用单独的类

c# - 在发布 android 应用程序之前,admob AdRequest 代码需要进行哪些更改?

android - 锁屏上的 Activity 不会全屏显示

java - android字符串如何放置&字符

asp.net - 以编程方式控制 ListView CSS