所以我正在开发一款Android应用,并试图以编程方式添加点击触发Dialog
的按钮。
当我将OnClickListener
添加到这些按钮时,我要么从最后添加的Button
获取文本,这是一个错误,声称在调用tv1.setText时存在IOOBException(... ) - 索引7无效,大小为7,或者只是没有传递。有没有人知道我如何设置每个Button
来为每个按钮创建一个包含唯一信息的新Dialog
?
提出问题的代码片段:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_streaks);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
prefs = getSharedPreferences("carter.streakly", Context.MODE_PRIVATE);
editor = prefs.edit();
db = new DatabaseHelper(this);
mTableLayout = (TableLayout) findViewById(R.id.all_streak_table);
res = db.getAllData();
if(res.getCount() ==0) {
//show message
showMessage("Error", "Nothing found");
return;
}
streakArrayList = new ArrayList<>();
int counter = 0;
while (res.moveToNext()){
streakArrayList.add(new Streak(Integer.parseInt(res.getString(0)), res.getString(1), res.getString(2), res.getString(3), Integer.parseInt(res.getString(4))));
counter++;
}
LinearLayout.LayoutParams btnParams = new LinearLayout.LayoutParams(200, 200);
btnParams.setMargins(200, 30, 80, 30);
LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ActionBar.LayoutParams.WRAP_CONTENT);
tvParams.setMargins(100, 0, 0, 0);
i = 0;
while (i < counter){
if(i%2==0){
mTableRow = new TableRow(this);
mTableLayout.addView(mTableRow);
}
ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
mTableRow.addView(ll);
Button btn = new Button(this);
btn.setText(""+streakArrayList.get(i).getDaysKept());
btn.setId(i);
btn.setBackground(getResources().getDrawable(R.drawable.round_button));
btn.setLayoutParams(btnParams);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Dialog dialog = new Dialog(AllStreaks.this);
dialog.setContentView(R.layout.activity_enlarged);
tv1 = (TextView) dialog.findViewById(R.id.activity_enlarge_icon);
tv1.setText(streakArrayList.get(i-1).getActivityName());
dialog.show();
/*
Intent intent = new Intent(AllStreaks.this, EnlargedActivity.class);
intent.putExtra("passName", streakArrayList.get(view.getId()).getActivityName());
startActivity(intent);*/
}
});
ll.addView(btn);
TextView tv = new TextView(this);
tv.setText(streakArrayList.get(i).getActivityName());
tv.setId(i);
tv.setGravity(Gravity.CENTER | Gravity.BOTTOM);
tv.setTextSize(20);
tv.setLayoutParams(tvParams);
ll.addView(tv);
i++;
}
}
public void showMessage(String title, String message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
答案 0 :(得分:0)
您的代码存在许多问题。
首先,您使用的是TableRow
和LinearLayout
的全局实例。这意味着你在循环的每次迭代时都会覆盖它们,所以最后只有最后一次迭代的值才会可见。
您为Button
和TextView
提供相同的ID,它们应该是唯一的。
使用counter
变量是完全没必要的,您只需使用for循环来遍历streakArrayList
。
这样的事情:
for (Streak streak : streakArrayList) {
TableRow mTableRow = new TableRow(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
final String activityName = streak.getActivityName();
Button btn = new Button(this);
btn.setText("" + streak.getDaysKept());
btn.setId(aUniqueId);
btn.setBackground(getResources().getDrawable(R.drawable.round_button));
btn.setLayoutParams(btnParams);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Dialog dialog = new Dialog(AllStreaks.this);
dialog.setContentView(R.layout.activity_enlarged);
tv1 = (TextView) dialog.findViewById(R.id.activity_enlarge_icon);
tv1.setText(activityName);
dialog.show();
}
});
TextView tv = new TextView(this);
tv.setText(activityName);
tv.setId(anotherUniqueId);
tv.setGravity(Gravity.CENTER | Gravity.BOTTOM);
tv.setTextSize(20);
tv.setLayoutParams(tvParams);
ll.addView(btn);
ll.addView(tv);
mTableRow.addView(ll);
mTableLayout.addView(mTableRow);
}