This is my first question here in SO. So, here's my problem: I'm developing an app for android. This is how my app flows:
The problem is happening when I try to show the information for the user. Here is my ResultActivity:
public class ResultadoBuscaPlacaActivity extends Activity {
private VeiculoDAO vDao;
TextView campoPlaca;
TextView campoModelo;
TextView campoMarca;
TextView campoAno;
TextView campoRenavam;
TextView campoProprietario;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_resultado_busca_placa);
campoPlaca = (TextView) findViewById(R.id.valor_placa);
campoModelo = (TextView) findViewById(R.id.valor_modelo);
campoMarca = (TextView) findViewById(R.id.valor_marca);
campoAno = (TextView) findViewById(R.id.valor_ano);
campoRenavam = (TextView) findViewById(R.id.valor_renavam);
campoProprietario = (TextView) findViewById(R.id.valor_proprietario);
vDao = new VeiculoDAO(this);
vDao.open();
}
public void confirm(View v){
finish();
}
@Override
protected void onStart() {
super.onStart();
Veiculo v = new Veiculo();
v = vDao.getFirstElement();
if (v.getPlaca()!=null){
campoPlaca.setText(v.getPlaca());
campoModelo.setText(v.getModelo());
campoMarca.setText(v.getMarca());
campoAno.setText(v.getAno());
campoRenavam.setText(v.getRenavan().toString());
campoProprietario.setText(v.getNomeProprietario());
}
}
@Override
protected void onDestroy() {
vDao.close();
super.onDestroy();
}
@Override
protected void onPause() {
vDao.close();
super.onPause();
}
}
The VeiculoDAO is a simple DAO that calls a Custom SQLiteOpenHelper class. The method getFirstElement() is here:
public Veiculo getFirstElement(){
Cursor cursor = database.query(CustomSQLiteOpenHelper.TABLE_VEICULO,
columns, null, null, null, null, null);
cursor.moveToFirst();
Veiculo v = new Veiculo();
while (!cursor.isAfterLast()) {
v.setAno(cursor.getInt(cursor.getColumnIndex(CustomSQLiteOpenHelper.COLUMN_ANO)));
v.setMarca(cursor.getString(cursor.getColumnIndex(CustomSQLiteOpenHelper.COLUMN_MARCA)));
v.setModelo(cursor.getString(cursor.getColumnIndex(CustomSQLiteOpenHelper.COLUMN_MOD)));
v.setNomeProprietario(cursor.getString(cursor.getColumnIndex(CustomSQLiteOpenHelper.COLUMN_NMPROP)));
v.setPlaca(cursor.getString(cursor.getColumnIndex(CustomSQLiteOpenHelper.COLUMN_PLACA)));
v.setRenavan(cursor.getLong(cursor.getColumnIndex(CustomSQLiteOpenHelper.COLUMN_MARCA)));
}
cursor.close();
return v;
}
What is actually happening is: when the result activity calls the method onStart() and goes to the line:
v = vDao.getFirstElement();
The UI goes back to MenuActivity (because I called the SearchActivity by startActivityForResult() ) but it stops there, when it supposed to call ResultActivity.
In logcat, this message runs in loop:
D/dalvikvm(28637): GC_CONCURRENT freed 631K, 15% free 18650K/21824K, paused 1ms+4ms, total 25ms<br>
If I comment the specific line above, the app runs "normally" and the result Activity is called, but the information are empty (of course).
Does anyone know what should I do?
This loop never terminates since the cursor is never advanced inside the loop and the return value of isAfterLast()
does not change.
while (!cursor.isAfterLast()) {
}
Since you are only interested in the first result, change it to
if (cursor.moveToFirst()) {
}
You can remove the earlier moveToFirst()
call as redundant. It's still important to check the return value as there may be no result rows and then moveToFirst()
returns false.