How to prevent the app from crash when my adapter is empty. I used notifyDataSetChanged() to update my list...but when it's empty, my app crash. I tried to check if my map is null, but it's not working. Someone can help?
public Adapter_agenda (Context contexto, Map<String, ArrayList<Classes.Eventos>> map, ListView lista) {
dados = map;
this.contexto = contexto;
this.lista = lista;
suporte = new Suporte();
nomes = new ArrayList<String>();
webserver = new Webserver();
inflater = LayoutInflater.from(contexto);
for (Map.Entry<String, ArrayList<Classes.Eventos>> entry : dados.entrySet()) {
nomes.add(entry.getKey());
}
Collections.sort(nomes);
db = new Consultas(contexto);
db.createDatabase();
db.open();
participantes = db.Consulta_Participantes(4);
db.close();
ids = new int[participantes.size()];
nome_participante = new String[participantes.size()];
for (int a = 0; a < participantes.size(); a++) {
Classes.Participantes temp = participantes.get(a);
nome_participante[a] = temp.nome;
ids[a] = temp.id_participante;
}
}
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return dados.get(nomes.get(groupPosition)).get(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.linha_listview, parent, false);
}
if (dados != null) {
evento = dados.get(nomes.get(groupPosition)).get(childPosition);
TextView Evento = (TextView)convertView.findViewById(R.id.horario_palco);
TextView Hora = (TextView)convertView.findViewById(R.id.horario_dia);
CheckBox add_agenda = (CheckBox) convertView.findViewById(R.id.horario_add);
TextView eu_vou = (TextView) convertView.findViewById(R.id.horario_euvou);
eu_vou.setText("Cancelar");
String temp_hora;
db.open();
String modulo = db.Nome_modulo(evento.palco);
db.close();
final int id_evento = evento.id_evento;
final int agendado = evento.agendado;
final int hora = evento.horario;
check = false;
add_agenda.setChecked(agendado == 0 ? false : true);
String temp_evento = evento.evento;
check = false;
Evento.setText(temp_evento);
Hora.setText(temp_hora);
String temp = getGroup(groupPosition).toString();
add_agenda.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (check) {
db.open();
if(db.Add_agenda(id_evento, isChecked)) {
dados.clear();
dados = db.Carrega_Agenda();
notifyDataSetChanged();
Toast.makeText(contexto, "Evento removido com sucesso", Toast.LENGTH_LONG).show();
}
db.close();
}
}
});
check = true;
}
return convertView;
}
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return dados.get(nomes.get(groupPosition)).size();
}
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return nomes.get(groupPosition);
}
public int getGroupCount() {
// TODO Auto-generated method stub
return nomes.size();
}
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.lista_expansivel, parent, false);
}
TextView NomeCantor = (TextView)convertView.findViewById(R.id.lista_epx_item_label);
NomeCantor.setText(getGroup(groupPosition).toString());
return convertView;
}
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
The error occurs when try to count the children.
09-02 11:14:18.692: E/AndroidRuntime(17814): FATAL EXCEPTION: main
09-02 11:14:18.692: E/AndroidRuntime(17814): java.lang.NullPointerException
09-02 11:14:18.692: E/AndroidRuntime(17814): at com.rs21.hallel.objetos.Adapter_agenda.getChildrenCount(Adapter_agenda.java:150)
09-02 11:14:18.692: E/AndroidRuntime(17814): at android.widget.ExpandableListConnector.refreshExpGroupMetadataList(ExpandableListConnector.java:568)
09-02 11:14:18.692: E/AndroidRuntime(17814): at android.widget.ExpandableListConnector.access$000(ExpandableListConnector.java:50)
09-02 11:14:18.692: E/AndroidRuntime(17814): at android.widget.ExpandableListConnector$MyDataSetObserver.onChanged(ExpandableListConnector.java:862)
09-02 11:14:18.692: E/AndroidRuntime(17814): at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
09-02 11:14:18.692: E/AndroidRuntime(17814): at android.widget.BaseExpandableListAdapter.notifyDataSetChanged(BaseExpandableListAdapter.java:56)
09-02 11:14:18.692: E/AndroidRuntime(17814): at com.rs21.hallel.objetos.Adapter_agenda$1.onCheckedChanged(Adapter_agenda.java:135)
09-02 11:14:18.692: E/AndroidRuntime(17814): at android.widget.CompoundButton.setChecked(CompoundButton.java:137)
09-02 11:14:18.692: E/AndroidRuntime(17814): at android.widget.CompoundButton.toggle(CompoundButton.java:92)
09-02 11:14:18.692: E/AndroidRuntime(17814): at android.widget.CompoundButton.performClick(CompoundButton.java:104)
09-02 11:14:18.692: E/AndroidRuntime(17814): at android.view.View$PerformClick.run(View.java:17229)
09-02 11:14:18.692: E/AndroidRuntime(17814): at android.os.Handler.handleCallback(Handler.java:615)
09-02 11:14:18.692: E/AndroidRuntime(17814): at android.os.Handler.dispatchMessage(Handler.java:92)
09-02 11:14:18.692: E/AndroidRuntime(17814): at android.os.Looper.loop(Looper.java:137)
09-02 11:14:18.692: E/AndroidRuntime(17814): at android.app.ActivityThread.main(ActivityThread.java:4960)
09-02 11:14:18.692: E/AndroidRuntime(17814): at java.lang.reflect.Method.invokeNative(Native Method)
09-02 11:14:18.692: E/AndroidRuntime(17814): at java.lang.reflect.Method.invoke(Method.java:511)
09-02 11:14:18.692: E/AndroidRuntime(17814): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
09-02 11:14:18.692: E/AndroidRuntime(17814): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
09-02 11:14:18.692: E/AndroidRuntime(17814): at dalvik.system.NativeStart.main(Native Method)
change
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return dados.get(nomes.get(groupPosition)).size();
}
to
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
if(dados!=null && dados.size>0){
return dados.get(nomes.get(groupPosition)).size();
}else return 0;
}