Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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:

enter image description here

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;
     }
  }

}
share|improve this question
    
Looking good so far! Now you need a model object to store the state, or what the user has entered so far. You can store it in a singleton or pass it in to each of your listeners. – Erik B Apr 14 '14 at 22:52
    
Hi Erik. I'm not to confident when it comes to programming so can I just perhaps ask for an elaboration on what you said for my understanding? What steps would I have to take to implement a model object and would I pass it through my listeners with the onClick function? – MattGarnett Apr 14 '14 at 23:06

Create an object called UserState and add an int called userClickNumber that increments with each click (or each successful click?). Create this in your adapter constructor or pass it into your adaptor constructor. Store this as an instance variable in your adapter and pass it in to your listeners like so:

btn.setOnClickListener(new MyOnClickListener(position, userStateVar)); 

then in your processNumber you can check they clicked the right button by getting the array and doing array[userStateVar.getUserClickNumber()].

share|improve this answer
    
Hi Erik, am I right in thinking you mean I need to create an Object like below: public Object UserState() { int userClickNumber = 0; return userClickNumber; } and then pass it in to my adapter constructor like so? public ButtonAdapter(Context c, Object UserState) { mContext = c; } Do I increment userClickNumber in the processNumber method as well as the check? – MattGarnett Apr 15 '14 at 1:21
    
You are getting closer! Instead of putting code in the comments here, try gist.github.com and send the link for that. – Erik B Apr 15 '14 at 18:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.