سؤال

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:

  • I call a MainActivity;
  • The user press a button that calls a LoginActivity;
  • The user fill in the username and password;
  • LoginActivity calls a AsynchronousTask that send the data for a JAX-WS (I use ksoap2 library to call my SOAP web service);
  • If the information are right, a MenuActivity is called;
  • There is a button for "License Plate Search". When it is pressed, I call a Activity for input the license plate and an "OK" button for search;
  • When the "OK" button is pressed, I send the data for a new AsynchronousTask that search for the plate in my web service;
  • If the license plate is a valid one (activity result = OK) I populate my database with: License Plate Number, Year, Model and Developer;
  • After this I call a new Activity called ResultActivity where a show the vehicle information in a new Dialog (HERE IS THE PROBLEM);
  • But, if the result is CANCELED (when my web service doesn't found the license plate) I just show a Alert for the user.

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top