从数组列表中删除 [英] Remove from arraylist

查看:107
本文介绍了从数组列表中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

My code is this:

    public class startgame extends Activity implements OnClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.level1);

        final Random rgenerator = new Random();       

        //setup the questions
        List<String> questions1 = new ArrayList<String>();
        questions1.add("Who is the actual CEO at Apple?");
        questions1.add("Who is the actual CEO at Microsoft?");
        questions1.add("Android is made by:");

        String thequestion = questions1.get(rgenerator.nextInt(questions1.size()));

        TextView question = (TextView)findViewById(R.id.textView1);
        question.setText(thequestion);

        questions1.remove(thequestion);

        //Initialise the button variables
        Button button1 = (Button)findViewById(R.id.button1);
        Button button2 = (Button)findViewById(R.id.button2);
        Button button3 = (Button)findViewById(R.id.button3);
        Button button4 = (Button)findViewById(R.id.button4);

        if (thequestion.equals("Who is the actual CEO at Apple?")) {
            List<String> questions1res = new ArrayList<String>();
            questions1res.add("Steve Jobs");
            questions1res.add("Steven Sinofsky");
            questions1res.add("Tim Cook");
            questions1res.add("Steve Ballmer");

            button1.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
        questions1res.remove(button1.getText());
            button2.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
        questions1res.remove(button2.getText());
            button3.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
        questions1res.remove(button3.getText());
            button4.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
        questions1res.remove(button4.getText());
        }
    }

public void onClick(View v) {

        switch (v.getId()){
        case R.id.button1:
        case R.id.button2:
        case R.id.button3:
        case R.id.button4:
        }
}
}

什么ID确实是这样的:
选择的问题,这ArrayList的1个问题。创建按钮,并把选择的问题,在一个字符串,并在屏幕上显示的字符串。如果该字符串为谁是苹果的实际CEO?然后随机把史蒂夫·乔布斯和按钮所有这些问题的答案。

What id does is this: Choose 1 question from that arraylist of questions. Create the buttons, and put the chosen question in a string, and show that string on the screen. If that string is 'Who is the actual CEO at Apple?' then randomly put Steve Jobs and all those answers on buttons.

我想是这样的:
如果用户presses包含该按钮:蒂姆·库克',那么:
删除谁是苹果的实际CEO?从问题列表中,随机抽取的问题ArrayList中的另一个问题,并随机把上的按钮的答案(我已经做了同样的东西,刚才那是另外一个问题)。

What I want is this: If the user presses the button that contains: 'Tim Cook' then: Remove 'Who is the actual CEO at Apple?' from the questions list, and randomly chose another question from the ArrayList of questions, and randomly put the answers on the buttons (the same stuff I already did, just that is another question).

我的问题是,我真的不能有存取权限的阵列将其删除,因为我的一切是当按钮pressed.I试图使一个功能的情况下,但每次我执行功能,该列表总是重新创建....

My problem is that I can't really have acces to the array to delete it,because all I got is the case when the button is pressed.I tried to make a function,but every time I execute the function,the list is always recreated....

有人可以纠正code给我吗?并添加缺少什么?

Can someone correct the code for me? And add what is missing?

推荐答案

尝试改变ArrayList的范围(例如使用一个私有成员),使您的onClick方法访问...我认为你必须做同样与您的适配器将它调整到您的需要。

Try to change the scope of the ArrayList (use a private member for example) to enable access from your onClick method… I assume you'll have to do the same with your adapter to tweak it to your needs.

更新:

一个快速和肮脏的实现(不带适配器也不ViewHolder,的的):

A quick-and-dirty implementation (without adapter nor ViewHolder, etc.):

package com.stackoverflow.randomarray;

import java.lang.String;
import java.util.List;
import java.util.ArrayList;
import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class SoRandomArray extends Activity implements View.OnClickListener {
    private Random mRandom = new Random();
    private List<String> mQuestionsList;
    private String mCurrentQuestion = null;
    private List<String> mAnswersList;

    TextView mQuestionTv;
    Button mButton1;
    Button mButton2;
    Button mButton3;
    Button mButton4;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mQuestionsList = new ArrayList<String>();
        mAnswersList = new ArrayList<String>();

        initQuizData();

        mQuestionTv = (TextView)findViewById(R.id.textView1);
        // Retrieve the buttons declared by the xml layout
        mButton1 = (Button)findViewById(R.id.button1);
        mButton2 = (Button)findViewById(R.id.button2);
        mButton3 = (Button)findViewById(R.id.button3);
        mButton4 = (Button)findViewById(R.id.button4);

        mButton1.setOnClickListener(this);
        mButton2.setOnClickListener(this);
        mButton3.setOnClickListener(this);
        mButton4.setOnClickListener(this);

        shuffle();
    }

    private void initQuizData() {
        mQuestionsList.add("Who is the actual CEO at Apple?");
        mQuestionsList.add("Who is the actual CEO at Microsoft?");
        mQuestionsList.add("Android is made by:");

        mAnswersList.add("Steve Jobs");
        mAnswersList.add("Steven Sinofsky");
        mAnswersList.add("Tim Cook");
        mAnswersList.add("Steve Ballmer");
    }

    private void shuffle() {
        mCurrentQuestion = mQuestionsList.get(mRandom.nextInt(mQuestionsList.size()));
        mQuestionsList.remove(mCurrentQuestion);
        mQuestionTv.setText(mCurrentQuestion);

        mAnswersList.add("Steve Jobs");
        mAnswersList.add("Steven Sinofsky");
        mAnswersList.add("Tim Cook");
        mAnswersList.add("Steve Ballmer");
        mButton1.setText(mAnswersList.get(mRandom.nextInt(mAnswersList.size())));
        mAnswersList.remove(mButton1.getText());
        mButton2.setText(mAnswersList.get(mRandom.nextInt(mAnswersList.size())));
        mAnswersList.remove(mButton2.getText());
        mButton3.setText(mAnswersList.get(mRandom.nextInt(mAnswersList.size())));
        mAnswersList.remove(mButton3.getText());
        mButton4.setText(mAnswersList.get(mRandom.nextInt(mAnswersList.size())));
        mAnswersList.remove(mButton4.getText());
    }

    private boolean validateAnswer(String question, String answer) {

        if(question.equals("Who is the actual CEO at Apple?")) {
            if(answer.equals("Tim Cook")) {
                return true;
            } else {
                return false;
            }
        } else if (question.equals("Android is made by:")) {
            return false;
        } else if (question.equals("Who is the actual CEO at Microsoft?")) {
            if(answer.equals("Steve Ballmer")) {
                return true;
            } else {
                return false;
            }
        }

        return false;
    }

    public void onClick(View v) {
        Toast toast;

        if(validateAnswer(mCurrentQuestion, ((Button)findViewById(v.getId())).getText().toString())) {
            toast = Toast.makeText(this, "Good!", Toast.LENGTH_SHORT);
        } else {
            toast = Toast.makeText(this, "Too bad!", Toast.LENGTH_SHORT);
        }

        if(mQuestionsList.size()>0) {
            toast.show();
            shuffle();
        } else {
            toast.show();
        }
    }
}

相关的布局main.xml中:

The associated layout main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              >
  <TextView
      android:id="@+id/textView1"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="Hello World, SoRandomArray"
      />
  <Button
      android:id="@+id/button1"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content"
      android:text=""
       />
  <Button
      android:id="@+id/button2"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content"
      android:text=""
           />
  <Button
      android:id="@+id/button3"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content"
      android:text=""
       />
  <Button
      android:id="@+id/button4"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content"
      android:text="" />
</LinearLayout>

您将不得不纠正的按钮随机化的一些问题,这不是一个国家的最先进的,但是这是想法,它会给你一个开始...

You'll have to correct some issue with the randomizing of the buttons, that's not state-of-the-art but that's the idea and it will give you a start…

这篇关于从数组列表中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆