小编Bal*_*des的帖子

在循环中按名称访问变量

我正在开发一个Android项目,我有很多可绘制的东西.这些绘图资源都命名为喜欢的icon_0.png,icon_1.png... icon_100.png.我想将这些drawable的所有资源id添加到Integers的ArrayList中.(对于那些不了解android的人,只有Java,我在谈论静态变量,在类的静态内部类中,就像R.drawable.icon_0.所有这些静态变量都是整数.)

有没有更有效的方法来做到这一点,而不是逐个添加它们?喜欢

ArrayList<Integer> list = new ArrayList<Integer>();
list.add(R.drawable.icon_1);
list.add(R.drawable.icon_2);
...
list.add(R.drawable.icon_100);
Run Code Online (Sandbox Code Playgroud)

我可以以某种方式循环它们吗?喜欢

for(int i=0; i<100; i++)
{
    list.add(R.drawable.icon_+i);  //<--- I know this doesn't work.
}
Run Code Online (Sandbox Code Playgroud)

我无法控制这些静态整数所在的文件,并且我无法在运行时创建drawable.

任何帮助,将不胜感激!

编辑

好吧,我读了答案,但我有一个主要问题:我无法访问任何Context需要创建此数组/ id列表的实例(我在静态初始化块中执行),因此getResources()方法,建议的两个答案不会起作用.有没有其他方法这样做?

java android

8
推荐指数
1
解决办法
1118
查看次数

angularJS中的全局函数

我是angularJS的新手.那么需要有关如何在角度项目中添加全局函数的帮助?加载我的角度应用程序的文件是使用showScrollAndGo函数,它应该作为全局方法工作,但它不起作用.app.js的代码是:

'use strict'; define(
[
    'angular',
    'jQuery',
    'angular-route',
    'angular-resource',
    'angular-ui-bootstrap',
    'highcharts',
    'highcharts-theme',
    'highcharts-ng',
    'controllers/index',
    'directives/index',
    'filters/index',
    'services/index',
    'angular-token-auth',
    'angular-local-storage',
    'jquery.slimscroll',
    'jquery-icheck'
],
function(angular, $) {
    'use strict';
    return angular.module('radian', ['ngRoute', 'ngResource', 'ui.bootstrap', 'app.services', 'app.controllers', 'app.directives','app.filters', 'highcharts-ng', 'ng-token-auth', 'ngStorage'])
        .constant('globals', {
            API_URL: 'http://localhost:3000/api',
            AUTH_URL: 'http://radiancor-env.elasticbeanstalk.com',
            TEMPLATE_URL: 'app/views'
        })
        .constant('pagination', {
            items_per_page: 10,
            max_size: 5,
            direction_links: true,
            boundary_links: true,
            current_page: 1,
            total_items: 0
        })
        .config(['$routeProvider', 'globals', routeConfiguration])
        .config(['$httpProvider', httpConfiguration])
        .config(['$authProvider', 'globals', authProvider])
        .config(['$rootScopeProvider', root_functions])
        .config(['paginationConfig', paginationConfiguration]);

    function authProvider($authProvider, globals) {
        $authProvider.configure({
            apiUrl: …
Run Code Online (Sandbox Code Playgroud)

javascript angularjs angular-ui angularjs-scope

6
推荐指数
1
解决办法
2万
查看次数

JSON模式引用文档中的另一个元素

Is there a way to express reference to another element in the same JSON document using JSON schema? The title might be a bit confusing, but i'm not looking for the "$ref" attribute, which references another type, but I'm curious, if there is a way, to reference another element in the document using a specified field. I know this is possible to enforce using xsd for xml documents, not sure about JSON.

I want to do something like this: …

json jsonschema

6
推荐指数
1
解决办法
1552
查看次数

Chai.js比较数组而不考虑顺序

我想要做的是使用chai.js比较2个基元数组,但不考虑元素的顺序 - 比如它们是2组.

现在显然我可以这样做:

const actual = ['a', 'b', 'c'];
const expected = ['b', 'c', 'a'];
expect(actual).to.have.length(expected.length);
expected.forEach(e => expect(actual).to.include(e));
Run Code Online (Sandbox Code Playgroud)

但我很好奇是否有一种首选的"内置"方式(我在文档中找不到它).

javascript chai

6
推荐指数
2
解决办法
1241
查看次数

Typescript 类型任意数量的通用字段

首先,我不能 100% 确定我想做的事情是否可能,但我尝试描述最好的可能。

interface Property<T> {
  value: T
}

interface PropertyBag {
  [key: string]: Property<???>
}

function toPlainObject(props: PropertyBag): ??? {
  return Object.keys(props)
    .reduce((acc, key) => Object.assign(acc, { [key]: props[key].value }), {})
}
Run Code Online (Sandbox Code Playgroud)

一个演示我想做的事情的例子:

interface Person {
  name: string
  age: number
}

const name: Property<string> = { value: 'John Doe' }
const age: Property<number> = { value: 35 }

const props: PropertyBag = { name, age }

const person: Person = toPlainObject(props)
Run Code Online (Sandbox Code Playgroud)

我想知道的是我怎样才能输入toPlainObjectand 的返回类型PropertyBag(其中类型是???)。使用 TS 是否可以实现这一点?

typescript

6
推荐指数
1
解决办法
1284
查看次数

使用 typescript 编译器 API 获取接口字段类型信息

考虑以下接口:

interface X { 
  x: string 
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用 typescript 编译器 API 来获取 propertyx的类型。这是我到目前为止所拥有的:

import {
  PropertySignature,
  createSourceFile,
  ScriptTarget,
  ScriptKind,
  SyntaxKind,
  InterfaceDeclaration,
  Identifier,
} from 'typescript'

describe('Compiler test', () => {
  it('should be able to find type information about X.x', () => {
    const sourceText = 'interface X { x: string }'
    const ast = createSourceFile('source.ts', sourceText, ScriptTarget.ES5, false, ScriptKind.TS)
    const interfaceX = ast
      .getChildAt(0)
      .getChildren()
      .find((child) => child.kind === SyntaxKind.InterfaceDeclaration) as InterfaceDeclaration
    const propX = interfaceX.members.find((member) => (member.name …
Run Code Online (Sandbox Code Playgroud)

abstract-syntax-tree typescript typescript-compiler-api

6
推荐指数
1
解决办法
1542
查看次数

这两种原型继承实现之间的区别是什么?

这两个原型继承实现之间有什么区别,并且考虑到我们正在使用2个不同的"原型"(仅在函数和内部原型上的原型属性),以及这些实现在原型链查找中有何不同?另外,第一个实现(使用prototype属性)是否依赖于我们对new运算符的使用?

分配给函数的prototype属性并使用new运算符:

function foo() {}

foo.prototype.output = function(){
   console.log('inherits from Function.prototype property');
};

bar = new foo();
bar.output();
Run Code Online (Sandbox Code Playgroud)

将函数存储在对象文字中并使用该Object.create()方法:

var foo = {
  output: function(){
    console.log('inherits from the internal prototype');
  }
};

var bar = Object.create(foo);
bar.output();
Run Code Online (Sandbox Code Playgroud)

javascript inheritance prototype prototypal-inheritance

5
推荐指数
1
解决办法
118
查看次数

Javascript功能

有这样的JavaScript代码:

function a() { 
  a = 3; 
  return a;
}
console.log(a());
console.log(a());
Run Code Online (Sandbox Code Playgroud)

执行后打印出:3,输入错误.请有人解释原因

javascript function console.log

5
推荐指数
1
解决办法
93
查看次数

如何在javascript中下载希伯来语csv文件?

我正在 Chrome 扩展上使用 javascript 尝试希伯来语 csv 文件。

\n\n

但我得到了失败的字符串。

\n\n

在此输入图像描述

\n\n

这是我的代码片段。

\n\n
var csv = "Phone\\n\xd7\x90\xd7\xa0\xd7\x99 \xd7\x90\xd7\x95\xd7\x94\xd7\x91 \xd7\x90\xd7\x95\xd7\xaa\xd7\x9a\\n";\nvar file = new Blob([csv], {type: \'text/csv;utf-8\'});\nvar url = URL.createObjectURL(file);\nchrome.downloads.download({\n    url: url,\n    filename: name\n}, function() {\n    URL.revokeObjectURL(msg.url);\n});\n
Run Code Online (Sandbox Code Playgroud)\n

javascript google-chrome

5
推荐指数
1
解决办法
884
查看次数

键入除 X 之外的任何数字

总而言之:

这种类型在 TS 中可能存在吗?Exclude<number, 200 | 400>(“除 200 或 400 之外的任何数字”)

我有以下用例。我有一个通用的响应类型:

type HttpResponse<Body = any, StatusCode = number> = {
  body: Body
  statusCode: StatusCode
}
Run Code Online (Sandbox Code Playgroud)

我想使用状态代码作为鉴别器:

// Succes
type SuccessResponse = HttpResponse<SomeType, 200>
// Known error
type ClientErrorResponse = HttpResponse<ClientError, 400>
// Anything else, generic error, issue is with the status code here.
type OtherErrorResponse = HttpResponse<GenericError, Exclude<number, 200 | 400>>

// The response type is a union of the above
type MyResponse = SuccessResponse | ClientErrorResponse …
Run Code Online (Sandbox Code Playgroud)

generics discriminator typescript typescript-typings

5
推荐指数
1
解决办法
1011
查看次数