Complementando minha dúvida anterior, será que se eu dividir a chamada do AlunoDAO e o dao.close() em onResume() e onPause() é o correto:
public class StudentsListActivity extends AppCompatActivity {
private ListView list;
private StudentDAO dao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
list = (ListView) findViewById(R.id.list);
// moved to onResume() method
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
Toast.makeText(StudentsListActivity.this, "The position is: " + position, Toast.LENGTH_SHORT).show();
}
});
list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapter, View view, int position, long id) {
Toast.makeText(StudentsListActivity.this, "Student clicked is: " + adapter.getItemAtPosition(position), Toast.LENGTH_SHORT).show();
return true;
}
});
}
@Override
protected void onResume() {
super.onResume();
dao = new StudentDAO(this);
List<Student> students = dao.getStudentsLists();
ArrayAdapter<Student> adapter = new ArrayAdapter<Student>(this, android.R.layout.simple_list_item_1, students);
list.setAdapter(adapter);
}
@Override
protected void onPause() {
super.onPause();
dao.close();
}
.
Agradeço desde já.