ArrayList stores only two values

Go To StackoverFlow.com

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

}

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

2012-04-05 19:25
by dothedos
use a static arraylist and be sure about getting the ID's - Tugrul 2012-04-05 19:35
You say that i should create an arraylist with ten values(null) and then replace them? How can i replace values with specific idex inside the arraylist - dothedos 2012-04-05 20:07
No, only use static key before the arraylist :) static ArrayList. On the other hand, you can do what you said, because you add values to index which you have selected - Tugrul 2012-04-05 20:20


2

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.

2012-04-05 21:10
by azertiti
Yes i thought that but i couldnt do. Can u provide me a hint - dothedos 2012-04-05 21:29
I edited the previous answer, check the last part on how to avoid starting the activity again - azertiti 2012-04-05 21:41
My friend it worked. Thanks!But i cannot understand it fully! If you have time to spare just send me an email to explain it. Thanks agai - dothedos 2012-04-05 21:42
The TextView can be updated anytime, you just have to make the new String. The old one is retrieved with the method getText, see http://developer.android.com/reference/android/widget/TextView.html#getText() This returns a CharSequence so you have to convert it to a String with toString(). The final step is to add the new id to the existing String and set it back to the TextView. Hope this explains all of it : - azertiti 2012-04-05 21:46
Not the textView part, the array list part is that i dont understand..: - dothedos 2012-04-05 22:08
static objects do not belong to a class instance, they are shared between all instances. That means the next time your activity is created idList still has the values from previous invocation. The other thing to look for is not to create the object again as that would clear the old values. That's where the null test comes handy, do the allocation only once - azertiti 2012-04-05 22:25
let us continue this discussion in chatdothedos 2012-04-05 22:31
Ads