Android : for loop not in loop until finish

My Android project has a for loop. When I call this for loop, a runs in the loop until a=9 and then exits out of the loop. But in my code category.size() is 36.

What is incorrect in my code? How can I fix this problem?

for (int a = 0; a < category.size(); a++) {

    File ext = Environment.getExternalStorageDirectory();
    File target = new File(ext, "Android/data/"+ac.getPackageName()
            +"/cache/"+String.valueOf(category.get(a).get("image").hashCode()));
    aq.download(category.get(a).get("image"), target, new AjaxCallback<File>() {

        public void callback(String url, File file, AjaxStatus status) {
            if (file != null) {

            }
        }
    });
    ActionItem act = null;

    act = new ActionItem(a,category.get(a).get("name").toString(),
            Drawable.createFromPath(target.getAbsolutePath()),
            category.get(a).get("image").toString());
    quickAction.addActionItem(act, category.size());
}

Upvotes: 0

Views: 140

Answers (2)

Kinnar Vasa
Kinnar Vasa

Reputation: 407

First, Always use try-catch for any DB/server operations:

for (int a = 0; a < category.size(); a++) {
            try {
                File ext = Environment.getExternalStorageDirectory();
                File target = new File(ext, "Android/data/" + ac.getPackageName() + "/cache/" + String.valueOf(category.get(a).get("image").hashCode()));
                aq.download(category.get(a).get("image"), target, new AjaxCallback<File>() {

                    public void callback(String url, File file, AjaxStatus status) {
                        if (file != null) {

                        }
                    }
                });
                ActionItem act = null;

                act = new ActionItem(a, category.get(a).get("name").toString(), Drawable.createFromPath(target.getAbsolutePath()), category.get(a).get("image").toString());
                quickAction.addActionItem(act, category.size());
            }catch (Exception e){
                Log.e("Exception", e.getMessage());
            }
        }

Second, after doing this, check out the log, it may some exception throwing after that which stops your loop after 9th element.

Upvotes: 1

Hareshkumar Chhelana
Hareshkumar Chhelana

Reputation: 24848

Check a value first line inside loop like below :

if(a>8){
  break;
}

Upvotes: 0

Related Questions