2012-04-09 3 views
0

Je souhaite gérer onclick sur les groupes d'une liste ExpandableListView personnalisée. Avec ce code, je reçois le numéro de groupe en cliquant dessus:Android ExpandableListView obtient le numéro de groupe lors de la fermeture du groupe

exList.setOnGroupClickListener(new OnGroupClickListener() { 

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

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

J'ajoute le code simplifié. J'espère que vous pouvez comprendre ce que le problème pourrait être:

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"); 
    } 
} 

Cependant, quand je clique à nouveau, le groupe ferme, mais je ne suis pas le numéro de groupe. Je ne comprends, quand j'ouvre un groupe.

Si je

exList.setOnGroupClickListener(new OnGroupClickListener() { 

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

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

}); 

je reçois deux fois le numéro de groupe, mais les groupes ne sont pas d'ouverture.

Des idées?

+0

Vous avez manqué quelque chose dans votre journal. 'onGroupClick' vous transmettra définitivement la position de groupe indépendamment du fait que le groupe soit développé ou callapé. En ce qui concerne votre deuxième bloc de code: vous revenez toujours vrai pour que le système ne gère pas l'expansion/callpasing – 207

+0

Je n'ai rien manqué. Je l'ai testé plusieurs fois, je ne peux pas voir la ligne de log quand je ferme le groupe. Je l'ai essayé avec du pain grillé. Je reçois les messages uniquement à l'ouverture. – erdomester

+0

Je suppose que vous utilisez l'implémentation par défaut de l'infrastructure et non une personnalisée. Vous pouvez fournir le code approprié afin que le problème puisse être reproduit – 207

Répondre

0

Dans onCreate() votre configuration de votre ExpListView (findViewById, adaptateur, groupe/enfant, etc.). Mais dans refresh() votre configuration de votre ExpListView sans définir l'auditeur ... vous appelez seulement findViewById et définissez l'adaptateur. Vous devez faire la même chose que vous l'avez fait en onCreate().

Cependant, je ne pense pas qu'il soit nécessaire de "réinitialiser" votre ExpListView en refresh() et vous devriez probablement revoir votre code. Si vous voulez que je vous suggère de définir votre ExpListView comme variable membre et refresh vous supprimez la ligne

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

dans votre méthode refresh

+0

Cela n'aide pas. Je ne sais pas ce que je devrais mettre le groupclicklistener à la refreshlist(), ce serait la duplication. – erdomester

+0

C'est pourquoi je vous ai suggéré de supprimer la ligne mentionnée ci-dessus de votre méthode 'refresh()'. Si ça ne marche pas, je ne peux plus t'aider car ton code est très difficile à lire ... ne sois pas fâché mais c'est vraiment un gâchis – 207

+0

Je sais, je sais. Je viens d'ouvrir un nouveau sujet. Si cela peut être résolu, je ne suis pas la solution à ce problème. Merci pour votre temps! – erdomester

0

Cependant, le onGroupClickListener ne fonctionne pas correctement, j'ai trouvé une autre solution . J'ai utilisé setOnGroupExpandListener et setOnGroupCollapseListener et ils fonctionnent magiquement!

Questions connexes