Android的 - 分配和检索的ID动态 [英] Android - assign and retrieve ID's dynamically

查看:87
本文介绍了Android的 - 分配和检索的ID动态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何assing ID的动态调用 SETID()。为ID的唯一,我曾经使用 ids.xml ,并传递给 SETID()的ID从$ p $编号的p-产生的游泳池。

问1:有什么办法来分配的ID不利用 ids.xml ,因为我无法预料,我会多少ID的需要运行时?

我试图绕过第一个问题presented中的问题1 的通过动态分配每个其中根据它的标签的散列的id(每个标签是唯一的),但是没有办法来gaurantee该ID的不会与 R.java

生成的ID的汽车碰撞被

问1.1:如何的ID命名冲突能够得到解决。

问题2:假设我有ID值的分配我和动态生成。由于前述方式ID不 R.id findViewById出现()将不适用于检索视图。因此,如何能在视图中检索时的ID是已知的?

答2:您会能够后才的onCreate()返回控制来获取其相应的ID视图(终止)


解决方案

  

有什么办法来分配的ID不使用,因为我的ids.xml
  无法预测有多少的ID我需要在运行时?


这保证每个视图都有一个唯一的ID

 的for(int i = 0; I< yourIDcount;我++){
yourView.setId(ⅰ);
}


  

如何能当ID被称为视图进行检索?


  View.findViewById(yourView.getId());

可以用来让你的视图的ID,因为每个视图都有一个唯一的ID,你可以回到你想要的视图。

这是在运行时创建的,因为你指定的的onCreate它被指定为意见的ID,因为的onCreate只调用一次创建活动这个词动态的手段,可以确保该ID您分配保持不变。 ..

I know how to assing ID's dynamically by invoking setID(). For the ID's to be unique, I used to utilize ids.xml and pass to setID() ID's from the pre-generated pool of ID's.

Question 1: Is there any way to assign ID's without utilizing the ids.xml since I cannot anticipate how many ID's I will need in runtime?

I tried to bypass the first issue presented in Question 1 by dynamically assigning each of which an id based on its label's hash (each label is unique), but there is no way to gaurantee that ID's won't be colliding with ID's auto generated in R.java.

Question 1.1: How the ID naming collision can be resolved?

Question 2: Assume I have the ID value of which I assign and generate dynamically. Since the aformentioned ID does not appear in R.id, findViewById() won't be applicable for retrieving the view. Hence, how can the view be retrieved when the ID is known?

Answer 2: You'd be able to retrieve the view by its corresponding ID only after onCreate() has returned control (terminated).

解决方案

Is there any way to assign ID's without utilizing the ids.xml since I cannot anticipate how many ID's I will need in runtime?

This guarantees that every view has a unique ID

for(int i =0 ; i < yourIDcount ; i++){
yourView.setId(i);
}

how can the view be retrieved when the ID is known?

View.findViewById(yourView.getId());

can be used to get your view's id, since every view has a unique Id you can get back the view you wanted..

The word dynamic means which is created at runtime, since you assign id in onCreate it is assigned as the views id, since onCreate is called only once an activity is created, you can make sure that the id you assigned stays intact...

这篇关于Android的 - 分配和检索的ID动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆