【问题标题】:Android: Adding values in to array from button input and checking for correct order once all buttons pressedAndroid:从按钮输入向数组中添加值并在按下所有按钮后检查正确的顺序
【发布时间】: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


【解决方案1】:

创建一个名为 UserState 的对象并添加一个名为 userClickNumber 的 int,它随着每次点击(或每次成功点击?)而递增。在您的适配器构造函数中创建它或将其传递给您的适配器构造函数。将此作为实例变量存储在您的适配器中,并将其传递给您的侦听器,如下所示:

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

然后在您的 processNumber 中,您可以通过获取数组并执行 array[userStateVar.getUserClickNumber()] 来检查他们是否单击了正确的按钮。

【讨论】:

  • 嗨,Erik,我是否认为您的意思是我需要创建一个如下所示的对象:public Object UserState() { int userClickNumber = 0; return userClickNumber; },然后像这样将它传递给我的适配器构造函数? public ButtonAdapter(Context c, Object UserState) { mContext = c; }我是否在 processNumber 方法中增加 userClickNumber 以及检查?
  • 你越来越近了!不要在此处将代码放入 cmets 中,而是尝试 gist.github.com 并发送链接。
猜你喜欢
  • 2013-06-16
  • 2021-11-25
  • 1970-01-01
  • 1970-01-01
  • 2017-12-07
  • 2020-03-23
  • 1970-01-01
  • 1970-01-01
  • 2011-09-20
相关资源
最近更新 更多