ArrayList populating outside the method

时间:2017-12-18 05:44:04

标签: java android android-studio arraylist

Please check the updated code. It is saying arraylist is null, while trying to get the element from the oncreate method. By the way I'm adding the elements to the arraylist in inner class. Any help will be appreciated. Thanks..

private ArrayList<String> imageName;
ListView listView;
ArrayList<String> test;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context = this;
    listView = findViewById(R.id.clipslistview);
    test = new ArrayList<>();
    credentialProvider();
    setTransferUtility();
    getAll();
    Toast.makeText(context, test.get(0)+"", Toast.LENGTH_SHORT).show();
}

Thread thread = new Thread(){
    public void run(){
        final ListObjectsV2Request req = new ListObjectsV2Request().withBucketName("bucket-1234314-**********").withStartAfter(".jpg").withMaxKeys(10);
        ListObjectsV2Result result;

        imageNames = new ArrayList<>();

        result =  amazonS3Client.listObjectsV2(req);


        for (S3ObjectSummary objectSummary :
                result.getObjectSummaries()) {
            clipNames.add(objectSummary.getKey()+"");
            Log.d("TEEEST", objectSummary.getKey());
        }
    }
};

1 个答案:

答案 0 :(得分:1)

Try this

List<String> names = new ArrayList<>():

Thread thread = new Thread() {
    @Override
    public void run() {
       names.add("Hi");
       names.add("KK");
       names.add("Hello");
    }
};

thread.start();

public void lengthOflit() {
    Toast.makeText(getApplicationContext, names.size()+"", Length.SHORT).show;
}

FYI

No need to use Thread for adding data in to your ArrayList

List<String> names = new ArrayList<>():
names.add("Hi");
names.add("KK");
names.add("Hello");