动态地增加孩子的LinearLayout与得到每个孩子的位置 [英] Dynamically adding a child to LinearLayout with getting each child's position

查看:619
本文介绍了动态地增加孩子的LinearLayout与得到每个孩子的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有越来越的的的LinearLayout 的孩子的位置的问题。首先我动态地添加了许多按钮,然后我试图返回每个子项的索引并显示它成的的TextView 的。在这里,我分享code:

Java源代码

 私有String []类别;

私人的LinearLayout LL;
私人TextView的电视;

公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    。类别= getResources()getStringArray(R.array.categories);

    电视=(TextView中)findViewById(R.id.text);
    LL =(的LinearLayout)findViewById(R.id.hsvLinearLayout);

    的for(int i = 0; I< categories.length;我++){
        按钮BTN =新的按钮(这一点);
        btn.setText(类[I]);
        btn.setOnClickListener(buttonClick);
        ll.addView(BTN);
    }
}

OnClickListener buttonClick =新OnClickListener(){
    公共无效的onClick(视图v){
        tv.setText(ll.indexOfChild(ⅴ));
    }
};
 

XML结构

 <的LinearLayout
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:方向=横向
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
>

< Horizo​​ntalScrollView
    机器人:ID =@ + ID /页脚
    机器人:方向=横向
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:滚动条=无
    机器人:fadingEdge =无
    >

    <的LinearLayout
        机器人:ID =@ + ID / hsvLinearLayout
        机器人:方向=横向
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        >

    < / LinearLayout中>

< / Horizo​​ntalScrollView>

<的TextView
    机器人:ID =@ + ID /文
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    />
 

资源

 <字符串名称=今天>今天< /串>
<字符串名称=生命>生活< /串>
<字符串名称=角落>角< /串>
<字符串名称=银行>银行与LT; /串>
<字符串名称=它>其< /串>
<字符串名称=好玩>娱乐与LT; /串>

<数组名=类别>
    <项目> @字符串/现今LT; /项目>
    <项目> @字符串/生活< /项目>
    <项目> @字符串/拐角LT; /项目>
    <项目> @字符串/银行< /项目>
    <项目> @字符串/它与LT; /项目>
    <项目> @字符串/乐趣和LT; /项目>
< /阵列>
 

的动态添加是好的,但方式我设置的 OnClickListener 的产生一些错误。任何帮助将是有益的!那其目的是,如果我想添加一个或多个按钮(S)到的 Horizo​​ntalScrollView 的不是需要编辑多个文件,只是去为 sting.xml 并创建一个新的项目进入的类阵列的!

这是什么 LogCat中生产的:

  07-12 22:37:17.680:信息/的System.out(331):等待调试器来解决?
07-12 22:37:17.900:信息/的System.out(331):调试器已落户(1441)
07-12 22:37:20.870:信息/ ActivityManager(61):显示活动com.test / .TestDynam:10203毫秒(共10203毫秒)
07-12 22:37:25.552:WARN /的ResourceType(331):无包装标识中获得价值的资源号00000000时,
07-12 22:37:26.871:DEBUG / dalvikvm(132):GC释放190对象/在901ms 8976字节
 

要澄清 - 该应用程序启动时没有任何错误,但是当我点击一个按钮,它会产生这条线从上面的日志:

  07-12 22:37:25.552:WARN /的ResourceType(331):无包装标识中获得价值的资源号00000000时,
 

这是调试

  TestDynam [Android应用程序]
    DalvikVM [本地主机:8611]
        螺纹并[d 3的密度;主](吊(除资源$ NotFoundException))
            ViewRoot.handleMessage(消息)线:1704
            的ViewRoot(处理器).dispatchMessage(消息)线:99
            Looper.loop()线:123
            ActivityThread.main(字符串[])线:4203
            Method.invokeNative(对象,对象[],上课,下课[],类,整型,布尔)行:不可用[本地方法]
            Method.invoke(对象,对象...)线:521
            ZygoteInit $ MethodAndArgsCaller.run()线:791
            ZygoteInit.main(字符串[])线:549
            NativeStart.main(字符串[])行:不可用[本地方法]
        螺纹并[d 13取代;捆扎线2](运行)
        螺纹并[d 11是烷基;捆扎线#1](运行)
 

解决方案

你的问题是资源.... 您使用的setText(int)方法(即指数为int ...)看起来对资源,而不是字符串,这是不是StringBuilder的,而您可以抛出任何类型的,并得到一个字符串。 你需要更换

  

tv.setText(ll.indexOfChild(V));

  

tv.setText(Integer.toString(ll.indexOfChild(ⅴ)));

如果你想效率甚至一点:

 公共类TestActivity延伸活动{

    私有String []类别;

    私人的LinearLayout LL;
    私人TextView的电视;

    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        。类别= getResources()getStringArray(R.array.categories);

        电视=(TextView中)findViewById(R.id.text);
        LL =(的LinearLayout)findViewById(R.id.hsvLinearLayout);

        的for(int i = 0; I< categories.length;我++){
            按钮BTN =新的按钮(这一点);
            btn.setText(类[I]);
            btn.setOnClickListener(buttonClick);
            ll.addView(BTN);
            INT IDX = ll.indexOfChild(BTN);
            btn.setTag(Integer.toString(IDX));
        }
    }

    OnClickListener buttonClick =新OnClickListener(){
        公共无效的onClick(视图v){
            字符串idxStr =(字符串)v.getTag();
            tv.setText(idxStr);
        }
    };

}
 

I have a problem with getting the child's position of LinearLayout. First I'm adding dynamically a number of buttons and then I'm trying to return each child's index and display it into a TextView. Here I'm sharing code:

java source:

private String[] categories;

private LinearLayout ll;
private TextView tv;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    categories = getResources().getStringArray(R.array.categories);

    tv = (TextView) findViewById(R.id.text);
    ll = (LinearLayout) findViewById(R.id.hsvLinearLayout);

    for(int i = 0; i < categories.length; i++) {
        Button btn = new Button(this);
        btn.setText(categories[i]);
        btn.setOnClickListener(buttonClick);
        ll.addView(btn);
    }
}

OnClickListener buttonClick = new OnClickListener() {
    public void onClick(View v) {
        tv.setText(ll.indexOfChild(v));
    }
};

xml structure:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<HorizontalScrollView
    android:id="@+id/Footer"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scrollbars="none"
    android:fadingEdge="none"
    >

    <LinearLayout
        android:id="@+id/hsvLinearLayout"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >

    </LinearLayout>

</HorizontalScrollView>

<TextView
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />

resources:

<string name="today">Today</string>
<string name="life">Life</string>
<string name="corner">Corner</string>
<string name="banks">Banks</string>
<string name="it">IT</string>
<string name="fun">Fun</string>

<array name="categories">
    <item>@string/today</item>
    <item>@string/life</item>
    <item>@string/corner</item>
    <item>@string/banks</item>
    <item>@string/it</item>
    <item>@string/fun</item>
</array>

The dynamically adding is fine, but the way I'm setting the OnClickListener produce some error. Any help will be useful! The purpose of that is if I want to add one or more button(s) to the HorizontalScrollView not to be necessary to edit many files, and just to go to sting.xml and create a new item into the categories-array!

This is what LogCat produced:

07-12 22:37:17.680: INFO/System.out(331): waiting for debugger to settle...
07-12 22:37:17.900: INFO/System.out(331): debugger has settled (1441)
07-12 22:37:20.870: INFO/ActivityManager(61): Displayed activity com.test/.TestDynam: 10203 ms (total 10203 ms)
07-12 22:37:25.552: WARN/ResourceType(331): No package identifier when getting value for resource number 0x00000000
07-12 22:37:26.871: DEBUG/dalvikvm(132): GC freed 190 objects / 8976 bytes in 901ms

To clarify - the application starts without any errors, but when I click on a button it produces this line from the above log:

07-12 22:37:25.552: WARN/ResourceType(331): No package identifier when getting value for resource number 0x00000000

And this is from debugger:

TestDynam [Android Application] 
    DalvikVM[localhost:8611]    
        Thread [<3> main] (Suspended (exception Resources$NotFoundException))   
            ViewRoot.handleMessage(Message) line: 1704  
            ViewRoot(Handler).dispatchMessage(Message) line: 99 
            Looper.loop() line: 123 
            ActivityThread.main(String[]) line: 4203    
            Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
            Method.invoke(Object, Object...) line: 521  
            ZygoteInit$MethodAndArgsCaller.run() line: 791  
            ZygoteInit.main(String[]) line: 549 
            NativeStart.main(String[]) line: not available [native method]  
        Thread [<13> Binder Thread #2] (Running)    
        Thread [<11> Binder Thread #1] (Running)

解决方案

your problem is resources.... you use the setText(int) method (that index is int...) which looks for resources and not string, this is not StringBuilder for which you can throw any type and get a string. you needs to replace

tv.setText(ll.indexOfChild(v));

with

tv.setText(Integer.toString(ll.indexOfChild(v)));

and if you want even little bit of efficiency:

public class TestActivity extends Activity {

    private String[] categories;

    private LinearLayout ll;
    private TextView tv;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        categories = getResources().getStringArray(R.array.categories);

        tv = (TextView) findViewById(R.id.text);
        ll = (LinearLayout) findViewById(R.id.hsvLinearLayout);

        for(int i = 0; i < categories.length; i++) {
            Button btn = new Button(this);
            btn.setText(categories[i]);
            btn.setOnClickListener(buttonClick);
            ll.addView(btn);
            int idx = ll.indexOfChild(btn);
            btn.setTag(Integer.toString(idx));
        }
    }

    OnClickListener buttonClick = new OnClickListener() {
        public void onClick(View v) {
            String idxStr = (String)v.getTag();
            tv.setText(idxStr);
        }
    };

}

这篇关于动态地增加孩子的LinearLayout与得到每个孩子的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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