janishar / Tutorials

Published tutorial files
https://mindorks.com/blogs
Apache License 2.0
86 stars 137 forks source link

There is no need to use JSONArray in Utils.loadProfiles() #3

Open Zhuinden opened 7 years ago

Zhuinden commented 7 years ago

You can replace the JSONArray with TypeToken and use GSON to parse the List<Profile> directly.

public class Utils {
    @NonNull
    public static List<Profile> loadProfiles(Context context) {
        BufferedReader reader = null;
        try {
            AssetManager manager = context.getAssets();
            InputStream inputStream = manager.open("profiles.json");
            reader = new BufferedReader(new InputStreamReader(inputStream));
            Gson gson = new Gson(); // TODO dependency injection
            return gson.fromJson(reader, new TypeToken<ArrayList<Profile>>() {}.getType());
        } catch(Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            if(reader != null) {
                try {
                    reader.close();
                } catch(IOException e) {
                    // Ignore
                }
            }
        }
    }
}
janishar commented 7 years ago

@Zhuinden Sure

scorlingonzalez commented 6 years ago

hello brother, i am wondering why do i get repeated views whens swiping.

janishar commented 6 years ago

@scorlingonzalez It's added again for the demo purpose.

scorlingonzalez commented 6 years ago

what am trying to say is that i get repeated people while i swipe

scorlingonzalez commented 6 years ago

also for some reason i can get the image to show

janishar commented 6 years ago

@scorlingonzalez you are referring to the TinderSwipe project or tinder-swipe-v2?

scorlingonzalez commented 6 years ago

this is the link https://blog.mindorks.com/android-tinder-swipe-view-example-3eca9b0d4794

janishar commented 6 years ago

check this method:

    @SwipeOut
    private void onSwipedOut(){
        Log.d("EVENT", "onSwipedOut");
        mSwipeView.addView(this);
    }

remove this line: mSwipeView.addView(this)

It adds the view again if removed.

scorlingonzalez commented 6 years ago

@SwipeOut public void onSwipedOut() { mSwipeView.addView(this); }

scorlingonzalez commented 6 years ago

i do have this method in the same format... it is usally random behavior for example i may get the same person twice in a row or just at random.. and also if let say an array has 3 people i can scroll like 15 or more time randomly repeating the people... do you think i can send you my code in facebook maybe u can help me out a bit please

scorlingonzalez commented 6 years ago

i delete that line but still same behaviour