0

I am new to SO, and although I've been through the FAQ's, I hope I get this question right.

THE BACKGROUND

I have been successful in getting data using the Facebook SDK and displaying them in a custom listview. However, as any beginner, I have invariably slowed things down and the listview lags like crazy. To remedy that, I started implementing Lazy Loading using this example from SO: https://stackoverflow.com/a/3068012/450534

THE STUMBLING BLOCK

I this example @Fedor has hard-coded the URL's into a String Array. Now I want to replace that String array with those fetched from the Facebook query result (JSON).

Also, is that a good example to follow (strictly for this scenario) or is there a better example out there? I have spent close to 2 days just trying to figure the lazy loading part out. But I am stumped either for lack of efficient searching or the fact that I've just begun.

I hope this question makes sense. Thank you for your advice.

Cheers....

A sample of the link to an image returned using JSON to parse a Facebook query.

04-23 01:44:28.505: V/PICTURE LINK URL :(3902): https://s-external.ak.fbcdn.net/safe_image.php?d=AQBZ5-jVGrHA2hpU&w=90&h=90&url=http%3A%2F%2Fblog.mozilla.org%2Ftheden%2Fwp-content%2Fthemes%2FTheDen%2Fimg%2Fbg-header.png

A subset of the way @Fedor has hard-coded the URL's in his example.

private String[] mStrings={
            "http://a1.twimg.com/profile_images/349012784/android_logo_small_normal.jpg",
            "http://a1.twimg.com/profile_images/841338368/ea-twitter-icon.png",
            "http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
            "http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png",
            "http://a1.twimg.com/profile_images/850960042/elandroidelibre-logo_300x300_normal.jpg",
            "http://a1.twimg.com/profile_images/655119538/andbook.png",
            "http://a3.twimg.com/profile_images/768060227/ap4u_normal.jpg",
            "http://a1.twimg.com/profile_images/74724754/android_logo_normal.png",
            "http://a3.twimg.com/profile_images/681537837/SmallAvatarx150_normal.png"
    };

EDIT

The new edit was going to take a lot of space. Therefore, I am linking it pastebin: http://pastebin.com/HZfCTMy6

The facebook API has a bug where if there is a shared post on the News Feed, it does not give a "picture" tag and another query has to be made to get the picture URL using the "object_id" tag. In the pastebin code, this is put in an if + else if code block starting at line number 155

This is really a lengthy piece of code and I appreciated the time you may take going through it.

Any help with this will be really, really appreciated. Over two days and no solution in sight. :-(

Community
  • 1
  • 1
Siddharth Lele
  • 27,623
  • 15
  • 98
  • 151
  • What exactly are you asking? How to create a view for facebook api results? Or how to modify that example to do that? If so, then the question is too broad. If you have a more specific problem, can you add the code to your question? – Nitzan Tomer Apr 22 '12 at 20:08
  • I am pulling the image links displayed on the Facebook News Feed. And using @Fedor's Lazy Loading example, am trying to pass the image links via a String Array. The String Array ATM is hardcoded. I need that to be dynamic since the links fetched via JSON will change as the News Feeds do. Give me a minute to add some example code. – Siddharth Lele Apr 22 '12 at 20:12
  • Most of the code is irrelevant, and the part that is relevant you did not put.. Where's the code for *LazyAdapter*? You say that the problem is there no? That in this class the urls were hardcoded and this object uses just the last url instead of all of them, so that's the code that is needed here. – Nitzan Tomer Apr 23 '12 at 06:47
  • Adding it to the pastebin link now. My apologies for the missing code. It is part of the same activity right at the very bottom after the onCreate method. – Siddharth Lele Apr 23 '12 at 06:50
  • I don't understand, the member *mStrings* is part of the activity or the *LazyAdapter*? I really don't understand what the problem is, please explain once and for all exactly what you need, and supply the code for it – Nitzan Tomer Apr 23 '12 at 07:09
  • That is the entire code actually. Nothing has to be changed in the lazyadapter. it works with whatever array has been passed to it. In this activity, as in @Fedor's example, the string array **mStrings** is hard-coded (Line 261 in pastebin) What I want to do is, grab the URL from the JSON returned by facebook and use those links instead of the hard-coded **mStrings**. I hope this makes things a little clearer. And sorry for any confusion. – Siddharth Lele Apr 23 '12 at 07:18

1 Answers1

0

If you have a hardcoded definition like that:

class A {
    private String[] strings = { "str1", "str2", ... };

    public A() {
        ...
    }
    ...
}

you can simply break it with adding a setter (and changing for String array to a dynamic collection):

class A {
    private ArrayList<String> strings;

    public A() {
        this.strings = new ArrayList<String>();
        ...
    }

    public void addString(String str) {
        this.strings.add(str);
    }
    ...
}

If there's code in the constructor that depends on the array to have values, then move it to a new method and call it after you finished adding the strings.

As for the format of the data from facebook, since they return json it's easy to get the exact data you need, just extract the needed urls and add them to the list.


Edit

This is from your code:

ArrayList<String> listOfURLS;
....
if (json_data.has("picture"))   {
    for (int j = 0; j < json_data.length(); j++) {
        listOfURLS = new ArrayList<String>();
        listOfURLS.add(changedString);
    }
} else if (json_data.has("object_id")) {
    getPictureURL();
    listOfURLS = new ArrayList<String>();
    listOfURLS.add(finalPictureFromObjectID);
}

This of course will result in having just one url in the listOfUrls, you construct a new ArrayList every time, you should not do that. It should look something like:

ArrayList<String> listOfURLS;
....
if (json_data.has("picture"))   {
    for (int j = 0; j < json_data.length(); j++) {
        listOfURLS.add(changedString);
    }
} else if (json_data.has("object_id")) {
    getPictureURL();
    listOfURLS.add(finalPictureFromObjectID);
}
Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299