【发布时间】:2019-01-03 23:33:55
【问题描述】:
我想根据for循环条件在一个字符串中调用多个方法。
这段代码应该可以帮助你理解我在寻找什么:
public void onItemClick(View view, int position) {
for (count=0;count<=25;count++){
if (count==position){
String methodCall="A"+count+"List()";
A0List()=methodCall;
}
}
}
A0List(){
//Body
//when count=0 this method is called
}
A1List(){
//body
//when count=1 this method is called
}
// ...
A25List(){
//body
//when count=25 this method is called
}
我在我的 Android 应用上使用此代码来减少点击侦听器代码。
我正在寻找一种避免 if-else-if 阶梯循环的解决方案。
【问题讨论】:
-
您可以使用反射来实现,但我不会推荐它。相反,我相信您的
AXList()方法中的代码会非常相似,因此您应该只有一个方法并将数字作为参数传递。你能用其中几种方法显示代码来确认这一点吗? -
使用适配器将值放入另一个类或 Andorid 活动中
标签: java string android-studio for-loop methods