Android Java ArrayList inserting element not working
- by DavidNg
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());
}
});
}
}