Java 如何从循环中的onclick内的araylist获取ID
我知道也许有些人会投票否决我的问题,但我正在努力找到解决办法,却什么也找不到 我在循环中有一个arrayList,用于以编程方式显示一些按钮。在循环中,我有一个onCheckedChanged方法,在这个方法中,我尝试获取循环中元素的id 我的问题是,如何获取我正在显示的每个按钮的id。如果我尝试这样做:Java 如何从循环中的onclick内的araylist获取ID,java,android,for-loop,arraylist,Java,Android,For Loop,Arraylist,我知道也许有些人会投票否决我的问题,但我正在努力找到解决办法,却什么也找不到 我在循环中有一个arrayList,用于以编程方式显示一些按钮。在循环中,我有一个onCheckedChanged方法,在这个方法中,我尝试获取循环中元素的id 我的问题是,如何获取我正在显示的每个按钮的id。如果我尝试这样做: arrayList.get(i).getId(); 我无法访问变量I,因为它在循环中不是final,并且我无法放置final int I,因为我在I中得到了一个错误++ for (int
arrayList.get(i).getId();
我无法访问变量I,因为它在循环中不是final,并且我无法放置final int I,因为我在I中得到了一个错误++
for (int i = 0; i < arrayList.size(); i++) {
final ToggleButton toogleBtn = new ToggleButton (ActivityNew.this);
toogleBtn.setWidth(140);
toogleBtn.setHeight(20);
toogleBtn.setTextSize(12);
toogleBtn.setText(arrayList.get(i).getName());
toogleBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
toggleEnabled(toogleBtn);
arrayList.get(i).getId();
Toast.makeText(RegisterThreeActivityNew.this, Long.toString(idCategory), Toast.LENGTH_SHORT).show();
} else {
toggleDissabled(toogleBtn);
}
}
});
}
for(int i=0;i为什么不直接存储final int-id=arrayList.get(i.getId()代码>在侦听器之外
或者创建一个临时变量来保存i
:
final int index = i;
为什么不直接存储final int-id=arrayList.get(i).getId()代码>在侦听器之外
或者创建一个临时变量来保存i
:
final int index = i;
您不需要访问数组来获取按钮id,您可以直接访问它
toggleBtn.getId()
您是否尝试过这样访问它?您不需要访问阵列来获取按钮id,您可以直接访问它
toggleBtn.getId()
您是否尝试过这样访问它?尝试以下操作:
for (int i = 0; i < arrayList.size(); i++) {
final ToggleButton toogleBtn = new ToggleButton (ActivityNew.this);
toogleBtn.setWidth(140);
toogleBtn.setHeight(20);
toogleBtn.setTextSize(12);
toogleBtn.setText(arrayList.get(i).getName());
final long id = arrayList.get(i).getId();
toogleBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
toggleEnabled(toogleBtn);
id;
Toast.makeText(RegisterThreeActivityNew.this, Long.toString(idCategory), Toast.LENGTH_SHORT).show();
} else {
toggleDissabled(toogleBtn);
}
}
});
}
for(int i=0;i
试试这个:
for (int i = 0; i < arrayList.size(); i++) {
final ToggleButton toogleBtn = new ToggleButton (ActivityNew.this);
toogleBtn.setWidth(140);
toogleBtn.setHeight(20);
toogleBtn.setTextSize(12);
toogleBtn.setText(arrayList.get(i).getName());
final long id = arrayList.get(i).getId();
toogleBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
toggleEnabled(toogleBtn);
id;
Toast.makeText(RegisterThreeActivityNew.this, Long.toString(idCategory), Toast.LENGTH_SHORT).show();
} else {
toggleDissabled(toogleBtn);
}
}
});
}
for(int i=0;i
只需存储final int id=arrayList.get(i).getId()代码>在侦听器之外?不,不工作。我想要的是每次按下toggleButton时都能得到这个toggleButton的id。如果我在侦听器之外执行此操作,我总是获取loopjust存储的最后一个idfinal int id=arrayList.get(I).getId()代码>在侦听器之外?不,不工作。我想要的是每次按下toggleButton时都能得到这个toggleButton的id。如果我在监听器之外执行此操作,我总是会得到循环的最后一个id,您的意思是:final long id=arrayList.get(I).getId();toogleBtn.setOnCheckedChangeListener(新建CompoundButton.OnCheckedChangeListener(){public void onCheckedChanged(CompoundButton buttonView,布尔值isChecked){if(isChecked){toggleEnabled(toogleBtn);Toast.makeText(ActivityNew.this,Long.toString(id),Toast.LENGTH_SHORT).show();}else{toggledisabled(toogleBtn);}};您的意思是:final long id=arrayList.get(i).getId();toogleBtn.setOnCheckedChangeListener(新建CompoundButton.OnCheckedChangeListener(){public void onCheckedChanged(CompoundButton buttonView,布尔值isChecked){if(isChecked){toggleEnabled(toogleBtn);Toast.makeText(ActivityNew.this,Long.toString(id),Toast.LENGTH_SHORT).show();}else{toggledisabled(toogleBtn);}};