Note that there are some explanatory texts on larger screens.

plurals
  1. POown adapter for expandablelistview crash cannot cast hasmap to list
    text
    copied!<p>I have my onw adapter for a <code>expandiblelistview</code>, it show the parents fine but when i click in a group to see the childs it crash:</p> <p><code>java.lang.ClassCastException: java.util.HashMap cannot be cast to java.util.List</code></p> <p>This is my own adapter:</p> <pre><code> public class adaptadorLista extends SimpleExpandableListAdapter { private Context mContext; private List&lt;? extends List&lt;? extends Map&lt;String, ?&gt;&gt;&gt; mChildData; private List&lt;? extends Map&lt;String, ?&gt;&gt; mParentData; private String[] mChildFrom; private int[] mChildTo; public adaptadorLista(Context contexto, List&lt;? extends Map&lt;String, ?&gt;&gt; creaListaDeGrupos, int lineaPadre, String[] strings, int[] is, List&lt;? extends List&lt;? extends Map&lt;String, ?&gt;&gt;&gt; creaListadeHijos, int lineaHija, String[] strings2, int[] is2) { super(contexto, creaListaDeGrupos, lineaPadre, strings, is, creaListadeHijos, lineaHija, strings2, is2); mContext = contexto; mChildData = creaListadeHijos; mChildFrom = strings2; mChildTo = is2; } @Override public long getChildId(int groupPosition, int childPosition) { return 0; } public View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) { View v; if (convertView == null) { v = newChildView(isLastChild, parent); } else { v = convertView; } bindView(v, mChildData.get(groupPosition).get(childPosition), mChildFrom, mChildTo, groupPosition, childPosition); return v; } // This method binds my data to the Views specified in the child xml private void bindView(View view, Map&lt;String, ?&gt; data,String[] from, int[] to, int groupPosition, int childPosition) { int len = to.length - 1; // Apply TextViews for (int i = 0; i &lt; len; i++) { TextView v = (TextView) view.findViewById(to[i]); if (v != null) { v.setText((String) data.get(from[i])); } } } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } } </code></pre> <p>And the constructor with 2 methods for populate the data:</p> <pre><code>adaptadorLista expListAdapter = new adaptadorLista( contexto, creaListaDeGrupos(), R.layout.linea_padre, new String[] { "numero_servicio","profesion","direccion","fecha_cad" }, new int[] { R.id.tvServicio,R.id.tvProfesion,R.id.tvDireccion,R.id.tvfecha}, creaListadeHijos(), R.layout.linea_hija, new String[] { "tipo", "diasEnCurso","estado","fechaProCita" }, new int[] {R.id.tvTipo , R.id.tvDiasEnCurso, R.id.tvEstado,R.id.tvFechaProximaCita} ); setListAdapter( expListAdapter); registerForContextMenu(getExpandableListView()); } private List&lt;? extends List&lt;? extends Map&lt;String, ?&gt;&gt;&gt; creaListadeHijos() { ArrayList resultado_hijo = new ArrayList(); for(int i=0;i&lt;ArrayServicios.size();i++){ HashMap hijo = new HashMap(); hijo.put("tipo", tipos.get(i)); hijo.put("diasEnCurso", diasEnCurso.get(i)); hijo.put("estado", estados.get(i)); hijo.put("fechaProCita", fechasProximasCitas.get(i)); resultado_hijo.add(hijo); } return (List)resultado_hijo; } @SuppressWarnings({ "unchecked", "rawtypes" }) private List&lt;? extends Map&lt;String, ?&gt;&gt; creaListaDeGrupos() { ArrayList resultado_padre = new ArrayList(); for(int i=0;i&lt;ArrayServicios.size();i++){ HashMap padre = new HashMap(); padre.put("numero_servicio", numerosServicios.get(i)); padre.put("profesion", profesiones.get(i)); padre.put("direccion", direcciones.get(i)); padre.put("fecha_cad", fechasCaduca.get(i)); resultado_padre.add(padre); } return (List)resultado_padre; } </code></pre> <p>I found a example using this adaptador and works but not for me..</p> <p><strong>Solution :</strong></p> <pre><code>/********Metodo que rellena la lista de hijos*********************/ private List&lt;? extends List&lt;? extends Map&lt;String, ?&gt;&gt;&gt; creaListadeHijos() { List&lt;List&lt;Map&lt;String, String&gt;&gt;&gt; resultado_hijo = new ArrayList&lt;List&lt;Map&lt;String, String&gt;&gt;&gt;(); for(int i=0;i&lt;ArrayServicios.size();i++){ List&lt;Map&lt;String, String&gt;&gt; hijo = new ArrayList&lt;Map&lt;String,String&gt;&gt;(); Map&lt;String, String&gt; curChildMap = new HashMap&lt;String, String&gt;(); curChildMap.put("tipo", tipos.get(i)); curChildMap.put("diasEnCurso", diasEnCurso.get(i)); curChildMap.put("estado", estados.get(i)); curChildMap.put("fechaProCita", fechasProximasCitas.get(i)); hijo.add(curChildMap); resultado_hijo.add(hijo); } return resultado_hijo; } /*********************************************************************/ </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload