So I'm building a sequence simulation test for my first android app. This involves displaying for example a grid of 9 values from 1 to 9 in a random fashion. The idea is to measure how long it takes for the user to touch all 9 values in the correct order, finishing the test. Example below:
As you can see, I've created an adapter to display the grid UI correctly, it randomizes the order of the buttons in the grid and displays it on the screen. The problem is I'm stuck when it comes to the actual testing. I'm not sure how to check the user has pressed all the numbers in the correct order? Am I going about this in the right way?
Any help much appreciated, code below:
ONCLICKLISTENER
public class MyOnClickListener implements 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
public class ButtonAdapter extends 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;
}
}
}