Find the resource id for the views located on the layout - Android User Interface

Android examples for User Interface:Layout

Description

Find the resource id for the views located on the layout

Demo Code

/*/*from ww w .  j a  v  a2s  .  c  o m*/
 * Copyright (C) 2013 Blandware (http://www.blandware.com)
 *
 * 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.
 */
//package com.java2s;
import android.content.Context;

public class Main {
    /**
     * Find the resource id for the views located on the layout
     * @param fieldNames column names of the DB table
     * @return array of the layout resource id
     */
    public static int[] getLayoutViewIds(Context context,
            String[] fieldNames) {
        if (fieldNames == null || fieldNames.length == 0)
            return null;

        int[] layoutViewIds = new int[fieldNames.length];
        for (int i = 0; i < fieldNames.length; i++) {
            String fieldName = fieldNames[i];
            int id = context.getResources().getIdentifier(fieldName, "id",
                    context.getPackageName());
            layoutViewIds[i] = id;
        }

        return layoutViewIds;
    }
}

Related Tutorials