문제

I am trying to populate an array of image urls from Post class but the activity returns a blanked page. am i doing it right. sorry am learning android don't know much.

ArrayList PostDetails = new ArrayList();

    for (int index=0;index<imageUrls.length;index++){
        imageUrls[index]=PostDetails.get(index).getImag();
    }
도움이 되었습니까?

해결책

You should wait till the task is completed, you can put the last part in onPostExecute method..

protected void onPostExecute( ) {
    String[]imageUrls= new String[PostDetails.size()];

   for (int index=0;index<imageUrls.length;index++){
       imageUrls[index]=PostDetails.get(index).getImag();
   }
}

다른 팁

The answer by Nunu is correct, but I would like to expand on it a little. The assumption is that loadingTask is an AsyncTask, thus when you call execute the code within doInBackground will execute asynchronously. The code where you create and populate the array will execute in parallel to your population of PostDetails, which I assume is populated within the loadingTask. Since the array loop will execute very quickly, it will have completed long before the network operation performed by loadingTask. Thus your page is blank because you have looped through the array before PostDetails has been populated.

The solution is just as Nunu suggests, which is to move the population of the imageUrls array into the onPostExecute method of your AsyncTask. Also, any UI manipulation that uses this array should also be moved into onPostExecute.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top