samedi 17 septembre 2016

Return statement being skipped

I have a recyclerview which is meant to show transactions but seperated by their dates. However my getItemViewType doesnt seem to work. This is the code:

    @Override
    public int getItemViewType(int position) {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
        if (mCompareDate == null || !fmt.format(mTransactions.get(mTransactionPosition).getTime()).equals(fmt.format(mCompareDate))) {
            mCompareDate = mTransactions.get(mTransactionPosition).getTime();
            return 2;
        } else if (mCompareDate != null || fmt.format(mTransactions.get(mTransactionPosition).getTime()).equals(fmt.format(mCompareDate))){
            return 1;
        }
        return 0;
    }

I have an if statement that checks a value, however when I run it, it crashes. I didnt have an 'else if' statement before, but I was hoping it would help the crash, an else statement should work fine.

Anyway I debugged it and watch it step by step and the program seems to find that the first statement is correct, it then does everything inside the statement including the 'return 2;' however afterwards it just skips to return 0, which results in a crash, if I didnt have the else if then it would skips straight into the else statement and return 1, also crashing it.

Aucun commentaire:

Enregistrer un commentaire