I'm having problems with two listviews, each one of them generated by an activity. From the listview A, a list of items, I can go to the listview B, options for an specific item of list A.
The problem is when I go from A to B, then back (using the Android back button) to A, and then try to go to B once again: (A --> B --back button-> A --> error when trying to go to B).
I really don't even know where to begin to search for the bug, so I wanted to ask here: anyone with the same error who solved it and can share the solution?
Thanks!
EDIT: I'm posting here the relevant code, as I said I don't know what could be causing the bug so I posted the two listviews:
LISTVIEW A: It has an image, a title of the emergency and details with the distance.
public class EmergenciesList extends ListActivity {
String[] listaEmergencias = new String[] { "Raoul Duffy con Wisconsin",
"Mac Iver con Moneda", "Cdte. Malbec con Barnechea",
"Av. Apoquindo con Pehuen", "Las Hualtatas con Indiana" };
String[] detalleEmergencias = new String[] { "< 50 mts", "2 km", "1,2 km",
"3,4 km", "350 mts" };
private Integer[] imgid = { R.drawable.chico, R.drawable.mediano,
R.drawable.grande, R.drawable.chico, R.drawable.mediano,
R.drawable.grande };
RelativeLayout layr1;
Animation ar3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
data = new Vector<RowData>();
for (int i = 0; i < listaEmergencias.length; i++) {
try {
// RowData is just a private class for the title, details, and image.
rd = new RowData(i, listaEmergencias[i], detalleEmergencias[i]);
} catch (ParseException e) {
e.printStackTrace();
}
data.add(rd);
}
adapter = new CustomAdapter(this, R.layout.second_list, R.id.title, data);
setListAdapter(adapter);
getListView().setTextFilterEnabled(true);
}
public void onListItemClick(ListView parent, View v, int position, long id) {
adapter = (CustomAdapter) parent.getAdapter();
data.removeAllElements();
Intent singleEmergencyMenu = new Intent(EmergenciesList.this, EmergencyMenu.class);
Bundle container = new Bundle();
container.putInt("id", position);
singleEmergencyMenu.putExtra("data2", container);
startActivity(singleEmergencyMenu);
}
}
LISTVIEW B: It has a checkbox in the header and three elements in the list.
public class EmergencyMenu extends ListActivity {
String[] emergencia = new String[] { "Mostrar ubicación",
"Carros asignados", "Archivos" };
final String SETTING_EMERGENCY = "id_actual_emergency";
private int emergencyID = -1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.emergencyID = getIntent().getBundleExtra("data2").getInt("id");
ListView lv = getListView();
lv.setTextFilterEnabled(true);
// A Header to have a single checkbox to select the emergency
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TextView header = (TextView) inflater.inflate(
R.layout.list_reports_header, null);
lv.addHeaderView(header, null, false);
List<String> data = new ArrayList<String>();
for (int i = 0; i < emergencia.length; i++) {
try {
data.add(emergencia[i]);
} catch (ParseException e) {
e.printStackTrace();
}
}
setListAdapter(new ArrayAdapter<String>(this, R.layout.emergency_menu,
data));
LoadSelection();
CheckBox checkBox = (CheckBox) findViewById(R.id.select_emergency_id);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
SaveSelection();
}
});
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Mostrar ubicación
if (position == 1) {
Toast.makeText(getApplicationContext(),"Mostrar ubicación", Toast.LENGTH_SHORT).show();
}
// Carros asignados
else if (position == 2) {
Toast.makeText(getApplicationContext(), "Carros asignados",Toast.LENGTH_SHORT).show();
}
// Archivos
else if (position == 3) {
Toast.makeText(getApplicationContext(),
"Archivos de la emergencia",Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
protected void onPause() {
SaveSelection();
super.onPause();
}
// save the selections in the shared preference in private mode for the
// user
private void SaveSelection() {
CheckBox checkBox = (CheckBox) findViewById(R.id.select_emergency_id);
if (checkBox.isChecked()) {
SharedPreferences settingsActivity = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor prefEditor = settingsActivity.edit();
prefEditor.putInt(SETTING_EMERGENCY, emergencyID);
prefEditor.commit();
}
}
// Check the checkbox if this is the actual emergency (selected before).
private void LoadSelection() {
SharedPreferences settingsActivity = getPreferences(MODE_PRIVATE);
if (settingsActivity.contains(SETTING_EMERGENCY)) {
// Checkbox de seleccionar la emergencia
int id_actual = settingsActivity.getInt(SETTING_EMERGENCY, -1);
if (emergencyID == id_actual) {
CheckBox checkBox = (CheckBox) findViewById(R.id.select_emergency_id);
checkBox.setChecked(true);
}
}
}
}