What is the difference between TypedArray.getInteger () and TypedArray.getInt ()?

Looking at the source code for TypedArray ( link ), I cannot understand what the difference is between the two methods. getInt() is basically the same as getInteger() , but with a little padding at the end that I don't understand. Can someone explain this to me?

The reason I need to know the difference is to implement a custom onGetDefaultValue() subclass and get the default value that I need to override onGetDefaultValue() , which captures an integer from TypedArray. Example:

 @Override protected Object onGetDefaultValue(TypedArray a, int index) { return a.getInteger(index, DEFAULT_VALUE); } 

Here I use getInteger() , but if getInt() better, I will use this.

+7
source share
1 answer

They just have different cases of "everything else." They both return int for valid int and defValue for null . The difference is how they handle the case of neither one nor the other.

From link :

 /* * Copyright (C) 2008 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ TypedValue v = mValue; if (getValueAt(index, v)) { Log.w(Resources.TAG, "Converting to int: " + v); return XmlUtils.convertValueToInt( v.coerceToString(), defValue); } Log.w(Resources.TAG, "getInt of bad type: 0x" + Integer.toHexString(type)); return defValue; 

This is the additional information you are talking about. It seems to convert the unknown value to a string and then to an integer. This can be useful if you saved "12" or some equivalent value.

Additionally, getInteger throws an exception if it is not null , not int . In contrast, if all else fails, getInt returns a default value.

Also note that their behavior in this odd case is different from the fact that calling one above the other would be incorrect in each case. The best solution is one that meets your expectations of failure.

+7
source

All Articles