user12219021
user12219021

Reputation:

Throwing Number Format Exception for input String "type" Custom List View

I have tried the below code in Custom List View.I tried to replace Call type with "Incoming","Outgoing" and "missed" it is showing Number Format Exception: For Input String "type"

assert cr != null;
if (cr.moveToLast()) {
    for (int i = 0; i < cr.getCount(); i++) {
        CallLogData cons = new CallLogData();

        cons.setNumber(cr.getString(cr.getColumnIndex(CallLog.Calls. NUMBER)));
        cons.setType(cr.getString( cr.getColumnIndex(CallLog.Calls. TYPE)));
        callsListView.add(cons);

        cr.moveToPrevious();
    }

    String dir = null;
    int dircode = Integer.parseInt(getString(Integer.parseInt(CallLog.Calls. TYPE)));
    switch (dircode) {
        case CallLog.Calls.OUTGOING_TYPE:
            dir = "OUTGOING";
            break;

            case CallLog.Calls.INCOMING_TYPE:
                dir = "INCOMING";
                break;

            case CallLog.Calls.MISSED_TYPE:
                dir = "MISSED";
                break;
        }

        sb.append("\n  ").append(dir);

    }
cr.close();

This line where i am getting an error

 int dircode = Integer.parseInt(getString(Integer.parseInt(CallLog.Calls. TYPE)));

Upvotes: 0

Views: 139

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191738

CallLog.Calls.TYPE is a column name, not an integer, so it cannot be parsed as one.

According to this line, your database is holding a string for the type, as well, not a number.

cons.setType(cr.getString(cr.getColumnIndex(CallLog.Calls.TYPE)));

If you want CallLogData to store the string value of the type, you should move the switch case up and maybe use getInt from the database instead, since that is what it appears that the type is... Although, you should refer to your CREATE TABLE statement to verify that.

if (cr.moveToLast()) {
    int numIdx = cr.getColumnIndex(CallLog.Calls.NUMBER);
    int typeIdx = cr.getColumnIndex(CallLog.Calls.TYPE);

    for (int i = 0; i < cr.getCount(); i++) {
        CallLogData cons = new CallLogData();
        cons.setNumber(cr.getString(numIdx));

        String type;
        int callType = cr.getInt(typeIdx);
        switch (callType) {
            case CallLog.Calls.OUTGOING_TYPE:
                type = "OUTGOING";
                break;
            case CallLog.Calls.INCOMING_TYPE:
                type = "INCOMING";
                break;
            case CallLog.Calls.MISSED_TYPE:
                type = "MISSED";
                break;
            default:
                type = "UNKNOWN";
        }
        cons.setType(type);
    }

If the database column really is a string, then you'd want this

Integer.parseInt(cr.getString(typeIdx));

Also, if you've inserted/created database values yourself, then you could change the type integer to a string value then instead of at query time.

Upvotes: 1

Related Questions