Android ExpandableListView在关闭组时获取组号

时间:2012-04-09 16:50:24

标签: android expandablelistview

我想处理onclick上自定义ExpandableListView的组。使用此代码,我在点击它时获得组的数量:

exList.setOnGroupClickListener(new OnGroupClickListener() {

    @Override
    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {

        Log.i("group position", groupPosition + "");
        return false;
    }
});

我正在添加简化代码。我希望你能弄明白问题可能是什么:

public class ProductLists extends Activity {
    private static final String G_TEXT = "G_TEXT";
    private static final String C_TITLE = "C_TITLE";
    private static final String C_TEXT = "C_TEXT";
    private static final String C_CB = "C_CB";

    List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();    
    List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();

    List<Map<String, Boolean>> groupCheckBox = new ArrayList<Map<String,Boolean>>();
    List<List<Map<String, Boolean>>> childCheckBox = new ArrayList<List<Map<String,Boolean>>>();
    String loadedexpanded;
    String upperString;
    int number, k_num, k_num2;

    String etext_newitem;
    EditText et_newitem;

    String CheckedItem;
    String CheckedItems;
    ExpandBaseAdapter adapter;
    ExpandableListView exList;

    HotOrNot info;
    ArrayList<String> grpsfav = new ArrayList<String>();

    ArrayList<String> OftenUsedAll = new ArrayList<String>();
    ArrayList<String> OftenUsedID = new ArrayList<String>();
    ArrayList<String> OftenUsedName = new ArrayList<String>();
    ArrayList<String> OftenUsedNumber = new ArrayList<String>();

    Button sqlExp, sqlAdd;
    ArrayList<String> PRLists = new ArrayList<String>();
    ArrayList<String> PRListsR = new ArrayList<String>();
    ArrayList<String> PRListsID = new ArrayList<String>();
    ArrayList<String> PRLists2 = new ArrayList<String>();
    ArrayList<String> todoItems = new ArrayList<String>();
    ArrayList<String> todoItemsID = new ArrayList<String>();
    ArrayList<String> todoItemsNAME = new ArrayList<String>();
    List<String> usable_chars = Arrays.asList(";", "'", "/", "\"", "%", "'\'", "$", "+", "-", "=", ":", "_");
    ArrayList<String> todoItemsTEMP = new ArrayList<String>();
    ArrayList<String> todoItemsIDTEMP = new ArrayList<String>();
    ArrayList<String> todoItemsNAMETEMP = new ArrayList<String>();

    ArrayList<String> inners = new ArrayList<String>();
    ArrayList result2;
    Button sqlView, sqlValami;
    Cursor c, c2;
    String newlistname, modifiedlistname;
    String loadedCapital, loadedshowhints;
    SharedPreferences sharedPreferences;

    ExpandableListView expandlist;
    DisplayMetrics metrics;
    int width;

    List<String> selectionList = Arrays.asList("Rename", "Delete");
    List<String> selectionListA = Arrays.asList("Add item", "Rename group", "Delete group");
    CharSequence[] selectionList2, selectionList2A;
    String rowIdtobemodified, rowIDtobedeleted;
    int groupPosition;
    int childPosition;
    AlertDialog.Builder builder;

    Dialog dialog_newitem, dialog_newgroup;

    Dialog dialog_renamelist, dialog_deletelist;
    EditText et_renamelist, et_deletelist;
    //String etext_renamelist, etext_deletelist;
    String renamelist_name, deletelist_name;
    Spinner SPProductLists3, SPProductLists32;
    Button btn_rename_save, btn_rename_cancel, btn_delete_save, btn_delete_cancel;
    String allerrors_newproductlistdialog;
    ArrayList<String> errors_addgroup = new ArrayList<String>();
    String selected_item_from_lists_torename, selected_item_from_lists_torename2;
    List<String> nonusable_chars = Arrays.asList(";", "'", "/", "\"", "%", "'\'", "$", "+", "-", "=", ":", "_");

    int max;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_productlists3);

        exList = (ExpandableListView) findViewById(R.id.layoutExListView);

        info = new HotOrNot(this);
        info.open();
        //----------------------------query tables--------------------------------
        c = info.showAllTables();
        if (c.moveToFirst())
        {
            do{
                PRLists.add(c.getString(0));

            }while (c.moveToNext());
         }
         if (PRLists.size() >= 0)
         {
             for (int i=0; i<PRLists.size(); i++)
             {
                Log.d("PRLists(" + i + ")", PRLists.get(i) + "");
             }
         }

        //Declare base adapter
        max = 0;
        for (int i = 0; i < PRLists.size(); i++) 
        {
            Map<String, String> curGroupMap = new HashMap<String, String>();
            groupData.add(curGroupMap);
            curGroupMap.put(G_TEXT, PRLists.get(i).replaceAll("_", " ").substring(2, PRLists.get(i).replaceAll("_", " ").length()));

            List<Map<String, String>> children = new ArrayList<Map<String, String>>();

            if (!PRLists.get(i).equals("PR_Often_Used"))
            {
                Log.i("tabla", PRLists.get(i) + "");
                c2 = info.getAllTitlesPRtables(PRLists.get(i));
                if (c2.moveToFirst())
                {
                    do{
                        Map<String, String> curChildMap = new HashMap<String, String>();
                        children.add(curChildMap);
                        curChildMap.put(C_TITLE, c2.getString(1).replaceAll("_", " "));
                        curChildMap.put(C_TEXT, "Child ");
                    }while (c2.moveToNext());
                }
            }
            else
            {
                Log.i("tabla", "PR_OFTEN_USED");
                c2 = info.getAllTitlesOftenUsed("PR_Often_Used");
                if (c2.moveToFirst())
                {
                    do{
                        Map<String, String> curChildMap = new HashMap<String, String>();
                        children.add(curChildMap);
                        curChildMap.put(C_TITLE, c2.getString(1).replaceAll("_", " "));
                        curChildMap.put(C_TEXT, "Child ");
                    }while (c2.moveToNext());
                }
            }

            if (children.size() > max)
            {
                max = children.size();
            }
            childData.add(children);
        }
        info.close();

        for ( int i = 0; i < PRLists.size(); i++) {
            List<Map<String, Boolean>> childCB = new ArrayList<Map<String,Boolean>>();
            for (int j = 0; j < max; j++) { //leghosszabb belso lista merete
                Map<String, Boolean> curCB = new HashMap<String, Boolean>();
                childCB.add(curCB);
                curCB.put(C_CB, false);
            }
            childCheckBox.add(childCB);
        }

        adapter = new ExpandBaseAdapter(ProductLists.this,
        groupData, childData, groupCheckBox, childCheckBox);
        exList  = (ExpandableListView) findViewById(R.id.layoutExListView);
        exList.setAdapter(adapter);
        exList.setGroupIndicator(null);
        exList.setDivider(null);

        int groupCount = adapter.getGroupCount();
        for (int i = 0; i < groupCount; i++) {
            exList.collapseGroup(i);
        }

        exList.setOnChildClickListener(new OnChildClickListener() {
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {

                CheckBox checkBox = (CheckBox) v.findViewById(R.id.multiple_checkbox);
                checkBox.toggle();
                if (childCheckBox.get(groupPosition).get(childPosition).get(C_CB)) //ha itt hiba akkor azert van m az adapterben akkor allitok cb statuszt ha van children, es mivel ha nincs akkor ez ertelmezhetetlen
                {
                    childCheckBox.get(groupPosition).get(childPosition).put(C_CB, false);
                }
                else {
                    childCheckBox.get(groupPosition).get(childPosition).put(C_CB, true);
                }

                return false;
            }
        });

        exList.setOnGroupClickListener(new OnGroupClickListener() {

            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {

                //Log.i("group position", groupPosition + "");
                Toast.makeText(ProductLists.this, "group position" + groupPosition, Toast.LENGTH_SHORT).show();
                Log.i("PRLists.get(groupPosition)", PRLists.get(groupPosition));
                if (PRLists.get(groupPosition).equals("PR_Often_Used"))
                {

                }
                return false;
            }
        });

        exList.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
                    groupPosition = ExpandableListView.getPackedPositionGroup(id);
                    childPosition = ExpandableListView.getPackedPositionChild(id);

                    selectionList2 = selectionList.toArray(new CharSequence[selectionList.size()]);
                    builder = new AlertDialog.Builder(ProductLists.this);
                    builder.setItems(selectionList2, new DialogInterface.OnClickListener()
                    {
                        public void onClick(DialogInterface dialog, final int item) 
                        {
                            if (selectionList2[item].equals("Rename"))
                            {
                            }
                            if (selectionList2[item].equals("Delete"))
                            {
                            }
                        }
                    });
                    builder.show();
                    return true;
                }
                else if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
                    groupPosition = ExpandableListView.getPackedPositionGroup(id);
                    Log.i("tablagrp", PRLists.get(groupPosition) + "-" + groupPosition);

                    selectionList2A = selectionListA.toArray(new CharSequence[selectionListA.size()]);
                    builder = new AlertDialog.Builder(ProductLists.this);
                    builder.setItems(selectionList2A, new DialogInterface.OnClickListener()
                    {
                        public void onClick(DialogInterface dialog, final int item) 
                        {
                            if (selectionList2A[item].equals("Add item"))
                            {
                            }
                            else if (selectionList2A[item].equals("Rename group"))
                            {
                            }
                            else if (selectionList2A[item].equals("Delete group"))
                            {
                            }
                        }
                    });
                    builder.show();
                    return true;
                }
                return false;
            }
        });
    }


    public void refreshList() {
        if (PRLists.size() >= 0)  {
            PRLists.clear();
            PRListsID.clear();
        }
        groupData.clear();
        childData.clear();
        groupCheckBox.clear();
        childCheckBox.clear();

        info = new HotOrNot(this);
        info.open();
        //----------------------------query tables--------------------------------
        c = info.showAllTables();
        if (c.moveToFirst())
        {
            do{
                PRLists.add(c.getString(0));
            }while (c.moveToNext());
        }
        if (PRLists.size() >= 0)
        {
            for (int i=0; i<PRLists.size(); i++)
            {
                Log.d("PRLists(" + i + ")", PRLists.get(i) + "");
            }
        }
        ExpandBaseAdapter adapter = new ExpandBaseAdapter(ProductLists.this, groupData, childData, groupCheckBox, childCheckBox);
        ExpandableListView exList  = (ExpandableListView) findViewById(R.id.layoutExListView);
        exList.setAdapter(adapter);
        max = 0;
        for (int i = 0; i < PRLists.size(); i++) 
        {
            Map<String, String> curGroupMap = new HashMap<String, String>();
            groupData.add(curGroupMap);
            curGroupMap.put(G_TEXT, PRLists.get(i).replaceAll("_", " ").substring(2, PRLists.get(i).replaceAll("_", " ").length()));
            ArrayList parent = new ArrayList();
            List<Map<String, String>> children = new ArrayList<Map<String, String>>();
            ArrayList child = new ArrayList();
            c2 = info.getAllTitlesPRtables(PRLists.get(i));
            if (c2.moveToFirst())
            {
                do{
                    Map<String, String> curChildMap = new HashMap<String, String>();
                    children.add(curChildMap);
                    curChildMap.put(C_TITLE, c2.getString(1).replaceAll("_", " "));
                    curChildMap.put(C_TEXT, "Child ");
                }while (c2.moveToNext());
            }
            if (children.size() > max) {
                max = children.size();
            }
            childData.add(children);
        }
        info.close();
        for ( int i = 0; i < PRLists.size(); i++) {
            List<Map<String, Boolean>> childCB = new ArrayList<Map<String,Boolean>>();
            for (int j = 0; j < max; j++) {
                Map<String, Boolean> curCB = new HashMap<String, Boolean>();
                childCB.add(curCB);
                curCB.put(C_CB, false);
            }
            childCheckBox.add(childCB);
        }

        adapter.notifyDataSetChanged();
        //expand only those groups that was expanded by the user
        for (int i = 0; i < grpsfav.size(); i++) {
            Log.i("grpsfav items", grpsfav.get(i));
            exList.expandGroup(Integer.valueOf(grpsfav.get(i)));
        }
    }
    //the Holder class-------------------------------------------------------------------

    static class ViewHolder {
        TextView cTitle;
        // TextView cText;
        CheckBox checkBox;
    }

    public class ExpandBaseAdapter extends BaseExpandableListAdapter {

        String loadedFontSize, loadedFontType, loadedConfDel, loadedshowhints; 
        SharedPreferences sharedPreferences;

        Context mContext;
        private static final String G_TEXT = "G_TEXT";
        //private static final String G_CB = "G_CB";
        private static final String C_TITLE = "C_TITLE";
        private static final String C_TEXT = "C_TEXT";
        private static final String C_CB = "C_CB";

        List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
        List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();

        List<Map<String, Boolean>> groupCheckBox = new ArrayList<Map<String,Boolean>>();
        List<List<Map<String, Boolean>>> childCheckBox = new ArrayList<List<Map<String,Boolean>>>();

        private Context context;

        ViewHolder holder;

        public ExpandBaseAdapter(Context context,
                List<Map<String, String>> groupData, List<List<Map<String, String>>> childData,
                List<Map<String, Boolean>> groupCheckBox, List<List<Map<String, Boolean>>> childCheckBox) {
            this.groupData = groupData;
            this.childData = childData;
            this.groupCheckBox = groupCheckBox;
            this.childCheckBox = childCheckBox;
            this.context = context;
            mContext = context;
        }

        public View getGroupView(int groupPosition, boolean isExpanded,
                View convertView, ViewGroup parent) {
            View view = convertView;
            if (view == null) {
                LayoutInflater inflater = (LayoutInflater) 
                    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.groupitem, null);
            }
            TextView title = (TextView) view.findViewById(R.id.groupText);
            title.setText(getGroup(groupPosition).toString());
            ImageView image = (ImageView) view.findViewById(R.id.groupBox);
            if (isExpanded) {
                image.setBackgroundResource(R.drawable.expander_ic_maximized);
            }
            else {
                image.setBackgroundResource(R.drawable.expander_ic_minimized);
            }
            return view;
        }

        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        public Object getGroup(int groupPosition) {
            return groupData.get(groupPosition).get(G_TEXT).toString();
        }

        public int getGroupCount() {
            return groupData.size();
        }

        // **************************************
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            if (convertView == null) 
            {
                LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.childitem, null);
                holder = new ViewHolder();
                holder.cTitle = (TextView) convertView.findViewById(R.id.child_title);
                holder.checkBox = (CheckBox) convertView.findViewById(R.id.multiple_checkbox);
                convertView.setTag(holder);
            }
            else
            {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.cTitle.setText(childData.get(groupPosition).get(childPosition).get(C_TITLE).toString());
            if (getChildrenCount(groupPosition) > 0) {
                Log.i("chlrden", getChildrenCount(groupPosition) + "");
                holder.checkBox.setChecked(childCheckBox.get(groupPosition).get(childPosition).get(C_CB));
            }
            return convertView;
        }

        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        public Object getChild(int groupPosition, int childPosition) {
            return childData.get(groupPosition).get(childPosition).get(C_TITLE).toString();
        }

        public int getChildrenCount(int groupPosition) {
            return childData.get(groupPosition).size();
        }

        // **************************************
        public boolean hasStableIds() {
            return true;
        }

        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
    }


    public void SaveExpanded(String key, String value){
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.commit();
       }
    public void LoadExpanded(){
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        loadedexpanded = sharedPreferences.getString("expand", "false");
    }
}

然而,当我再次点击它时,该组关闭,但我没有得到组号。当我打开一个小组时,我才明白。

如果我使用

exList.setOnGroupClickListener(new OnGroupClickListener() {

    @Override
    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {

        if (groupPosition > 0){
            return true;
        }
         return false;
    }

});

我两次获得组号,但组不会打开。

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

onCreate()中设置ExpListView(findViewById,适配器,组/子侦听器等)。但是在refresh()中你设置了ExpListView而没有设置监听器......你只需要调用findViewById并设置适配器。你必须像在onCreate()中那样做。

但是我不认为你在refresh()中“重新初始化”你的ExpListView是必要的,你应该检查你的代码。如果你想我建议你将ExpListView定义为成员变量,并在refresh中删除该行

ExpandableListView exList  = (ExpandableListView) findViewById(R.id.layoutExListView);
您的refresh方法

中的

答案 1 :(得分:0)

然而,onGroupClickListener无法正常工作,我找到了另一种解决方案。我使用了setOnGroupExpandListenersetOnGroupCollapseListener,他们正在神奇地工作!