0

i have a custom expandableListView with 2 groups . When i expand the first group everything is fine; but when i expand the second group it shows again the content from the first group. How can i fix this ??

public class MiCuentaActivity extends Fragment {


    Usuario user;

     ExpandableListAdapter listAdapter;
        ExpandableListView expListView;
        List<String> listDataHeader;
        HashMap<String, List<HistorialOferta>> listDataChild;
        HashMap<String, List<HistorialOferta>> listWonSubastas;


     public MiCuentaActivity(Usuario user) {
        // TODO Auto-generated constructor stub
         this.user=user;
    }

     public MiCuentaActivity(){

     }

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.activity_mi_cuenta, container, false);

            //StrictMode.enableDefaults();

            expListView= (ExpandableListView) rootView.findViewById(R.id.listaExpan);

            expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

                @Override
                public void onGroupExpand(int arg0) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getActivity(), arg0+" expandeed", Toast.LENGTH_SHORT).show();
                }
            });

            new TareaSync().execute(new ApiConnector("cuenta", this.user));

            return rootView;
        }




    private class TareaSync extends AsyncTask<ApiConnector, Void, HashMap<String, List<HistorialOferta>>>{

        @Override
        protected HashMap<String, List<HistorialOferta>> doInBackground(ApiConnector... params) {
            // TODO Auto-generated method stub
            listDataHeader=new ArrayList<String>();
            listDataChild=new HashMap<String, List<HistorialOferta>>();

            //ApiConnector api=new ApiConnector("cuenta", this.user);
            List<HistorialOferta> ofertas=params[0].getCuenta();
            List<HistorialOferta> lstWon=params[0].getWon();

            listDataHeader.add("Historial Ofertas");
            listDataHeader.add("Subastas Ganadas");



            listDataChild.put(listDataHeader.get(0), ofertas);
            listDataChild.put(listDataHeader.get(1), lstWon);

            return listDataChild;

        }

        @Override
        protected void onPostExecute( HashMap<String, List<HistorialOferta>> list) {
            // TODO Auto-generated method stub
            setListAdapter(list);
        }

        private void setListAdapter( HashMap<String, List<HistorialOferta>> list) {
            // TODO Auto-generated method stub
            expListView.setAdapter(new ExpandableList(getActivity().getApplicationContext(), listDataHeader, list));
        }




    }
}

Adapter:

@Override
public Object getChild(int arg0, int arg1) {
    // TODO Auto-generated method stub
    return this.listOfertas.get(this._listDataHeader.get(arg0)).get(arg1);
}


@Override
    public View getChildView(int groupId, int childId, boolean arg2,
            View convertView, ViewGroup arg4) {
        // TODO Auto-generated method stub

        HistorialOferta ho = (HistorialOferta) getChild(groupId, childId);

        TableLayout table;

        if (convertView == null) {

            if(groupId==0){

            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater
                    .inflate(R.layout.list_expand_item, null);

            table = (TableLayout) convertView.findViewById(R.id.maintable);

            }

            if(groupId==1){

                LayoutInflater infla=(LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                convertView=infla.inflate(R.layout.list_expand_won_item, null);

                table=(TableLayout) convertView.findViewById(R.id.maintableWon);

            }



}


return convertView;




        }

1 Answers1

0

This might help you out.

Also, know that in ListViews, views are recycled later, so you need to make sure you actually reset everything. Using the ViewHolder pattern should help. Google that term to find more examples.

You could also have it that your HistorialOferta objects hold the two layout values that you need, so instead of using the if(groupId==0) code, you could say something like:

convertView = infalInflater.inflate(ho.getViewId(), null)
table = (TableLayout) convertView.findViewById(ho.getTableViewID());

Also, you never actually do anything with your convertView aside from inflating the view; is this what you intended?

gatlingxyz
  • 781
  • 6
  • 12
  • I already debbuged the getChildView method and the problems is here: HistorialOferta ho = (HistorialOferta) getChild(**groupId**, childId); Here **groupId says 0 instead of 1** when i expand the second group; that is why in the second group i see the same data from the first group – Anggelo Bernaola Ibarra Sep 27 '14 at 20:59