使用意图数组android

时间:2017-06-25 16:14:49

标签: java android android-intent

我的应用程序中有大约50个活动,我有一个算法,在超级活动中以按钮的形式显示10个这样的活动的标题,并为每个包含intent的按钮设置onclicklistener并调用具体活动。我尝试通过一系列意图来做到这一点,但我没有成功。有关如何执行此操作的任何建议?

package plkk.developers.com.livfit;

// this is my string which contains name of activities

final String ActivityIdMen[] = { "Deadlift", "Pushups", "Barbell_Bench", "Military_Press", "Barbell_Curl", "Close_Bench", "Seated_Cable", "Chinup", "Overhead_Press",
            "Power_Clean", "Jumping_Rope", "Hiit", "Barbell_Bench", "Deadlift", "Lat_Pulldown", "Barbell_Curl", "Skull_Crusher", "Diamond_Dips", "Squats",
            "Hill_Running", "Jumping_Rope", "Stationary_Bike", "Hiit", "Chinup", "Torso_Rotation", "Prone_Plank", "Medicine_Squat", "Front_Squat"
    };

// this is a fragment of the algorithm where I need help

            if(BMI<18.5){
                for(i=0;i<=8;i++) {
                    Button btn = new Button(this);
                    LinearLayout.LayoutParams P = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    P.weight = 1;
                    btn.setLayoutParams(P);
                    btn.setText(ActivityTextMen[i]);
                    btn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Class clas = null;
                            try{
                                clas = Class.forName("plkk.developers.com.livfit."+ActivityIdMen[i]);
                            }catch (ClassNotFoundException c){
                                c.printStackTrace();
                            }
                            if (clas!=null) {
                                Intent intent = new Intent(view.getContext(), clas);
                                startActivity(intent);
                            }
                        }
                    });
                    ll.addView(btn);
                }

// the intent always directs me to the class at i=9 (in the above case. I tried solving it by using array of intents but couldn't do that properly. 

1 个答案:

答案 0 :(得分:1)

取下重量分配器。 您是否在清单文件中声明了您的活动?

<强>更新

尝试使用索引设置标记。然后使用按钮标记的值来获取值。

if(BMI<18.5){
        for(i=0;i<=8;i++) {
            Button btn = new Button(this);
            LinearLayout.LayoutParams P = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            P.weight = 1;
            btn.setTag(i);
            btn.setLayoutParams(P);
            btn.setText(ActivityTextMen[i]);
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Class clas = null;
                    try{
                        clas = Class.forName("plkk.developers.com.livfit."+ActivityIdMen[Integer.parseInt(""+btn.getTag())]);
                    }catch (ClassNotFoundException c){
                        c.printStackTrace();
                    }
                    if (clas!=null) {
                        Intent intent = new Intent(view.getContext(), clas);
                        startActivity(intent);
                    }
                }
            });
            ll.addView(btn);
        }