Android Java ArrayList inserting element not working

Posted by DavidNg on Stack Overflow See other posts from Stack Overflow or by DavidNg
Published on 2012-06-19T21:08:19Z Indexed on 2012/06/19 21:16 UTC
Read the original article Hit count: 273

Filed under:
|
|

I have a simple Android program with one button and one textview. When the button is clicked, a number is inserted in in an ArrayList. However, it does not work. If changing to add, it works fine.

package test_ad.com;

import java.util.ArrayList;

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

public class Test_adActivity extends Activity {
    ArrayList<Integer> al = new ArrayList<Integer>();
    Button myB;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    myB = (Button) findViewById(R.id.button1);
    myTV = (TextView) findViewById(R.id.textView1);
    myB.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            al.set(i,10*i);//insert an Integer at index i, but not working
            i++;
            if (i == 5)
                myTV.setText(al.toString());
        }
    });
}

}

© Stack Overflow or respective owner

Related posts about android

Related posts about insert