public class ExpAdapter : BaseExpandableListAdapter
{
private int seed = 1000;
public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
{
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater) context.GetSystemService(Context.LayoutInflaterService);
convertView = inflater.Inflate(Resource.Layout.inspection_row_2, null);
}
int currentID;
Random randomizer = new Random(seed);
TextView question = (TextView) convertView.FindViewById(Resource.Id.questionText);
RadioGroup radio = (RadioGroup) convertView.FindViewWithTag("actionGroup");
currentID = randomizer.Next(1,101);
radio.Id = currentID;
RadioButton pass = (RadioButton) convertView.FindViewWithTag("passed");
currentID = randomizer.Next(1,101);
pass.Id = currentID;
RadioButton fail = (RadioButton) convertView.FindViewWithTag("failed");
currentID = randomizer.Next(1,101);
fail.Id = currentID;
RadioButton correct = (RadioButton)convertView.FindViewWithTag("corrected");
currentID = randomizer.Next(1,101);
correct.Id = currentID;
RadioButton na = (RadioButton)convertView.FindViewWithTag("na");
currentID = randomizer.Next(1,101);
na.Id = currentID;
string[][] items = questions.childItems();
question.Text = items[groupPosition][childPosition];
seed++;
return convertView;
}
}
使用斷點來遍歷上面的內容,在執行過程中currentID = 1。上面的方法被重複調用,因爲MonoDroid在父組下創建控件的子組,並且currentID是ALWAYS = 1。我還嘗試在整個類中創建currentID和randomizer作爲私有靜態變量。同樣的結果。C#隨機總是相同的數字
編輯:我試圖創建新的隨機實例種子值不同,每次調用GetChildView。代碼在上面進行編輯以顯示我是如何做到的。仍然currentID的值總是= 1.下面是Locals中隨機數發生器的信息屏幕截圖。無論種子號是什麼,這些信息看起來都是一樣的。 ???
你有沒有嘗試將種子喂入隨機發生器? – 2012-04-02 18:08:39
是的。同樣的結果。另外,我的理解是,在我的示例中使用seed會在每次迭代過程中生成相同的數字。該號碼正用於該對象的ID並且需要是唯一的。 – jmease 2012-04-02 18:11:12
我不明白你在問我什麼。我期望它使用DateTime創建一個新的Random對象,然後在GetChildView的下一次迭代之前抓取下一個隨機數5次,此時將根據當前的DateTime創建一個新的Random對象。 – jmease 2012-04-02 18:15:26