Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何为通过运行时创建的元素分配OnClickListener?_Android_Dynamic_Onclick_Onclicklistener - Fatal编程技术网

Android 如何为通过运行时创建的元素分配OnClickListener?

Android 如何为通过运行时创建的元素分配OnClickListener?,android,dynamic,onclick,onclicklistener,Android,Dynamic,Onclick,Onclicklistener,我的android应用程序要求我根据运行时获得的数据创建一些LinearLayout,因此我不知道它的编号,我必须将其放入for循环中才能创建它,因此分配给布局或其中元素的名称将随着每次迭代for循环而被覆盖,这就是我的代码: List<LinearLayout> inner_ver = new ArrayList<LinearLayout>(); for(int i = 0 ; i < size_from_run_time ; i+

我的android应用程序要求我根据运行时获得的数据创建一些LinearLayout,因此我不知道它的编号,我必须将其放入for循环中才能创建它,因此分配给布局或其中元素的名称将随着每次迭代for循环而被覆盖,这就是我的代码:

        List<LinearLayout> inner_ver = new ArrayList<LinearLayout>();
        for(int i = 0 ; i < size_from_run_time ; i++){
            LinearLayout temp_inner_ver = new LinearLayout(this);
            temp_inner_ver.setLayoutParams(temp_lay);
            temp_inner_ver.setOrientation(LinearLayout.VERTICAL);
            temp_inner_ver.setWeightSum(2);
            temp_inner_ver.setPadding(7, 7, 7, 7);
            inner_ver.add(temp_inner_ver);
        }

        for(int j = 0 ; j < inner_ver.size() ; j++){
            LinearLayout icon1 = new LinearLayout(this);
            inner_ver.get(j).addView(icon1);

            icon1.setLayoutParams(lp_icon);
            icon1.setBackgroundResource(R.drawable.ac_overlay);
            icon1.setOrientation(LinearLayout.HORIZONTAL);
            icon1.setTag(NORMAL);

            // icon1
            TextView text1 = new TextView(this);
            icon1.addView(text1);
            text1.setLayoutParams(text_name);
            text1.setText("something");
            text1.setTextSize(12);
            text1.setTextColor(Color.WHITE);

            ImageButton rgp1 = new ImageButton(this);
            icon1.addView(rgp1);
            rgp1.setLayoutParams(lp_ineer_ver);
            rgp1.setImageResource(R.drawable.grp);
            rgp1.setBackgroundColor(Color.TRANSPARENT);

            Button rgp_value1 = new Button(this);
            icon1.addView(rgp_value1);
            rgp_value1.setLayoutParams(lp_ineer_ver);
            rgp_value1.setText("something");
            rgp_value1.setTextSize(12);
            rgp_value1.setBackgroundColor(Color.TRANSPARENT);
            rgp_value1.setTextColor(Color.WHITE);

            ImageButton cool1 = new ImageButton(this);
            icon1.addView(cool1);
            cool1.setLayoutParams(lp_ineer_ver);
            cool1.setImageResource(Color.TRANSPARENT);
            cool1.setBackgroundColor(Color.TRANSPARENT);

            TextView cool_value1 = new TextView(this);
            icon1.addView(cool_value1);
            cool_value1.setLayoutParams(text_cool);
            cool_value1.setText("something");
            cool_value1.setTextSize(12);

            ver_rooms.addView(inner_ver.get(j)); // ver_rooms is a LinearLayout defined through the xml

        }   
List internal\u ver=new ArrayList();
for(int i=0;i

那么,如果我想为创建的项目(例如
rgb1
rgb\u value1
cool1
)添加一个
OnClickListener
,因为我可能有
internal\u ver
10,并且所有这些元素肯定都包含相同名称的所有元素,该怎么办

您可能会创建一个包含10个内部版本的数组或ArrayList。这里我假设,innerVerList是一个包含LinearLayout列表的数组

   for (LinearLayout l: list){
       for(int i=0; i<l.getChildCount(); ++i){
            l.getChildAt(i).addOnClickListner(/*name of onClick listener here*/ );
       }
    }
for(线性布局:列表){

对于(int i=0;iI不知道为什么,但当尝试它给我一个语法错误时,我没有得到类型不匹配:无法从元素类型LinearLayout转换为List
,正如您所知,
Internal\u ver
是一个布局列表,而不仅仅是一个布局它工作了,只需将
addOnClickListener
更改为
setOnClickListener