android - ExpandableListView with multiple choice save selected item into an array -
i have array filled api data, , have expandablelistview show items of array, i'm trying when user clicks on item saves th id of item array, if user select 2 items want 2 ids in array, happening this: no matter if select 1 of items gets of them , save inside array.
public class mainactivity extends fragment { private expandlistadapter expadapter; private expandablelistview expandlist; private button notificar; private context context; mainactivity mcontext; private button footer; private double raioescola; private circleoptions mcircle; gpstracker tracker; private location location; private integer idescola; public mainactivity() { } public mainactivity(context context) { this.context = context; } @override public view oncreateview(final layoutinflater inflater, viewgroup container, bundle savedinstancestate) { final view rootview = inflater.inflate(r.layout.expand, container, false); expandlist = (expandablelistview) rootview.findviewbyid(r.id.exp_list); notificar = (button) rootview.findviewbyid(r.id.btngetmoreresults); new asynctask(mainactivity.this).execute(); return rootview; } private class asynctask extends asynctask<string, void, void> { private progressdialog pd; public asynctask(mainactivity context) { mcontext = context; pd = new progressdialog(getactivity()); pd.settitle("por favor espere ..."); pd.setmessage("enviando ..."); if (!pd.isshowing()) { pd.show(); } } @override protected void doinbackground(final string... params) { try { string[] resposta = new webservice().get("filhos"); if (resposta[0].equals("200")) { jsonobject mjsonobject = new jsonobject(resposta[1]); jsonarray dados = mjsonobject.getjsonarray("data"); /* cria o array que vai receber os dados da api */ final arraylist<escolas> marraylist = new arraylist<escolas>(); /* percorre o array, adicionando cada linha encontrada em um arraylist */ (int = 0; < dados.length(); i++) { jsonobject item = dados.getjsonobject(i); escolas mescolas = new escolas(); mescolas.setid_escola(item.optint("id_escola")); mescolas.setcnpj(item.getstring("cnpj")); mescolas.setrazao_social(item.getstring("razao_social")); mescolas.setnome_fantasia(item.getstring("nome_fantasia")); mescolas.setdistancia(float.valueof(item.getstring("distancia"))); mescolas.setlogradouro(item.optstring("logradouro")); mescolas.setnumero(item.optstring("numero")); mescolas.setbairro(item.getstring("bairro")); mescolas.setcomplemento(item.getstring("complemento")); mescolas.setcep(item.getstring("cep")); mescolas.setcidade(item.getstring("cidade")); mescolas.setestado(item.getstring("estado")); mescolas.setlatitude(float.parsefloat(item.getstring("latitude"))); mescolas.setlongitude(float.parsefloat(item.getstring("longitude"))); raioescola = double.parsedouble(string.valueof(mescolas.getdistancia())); idescola = mescolas.getid_escola(); jsonobject alunos = item.optjsonobject("alunos"); jsonarray data = alunos.getjsonarray("data"); if (data != null) { arraylist<filhos> arrayalunos = new arraylist<filhos>(); (int = 0; < data.length(); a++) { filhos mfilhos = new filhos(); jsonobject clientes = data.getjsonobject(a); mfilhos.setid_aluno(clientes.optint("id_aluno")); mfilhos.setnome(clientes.optstring("nome")); mfilhos.setsobrenome(clientes.optstring("sobrenome")); mfilhos.setfoto(clientes.optstring("foto")); mfilhos.setmodalidade_de_ensino(clientes.optstring("modalidade_de_ensino")); mfilhos.setobservacoes(clientes.optstring("observacoes")); arrayalunos.add(mfilhos); } mescolas.setalunos(arrayalunos); } /* popula o array de viagens */ marraylist.add(mescolas); expadapter = new expandlistadapter(getactivity(), marraylist); getactivity().runonuithread(new runnable() { @override public void run() { expandlist.setadapter(expadapter); expadapter.notifydatasetchanged(); expadapter.setchoicemode(expandlistadapter.choice_mode_multiple); expandlist.setonchildclicklistener(new expandablelistview.onchildclicklistener() { @override public boolean onchildclick(final expandablelistview parent, view v, final int groupposition, final int childposition, final long id) { expadapter.setclicked(groupposition, childposition); final int index = parent.getflatlistposition(expandablelistview.getpackedpositionforchild(groupposition, childposition)); parent.setitemchecked(index, true); parent.setselectedchild(groupposition, childposition, true); parent.getchildat(index); notificar.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { class update extends timertask { public void run() { try { getactivity().runonuithread(new runnable() { @override public void run() { final float latitude = marraylist.get(groupposition).getlatitude(); final float longitude = marraylist.get(groupposition).getlongitude(); latlng latlng = new latlng(latitude, longitude); drawmarkerwithcircle(latlng); gpstracker gps = new gpstracker(getactivity()); double latitudegps = gps.getlatitude(); double longitudegps = gps.getlongitude(); float[] distance = new float[2]; location.distancebetween(mcircle.getcenter().latitude, mcircle.getcenter().longitude, latitudegps, longitudegps, distance); if (distance[0] > mcircle.getradius()) { toast.maketext(getactivity(), "outside", toast.length_long).show(); } else { toast.maketext(getactivity(), "inside", toast.length_long).show(); alertrest malertrest = new alertrest(); try { list<integer> myidlist = new arraylist<integer>(); (int = 0; < marraylist.get(groupposition).getalunos().size(); i++) { integer idalunos = marraylist.get(groupposition).getalunos().get(i).getid_aluno(); myidlist.add(idalunos); } malertrest.getnotificacao(1, marraylist.get(groupposition).getid_escola(), string.valueof(myidlist), latitudegps, longitudegps); } catch (exception e) { e.printstacktrace(); } } } }); } catch (exception e) { e.printstacktrace(); } } } timer timer = new timer(); timer.schedule(new update(), 0, 15000); } private void drawmarkerwithcircle(latlng position) { mcircle = new circleoptions().center(position).radius(raioescola); } }); return false; } }); } }); } /* retorna um array de objetos */ } else { throw new exception("[" + resposta[0] + "] erro: " + resposta[1]); } } catch (exception e) { e.printstacktrace(); } return null; } @override protected void onpostexecute(void cursor) { if (pd.isshowing()) { pd.dismiss(); } } } } alertrest:
public class alertrest extends webservice { private string recurso = "notificacoes"; private string[] resposta; private alertmodelo malertmodelo; public alertmodelo getnotificacao(integer id_usuario, string token, integer id_escola, string ids_aluno, double latitude, double longitude) throws exception { /* dispara requisição para api retornar os dados "recurso" necessário */ resposta = new webservice().posttoken(recurso, token, "{\"id_usuario\":" + id_usuario + "," + "\"id_escola\":" + id_escola + "," + "\"ids_aluno\":\"" + ids_aluno + "\"," + "\"latitude\":" + latitude + "," + "\"longitude\":" + longitude + "}"); jsonobject mjsonobject = new jsonobject(resposta[1]); if (resposta[1].equals("201")) { malertmodelo = new alertmodelo(); malertmodelo.setid_usuario(mjsonobject.getint(string.valueof(id_usuario))); malertmodelo.setid_escola(mjsonobject.getint(string.valueof(id_escola))); malertmodelo.setids_aluno(mjsonobject.getint(string.valueof(ids_aluno))); malertmodelo.setlatitude(mjsonobject.getdouble(string.valueof(latitude))); malertmodelo.setlongitude(mjsonobject.getdouble(string.valueof(longitude))); } return malertmodelo; } }
the code difficult follow due few nested loops , running tasks. anyway, suspect code below causing app save (unselected) items in list:
for (int = 0; < marraylist.get(groupposition).getalunos().size(); i++) { integer idalunos = marraylist.get(groupposition).getalunos().get(i).getid_aluno(); myidlist.add(idalunos); } note: iterating through entire list suspect. should pick item or items in list.
so...another set of relevant codes can determine code fix. not if there bug here also.
parent.setitemchecked(index, true); parent.setselectedchild(groupposition, childposition, true); parent.getchildat(index); note: these codes may fine. key code, think, index. not familiar expandablelistview.
finally, suggested code fix:
integer idalunos = marraylist.get(groupposition).getalunos().get(index).getid_aluno(); ... myidlist.add(idalunos); notice variable index used correct item, idalunos.
Comments
Post a Comment