我有一个textview
编程创建一个循环。点击一个按钮,我想填充每个用数组中不同值创建的textview
,我如何实现这一点。这是按钮被点击时发生的情况。谢谢您的帮助。从数组列表中设置textview的不同文本
btnPnar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listLayout.removeAllViews();
for (EditText et : editTextCollection)
{
gottenText = et.getText().toString();
inputList.add(gottenText);
Collections.shuffle(inputList);
}
// Creating alert Dialog with one Button
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this,R.style.DialogeTheme);
// Setting Dialog Title
alertDialog.setTitle("PNAR");
// Setting Dialog Message
alertDialog.setMessage("Enter Amount of Numbers to Pick");
final EditText input; input = new EditText(MainActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
input.setLayoutParams(lp);
input.setTextColor(Color.parseColor("#f06292"));
input.setInputType(2);
alertDialog.setView(input);
alertDialog.setPositiveButton("DONE",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
pnarGotten = Integer.parseInt(input.getText().toString());
Collections.shuffle(inputList);
for (int n =0; n<pnarGotten; n++) {
TextView textView = new TextView(MainActivity.this);
textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
textView.setGravity(Gravity.CENTER);
textView.setTextColor(Color.WHITE);
textView.setText(inputList.get(0));
textView.setText(inputList.get(1));
listLayout.addView(textView);
}
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("CANCEL",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.create();
alertDialog.show();
}
});
UPDATE 这是我加入到里面的for循环的代码,但它显示只有一个输出
for (int n =0; n<pnarGotten; n++) {
textViewForPnar = new TextView(MainActivity.this);
textViewForPnar.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
textViewForPnar.setGravity(Gravity.CENTER);
textViewForPnar.setTextColor(Color.WHITE);
while (iterator.hasNext())
{
textViewForPnar.setText(iterator.next());
}
listLayout.addView(textViewForPnar);
}
只需更换你的'对我的回答代码:) – Geek