如何拨打随机电话'数据库sqlite中的List中的名称。如何在不重复其值的情况下逐个调用。我的布局是一个textview,如果我点击它将改变名称。谢谢。
答案 0 :(得分:0)
未经测试,但应该没问题:
查询您的数据库,然后使用返回的Cursor
使用您想要的任何选项填充LinkedList
。
LinkedList list = new LinkedList();
if (cursor.getCount() > 0) {
for (int i = 0; i < cursor.getCount(); i++) {
cursor.moveToPosition(i);
list.add(cursor.getString(etc...);
}
}
创建一个Random
对象,并使用它来每次从list
中选择和删除随机元素:
Random rnd = new Random();
//The below section could be repeated on, for instance, a button click.
int randomValue = rnd.nextInt(list.size());
String result = list.get(randomValue);
list.remove(randomValue);
每次删除元素时,LinkedList
都会调整大小以适应它,因此不会重复结果。
答案 1 :(得分:0)
@PPartisan无法正常工作。 我添加按钮来测试textview的结果。仅当我按下并返回到活动时才检测到1行,结果是相同的值。我点击按钮不起作用。
rnd = new Random();
randomValue = rnd.nextInt(list.size());
result =list.get(randomValue).toString();
list.remove(randomValue);
btnClick.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
tv.setText(result);
}
});