Select me returns null, in the second iteration of the FOR loop, on

Asked

Viewed 27 times

0

I have a database, ROOM, I make a select right after the return of a asyncTask. I take the U-turn and make a select, awaiting the return of this select.

Works normally in the first iteration of for, but from the second on, the return is always zero. The data is in the database and should return me.

If I do for ALT+F8, the data return me, in the query. But by the for, is void.

Why does this happen?

Appdatabase:

private AppDataBase appDataBase;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_atendimento_tab);

    appDataBase = AppDataBase.getInstance(this);
}

@Database(entities = {CidVo.class}, version = 8, exportSchema = false)
public abstract class AppDataBase extends RoomDatabase {

 public abstract CidDao getCidDao();

    private static AppDataBase appDataBase;

    public static AppDataBase getInstance(Context context) {
        if (null == appDataBase) {
            appDataBase = buildDataBaseInstance(context);
        }
        return appDataBase;
    }

    private static AppDataBase buildDataBaseInstance(Context context) {
        return Room.databaseBuilder(context,
                AppDataBase.class,
                "AutoCompleteVo")
                .fallbackToDestructiveMigration()
                .allowMainThreadQueries().build();
    }

    //CID predicted
    public CidVo getCid(Context context, String idCidVo) {
        if (appDataBase == null) {
            appDataBase = AppDataBase.getInstance(context);
        }
        return appDataBase.getCidDao().getCidVo("%" + idCidVo + "%");
    }
}

DAO:

@Dao
public interface CidDao {
    //CID
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertAllCID(List<CidVo> cidVos);

    @Query("Select * from CidVo WHERE idCid = :idCid")
    CidVo getCidVo(String idCid);

}

FOR:

@Override
    public void retornoAsyncTaskResultPredict(AsyncTaskResult<Retorno> asyncTaskResult) {
        if (asyncTaskResult.getExceptionResult() == null) {

            RetornoPredicaoCid predicaoCidVo = (RetornoPredicaoCid) asyncTaskResult.getResult();

            if (predicaoCidVo.getRetorno() != null) {

                List<PredicaoCidVo> predCid = predicaoCidVo.getRetorno();
                List<CidVo> predictedText = new ArrayList<>();
                List<CidVo> predictedText1 = new ArrayList<>();

                for (int i = 0;i<predCid.size();i++) {
                    //appDataBase = AppDataBase.getInstance(this);
                    CidVo cidVo = appDataBase.getCidDao().getCidVo(predCid.get(i).getTextPredicted());
                    CidVo cidVo1 = appDataBase.getCid(this, predCid.get(i).getTextPredicted());
                    predictedText1.add(cidVo1);
                    predictedText.add(cidVo);
                }


            }
    }
  • if the problem is not in the query (ie has data) must be in control of the async (I don’t answer because I don’t know, I was wrong =p)

  • Async has already been done, this is the return, only.

  • In the second iteration, it loses the instance, it seems to me of getCidVo, but I do not understand the reason.

1 answer

0

In the second iteration onwards, the items came with a space at the beginning or end, causing the problem. Solved with a simple .trim();

predCid.get(i).getTextPredicted().trim()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.