如何在listview中显示整数?

ua4mk5z4  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(408)

我想在listview上显示for循环编号,但它不起作用。应用程序布局

public class MainActivity extends AppCompatActivity {

    private ListView marksixlist;
    private Button mRandombtn, mCleanbtn;
    private TextView mText;
    private ArrayList<Integer> marksixnum = new ArrayList<Integer>();
    private ArrayAdapter arrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        marksixlist = (ListView) findViewById(R.id.listview1);

        mRandombtn = (Button) findViewById(R.id.randombtn);
        mCleanbtn = (Button) findViewById(R.id.cleanbtn);
        mText = (TextView) findViewById(R.id.items);

        mRandombtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                for (int j = 1; j <= 6; j++) {
                    int random = (int) (Math.random()* 49+1);
                        marksixnum.add(random);
                        Collections.shuffle(marksixnum);
                }
            }
        });

        arrayAdapter = new ArrayAdapter<Integer>(this, R.layout.listitempage, marksixnum);
        marksixlist.setAdapter(arrayAdapter);
        Log.d("aaa", "The markssix is - " + marksixnum);
    }
}
6za6bjd0

6za6bjd01#

一旦生成了随机数并将其洗牌,那么在for循环的末尾添加 arrayAdapter 到listview marksixlist 再次,它将作为一种魅力。
我还清除了数组列表 marksixnum 在生成随机数之前,这样你的列表就不会继续增长。
它是可选的,如果需要最后一次单击元素,请删除 marksixnum.clear() ```
mRandombtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Do this if you do not want
// numbers from last click
marksixnum.clear();
for (int j = 1; j <= 6; j++) {
int random = (int) (Math.random()* 49+1);
marksixnum.add(random);
Collections.shuffle(marksixnum);
}
// add arrayAdapter to listview
marksixlist.setAdapter(arrayAdapter);
}
});

相关问题