【发布时间】:2014-05-29 02:22:24
【问题描述】:
所以我正在为我的第一个 android 应用程序构建一个序列模拟测试。这涉及以随机方式显示例如从 1 到 9 的 9 个值的网格。这个想法是测量用户以正确的顺序触摸所有 9 个值需要多长时间,完成测试。示例如下:
如您所见,我创建了一个适配器来正确显示网格 UI,它随机化网格中按钮的顺序并将其显示在屏幕上。问题是我在实际测试时被卡住了。我不确定如何检查用户是否按正确顺序按下了所有数字?我是否以正确的方式处理这件事?
非常感谢任何帮助,代码如下:
ONCLICKLISTENER
公共类 MyOnClickListener 实现 OnClickListener{
private final int position;
public MyOnClickListener(int position)
{
this.position = position;
}
public void processNumber(int number)
{
}
public void onClick(View v)
{
// Perform a function based on the position
processNumber(this.position);
}
}
按钮适配器
公共类 ButtonAdapter 扩展 BaseAdapter {
private Context mContext;
static final String[] numbers = new String[] {
"1", "2", "3",
"4", "5", "6",
"7", "8", "9",};
static final String[] mediumNumbers = new String[] {
"1", "2", "3", "4",
"5", "6", "7", "8",
"9", "10", "11", "12",
"13", "14", "15", "16",};
static final String[] hardNumbers = new String[] {
"1", "2", "3", "4", "5",
"6", "7", "8", "9", "10",
"11", "12", "13", "14", "15",
"14", "15", "16", "17", "18",
"21", "22", "23", "24", "25"};
// Gets the context so it can be used later
public ButtonAdapter(Context c)
{
mContext = c;
}
// Total number of things contained within the adapter
public int getCount()
{
if (SequenceLevelSelect.buttonPressed == 1)
{
return numbers.length;
}
if (SequenceLevelSelect.buttonPressed == 2)
{
return mediumNumbers.length;
}
if (SequenceLevelSelect.buttonPressed == 3)
{
return hardNumbers.length;
}
else
{
return 0;
}
}
// Require for structure, not really used in my code.
public Object getItem(int position)
{
return null;
}
// Require for structure, not really used in my code. Can
// be used to get the id of an item in the adapter for
// manual control.
public long getItemId(int position)
{
return position;
}
public View getView (int position, View convertView, ViewGroup parent)
{
Button btn;
if (convertView == null)
{
// if it's not recycled, initialize some attributes
btn = new Button(mContext);
btn.setLayoutParams(new GridView.LayoutParams(100, 100));
btn.setPadding(20, 20, 20, 20);
}
else
{
btn = (Button) convertView;
}
if (SequenceLevelSelect.buttonPressed == 1)
{
btn.setText(numbers[position]);
}
if (SequenceLevelSelect.buttonPressed == 2)
{
btn.setText(mediumNumbers[position]);
}
if (SequenceLevelSelect.buttonPressed == 3)
{
btn.setText(hardNumbers[position]);
}
btn.setTextColor(Color.WHITE);
btn.setId(position);
// Set the onclicklistener so that pressing the button fires an event
// We will need to implement this onclicklistener.
btn.setOnClickListener(new MyOnClickListener(position));
return btn;
}
//using the Fisher–Yates shuffle to randomly shuffle my array of strings
static void shuffleArray(String[] numbers)
{
Random rnd = new Random();
for (int i = numbers.length - 1; i > 0; i--)
{
int index = rnd.nextInt(i + 1);
// Simple swap
String a = numbers[index];
numbers[index] = numbers[i];
numbers[i] = a;
}
}
}
【问题讨论】:
-
目前看起来不错!现在您需要一个模型对象来存储状态,或者用户到目前为止输入的内容。您可以将其存储在单例中或将其传递给您的每个听众。
-
嗨,埃里克。在编程方面我没有信心,所以我可以要求详细说明您所说的以供我理解吗?我必须采取哪些步骤来实现模型对象,我会使用 onClick 函数将它传递给我的侦听器吗?
标签: android arrays arraylist adapter android-arrayadapter