我在循环中以编程方式创建了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();
}
});
更新 这是我在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);
}
答案 0 :(得分:0)
您应该使用inputList集合的Iterator
对象来从中恢复元素:
Iterator<String> iterator = inputList.iterator();
while (iterator.hasNext())
{
textView.setText(iterator.next());
}
答案 1 :(得分:0)
只需使用此代码替换您的for loop
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(n));
listLayout.addView(textView);
}