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);
}
}
} The problem here is that on the textview is displayed only two ids. The last one and the current. I want to show all the ids as far as the user wants. Not infinite of course. So how can i create an arraylist, lets say with ten values, and store the id each time?
You are adding only two IDs in your list so the behavior is correct. If your activity is created multiple times and you want to use the values added last time, 2 things must be done:
declare the list as a static member:
static ArrayList idList;
change the way you allocate it like this:
if (idList == null) { idList = new ArrayList(); }
This will do what you want. I would recommend not starting the activity again and just update the text in TextView when the buttons are clicked.
EDIT------
To update the TextView instead of starting the activity again do like this:
score.setText(score.getText().toString() + ", " + ID)
I just added a comma and the new id to the end of the existing text. Of course, you might want to format it differently. Having the displayed text and the new one should be good enough to get the desired format.