1
public class ClientGame extends Activity implements OnClickListener{
Button Answer1, Answer2, Answer3, Answer4;
TextView txtQuest;
DatabaseHelper dbHelper;
Cursor cur;
String qid;
String[] _ids = {};
ArrayList<String> idList;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.clientgame);

    dbHelper = new DatabaseHelper(this);
    cur = dbHelper.getRandomQuestion();
    startManagingCursor(cur);
    qid = cur.getString(cur.getColumnIndex(DatabaseHelper.colID));

    idList = new ArrayList<String>();
    idList.add(qid);        // i take the current id 

    try
    {
        Intent extras = getIntent();
        String ID = extras.getStringExtra("_id");
        idList.add(ID); // add the previous one
        _ids = (String[]) idList.toArray(new String[0]);

        score = (TextView)findViewById(R.id.textView2);
        score.setText("Your score is " + idList);
    }
    catch(Exception e)
    {
    }

    Answer1 = (Button) findViewById(R.id.button1);
    Answer1.setText(answer1);
    Answer1.setOnClickListener(this);
    Answer2 = (Button) findViewById(R.id.button2);
    Answer2.setText(answer2);
    Answer2.setOnClickListener(this);
    Answer3 = (Button) findViewById(R.id.button3);
    Answer3.setText(answer3);
    Answer3.setOnClickListener(this);
    Answer4 = (Button) findViewById(R.id.button4);
    Answer4.setText(answer4);
    Answer4.setOnClickListener(this);    

                                                }

@Override
public void onPause()   {
    super.onPause();
    finish();           }

@Override
public void onClick(View view)  {  //correct answer check and store skore and id
    if (view == findViewById(R.id.button1))
    {           
        int i = 1;
        String ID = cur.getString(cur.getColumnIndex("_id"));

        Intent ini = new Intent(ClientGame.this, ClientGame.class);
        ini.putExtra("_id", ID);
        startActivity(ini);             
    }   

    if (view == findViewById(R.id.button2))
    {
        int i = 1;
        String ID = cur.getString(cur.getColumnIndex("_id"));

        Intent ini = new Intent(ClientGame.this, ClientGame.class);
        ini.putExtra("_id", ID);
        startActivity(ini);             
    }
    if (view == findViewById(R.id.button3))
    {
        int i = 1;
        String ID = cur.getString(cur.getColumnIndex("_id"));

        Intent ini = new Intent(ClientGame.this, ClientGame.class);
        ini.putExtra("_id", ID);
        startActivity(ini);             
    }
    if (view == findViewById(R.id.button4))
    {
        int i = 1;
        String ID = cur.getString(cur.getColumnIndex("_id"));

        Intent ini = new Intent(ClientGame.this, ClientGame.class);
        ini.putExtra("_id", ID);
        startActivity(ini);             
    }

}

这里的问题是在 textview 上只显示了两个 id。最后一个和当前。我想显示用户想要的所有ID。当然不是无限的。那么我如何创建一个数组列表,假设有十个值,并且每次都存储 id?

4

1 回答 1

2

您只在列表中添加了两个 ID,因此行为是正确的。如果您的活动被多次创建并且您想使用上次添加的值,则必须做两件事:

  • 将列表声明为静态成员:

    静态 ArrayList idList;

  • 改变你分配它的方式,如下所示:

    if (idList == null) { idList = new ArrayList(); }

这将做你想要的。我建议不要再次启动活动,只需在单击按钮时更新 TextView 中的文本。

编辑 - - -

要更新 TextView 而不是再次启动活动,请执行以下操作:

score.setText(score.getText().toString() + ", " + ID)

我只是在现有文本的末尾添加了一个逗号和新的 id。当然,您可能希望以不同的方式对其进行格式化。拥有显示的文本和新的文本应该足以获得所需的格式。

于 2012-04-05T21:10:52.240 回答