使用for循环为id分配不同的按钮名称

时间:2017-10-26 09:28:18

标签: android button

我有5 buttons,我需要明确创建每个editText。我通过使用for循环找到了一个解决方案,我们可以将按钮分组。如下。

private Button[] btn = new Button[4];
private int[] btn_id = {R.id.btn0, R.id.btn1, R.id.btn2, R.id.btn3}; 
 for(int i = 0; i < btn.length; i++){
        btn[i] = (Button) findViewById(btn_id[i]);
        btn[i].setBackgroundColor(Color.rgb(207, 207, 207));
        btn[i].setOnClickListener(this);
    }

这里我只需要使用相同的button。如何将上述代码用于不同的button名称?

 private Button btn1,button2,bt3,b4;

如何实现上述button声明的for循环。我尝试将所有button添加到一个不起作用的arrayList。有什么建议。

2 个答案:

答案 0 :(得分:1)

你可以尝试这种方式,希望这对你有帮助!

import scrapy
class ToScrapeSpiderXPath(scrapy.Spider):
    name = 'scrape-xpath'
    start_urls = [
        'http://explorer.eu/contents/food/28?utf8=/',
    ]

    def parse(self, response):
        for flv in response.xpath('//html/body/main/div[4]'):

            yield {
                'Titulo': flv.xpath('//*@id="chromatography"]/table/tbody/tr[3]/th/strong/a/text()"]/tbody/tr[5]/td[3]/a[2]').extract(),
            'contenido': flv.xpath('//*@id="chromatography"]/table/tbody/tr[5]/td[3]/a[2]/text()').extract(),
             'clase': flv.xpath('//*[@id="chromatography"]/table/tbody/tr[5]/td[1]/text()').extract(),
            'Subclase': flv.xpath('//*[@id="chromatography"]/table/tbody/tr[5]/td[2]/a/text').extract(),
        }

答案 1 :(得分:0)

for-loop arrayButtons填充private Button[] btn = new Button[4];

for-loop

然后你可以在private Button btn1 = btn[0]; private Button btn2 = btn[1]; private Button btn3 = btn[2]; private Button btn4 = btn[3];

之后执行此操作
2017-10-26 09:18:05,102 WARN org.apache.hadoop.hdfs.protocol.BlockStoragePolicy: Failed to place enough replicas: expected size is 2 but only 0 storage types can be selected (replication=2, selected=[], unavailable=[DISK], removed=[DISK, DISK], policy=BlockStoragePolicy{HOT:7, storageTypes=[DISK], creationFallbacks=[], replicationFallbacks=[ARCHIVE]})
2017-10-26 09:18:05,102 WARN org.apache.hadoop.hdfs.server.blockmanagement.BlockPlacementPolicy: Failed to place enough replicas, still in need of 2 to reach 2 (unavailableStorages=[DISK], storagePolicy=BlockStoragePolicy{HOT:7, storageTypes=[DISK], creationFallbacks=[], replicationFallbacks=[ARCHIVE]}, newBlock=true) All required storage types are unavailable:  unavailableStorages=[DISK], storagePolicy=BlockStoragePolicy{HOT:7, storageTypes=[DISK], creationFallbacks=[], replicationFallbacks=[ARCHIVE]}
2017-10-26 09:18:05,103 INFO org.apache.hadoop.ipc.Server: IPC Server handler 7 on 9000, call org.apache.hadoop.hdfs.protocol.ClientProtocol.addBlock from 192.168.1.6:52435 Call#7 Retry#0
java.io.IOException: File /spark/apps/spark-app.jar could only be replicated to 0 nodes instead of minReplication (=1).  There are 2 datanode(s) running and 2 node(s) are excluded in this operation.
    at org.apache.hadoop.hdfs.server.blockmanagement.BlockManager.chooseTarget4NewBlock(BlockManager.java:1571)
    at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.getNewBlockTargets(FSNamesystem.java:3107)
    at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.getAdditionalBlock(FSNamesystem.java:3031)
    at org.apache.hadoop.hdfs.server.namenode.NameNodeRpcServer.addBlock(NameNodeRpcServer.java:725)
    at org.apache.hadoop.hdfs.protocolPB.ClientNamenodeProtocolServerSideTranslatorPB.addBlock(ClientNamenodeProtocolServerSideTranslatorPB.java:492)
    at org.apache.hadoop.hdfs.protocol.proto.ClientNamenodeProtocolProtos$ClientNamenodeProtocol$2.callBlockingMethod(ClientNamenodeProtocolProtos.java)
    at org.apache.hadoop.ipc.ProtobufRpcEngine$Server$ProtoBufRpcInvoker.call(ProtobufRpcEngine.java:616)
    at org.apache.hadoop.ipc.RPC$Server.call(RPC.java:982)
    at org.apache.hadoop.ipc.Server$Handler$1.run(Server.java:2049)
    at org.apache.hadoop.ipc.Server$Handler$1.run(Server.java:2045)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAs(Subject.java:422)
    at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1698)
    at org.apache.hadoop.ipc.Server$Handler.run(Server.java:2043)

你可以用更好的方式做到这一点,但我希望这能为你提供一个如何做的小指示。