当前位置: 首页 > 编程笔记 >

Android 导入资源中定义的对象数组

麻宜春
2023-03-14
本文向大家介绍Android 导入资源中定义的对象数组,包括了Android 导入资源中定义的对象数组的使用技巧和注意事项,需要的朋友参考一下

示例

在某些情况下,需要在应用程序的资源中创建和定义自定义对象。这样的对象可以由Java简单类型,例如Integer,Float,String。

这是如何导入在应用程序资源中定义的对象的示例。该对象包含Category3个类别的属性:

  • ID

  • 颜色

  • 名称

这POJO在categories.xml文件中具有等效性,其中每个数组具有为每个类别定义的相同属性。

  1. 为您的对象创建一个模型:

public class Category {
    private Type id;
    private @ColorRes int color;
    private @StringRes String name;

    public Category getId() {
        return id;
    }

    public void setId(Category id) {
       this.id= id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
       this.name= name;
    }

    public int getColor() {
        return color;
    }

    public void setColor(int color) {
       this.color= color;
    }

    public enum Type{
        REGISTRATION,
        TO_ACCEPT,
        TO_COMPLETE,
        TO_VERIFY,
        CLOSED
    }
}

  1. 在文件res/values夹中创建文件:

categories.xml

  1. 组成由资源组成的每个模型:

<array name="no_action">
    <item>0</item>
    <item>@android:color/transparent</item>
    <item>@string/statusRegistration</item>
</array>
<array name="to_accept">
    <item>1</item>
    <item>@color/light_gray</item>
    <item>@string/acceptance</item>
</array>
<array name="opened">
    <item>2</item>
    <item>@color/material_green_500</item>
    <item>@string/open</item>
</array>
<array name="to_verify">
    <item>3</item>
    <item>@color/material_gray_800</item>
    <item>@string/verification</item>
</array>
<array name="to_close">
    <item>4</item>
    <item>@android:color/black</item>
    <item>@string/closed</item>
</array>

  1. 在资源文件中定义一个数组:

    <array name="categories">
       <item>@array/no_action</item>
       <item>@array/to_accept</item>
       <item>@array/opened</item>
       <item>@array/to_verify</item>
       <item>@array/to_close</item>
    </array>
  2. 创建一个函数来导入它们:

    @NonNull
    public List<Category> getCategories(@NonNull Context context) {
    final int DEFAULT_VALUE = 0;
    final int ID_INDEX = 0;
    final int COLOR_INDEX = 1;
    final int LABEL_INDEX = 2;
    if (context == null) {
       return Collections.emptyList();
    }
    // 从`tasks_categories`数组中获取对象数组
    TypedArray statuses = context.getResources().obtainTypedArray(R.array.categories);
    if (statuses == null) {
       return Collections.emptyList();
    }
    List<Category> categoryList = new ArrayList<>();
    for (int i = 0; i < statuses.length(); i++) {
       int statusId = statuses.getResourceId(i, DEFAULT_VALUE);
       // 获取一个对象的属性
       TypedArray rawStatus = context.getResources().obtainTypedArray(statusId);
       Category category = new Category();
       int id = rawStatus.getInteger(ID_INDEX, DEFAULT_VALUE);
      Category.TypecategoryId;
       //ID应该使用`Category.Type`保持顺序。
       switch (id) {
           case 0:
               categoryId = Category.Type.REGISTRATION;
               break;
           case 1:
               categoryId = Category.Type.TO_ACCEPT;
               break;
           case 2:
               categoryId = Category.Type.TO_COMPLETE;
               break;
           case 3:
               categoryId = Category.Type.TO_VERIFY;
               break;
           case 4:
               categoryId  = Category.Type.CLOSED;
               break;
           default:
               categoryId = Category.Type.REGISTRATION;
               break;
       }
       category.setId(categoryId);
       category.setColor(rawStatus.getResourceId(COLOR_INDEX, DEFAULT_VALUE));
       int labelId = rawStatus.getResourceId(LABEL_INDEX, DEFAULT_VALUE);
       category.setName(getString(context.getResources(), labelId));
       categoryList.add(taskCategory);
    }
    return taskCategoryList;
    }
 类似资料:
  • 尝试构建ARM模板以部署多个VM。但是,模板验证失败,并显示以下错误消息 部署模板验证失败:“资源”为Microsoft。模板中未定义“网络/网络接口/sqlnodeNic”。请看https://aka.ms/arm-template有关用法的详细信息。

  • Unity will automatically detect files as they are added to your Project folder's Assets folder. When you put any asset into your Assets folder, you will see the asset appear in your Project View. Unit

  • 在一个AnyLogic项目中,在“抓住”块中,我需要从资源集中自定义地选择资源。在“抢占”的属性选项卡中阻止字段“resource Sets”,选中值和标志“Customize resource Choice”。在“资源选择条件”代码部分,我需要做出如下选择: 我如何检查一个资源单元是否来自给定的资源池,然后根据资源的特性区分它们?谢谢你。最诚挚的问候。

  • 以下模板部署: https://gist.github.com/rnkhouse/aea0a8fd395da37b19466348b919d620 错误: “部署失败,状态代码为400,消息:部署模板验证失败:'模板中未定义资源'Microsoft.Network/virtualNetworks/mtes dev VNET'。请参阅。”https://aka.ms/arm-template有关用法

  • Kubernetes 中的各个 Object 的配置指南。

  • 文:youyou 目前 Cocos Creator 支持导入的 3D 模型格式为使用非常广泛的 .fbx,基本上 3D 建模软件都支持导出这种格式。 导入的流程很简单,只需要将 .fbx 模型资源拖入到 资源管理器,等待片刻即可完成导入工作。导入完成后,在 资源管理器 中看到导入后的模型资源是一个可以展开的文件夹,导入模型的时候编辑器会自动解析模型的内容,并生成 Prefab、网格、骨骼动画 等资