0

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)));
6
  • 1
    I'd bet that CallLog.Calls. TYPE is the string "type", so Integer.parseInt("type") throws exception. Perhaps the second Integer.parseInt call in that line should be removed? Commented Oct 28, 2019 at 4:11
  • yes CallLog.Call.Type is the string"type" . I have tried by removing second Integer.parseInt but it is showing an error Commented Oct 28, 2019 at 4:16
  • Can you try this int dircode = Integer.parseInt(cr.getString(cr.getColumnIndex(CallLog.Calls.TYPE))); ? Commented Oct 28, 2019 at 4:47
  • Its not working Commented Oct 28, 2019 at 5:33
  • @yoyohoney What's the error?. Commented Oct 28, 2019 at 5:36

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

it is showing an error on this Line cons.setType(type);
Well, I did not test the code, and I don't see your CallLogData class, so what is the error?
Well, you should probably show your full list adapter code then and add more logging statements to debug the issue... The main point here is that it actually compiled

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.