【发布时间】:2017-06-21 15:20:40
【问题描述】:
我正在为线性布局动态创建按钮,单击按钮后它会转到新活动。我想传递一个字符串,其中包含有关使用该活动作为 putExtra 单击了哪个按钮的信息。由于某种原因,我添加到每个按钮 onClickListener 的意图被覆盖,因此它只发送最后一个按钮的字符串,而不是被点击的那个:
LinearLayout l = (LinearLayout) findViewById(R.id.allOptions);
for(int i=0; i<currentOptions.size(); i++){
Button newButton = new Button(this);
SortingGroup s = currentOptions.get(i);
newButton.setText(s.getName());
sortGroupName = s.getName();;
newButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(),CategorySelector.class);
intent.putExtra("sorting_category_name",sortGroupName);
startActivity(intent);
}
});
l.addView(newButton);
}
【问题讨论】: