Cristian Cam G
Cristian Cam G

Reputation: 362

List view load faster

I'm using a ListView but it load too slow and i need it to load faster, not sure how i can make the adapter load each of the listviews one by one, is it possible? here is my code:

                ArrayList<String> myTripOrigin = new ArrayList<>();
                ArrayList<String> myTripDestiny = new ArrayList<>();
                ArrayList<String> myTripHour = new ArrayList<>();
                ArrayList <boolean[]> myTripSchedule = new ArrayList<>();
                ArrayList<Boolean> myTripBools = new ArrayList<Boolean>();


                for(int i = 0; i<response.body().size();i++)
                {
                    myTripBools.add(response.body().get(i).isRol());
                    myTripOrigin.add(getAdress(new LatLng(Double.parseDouble(response.body().get(i).getOrigin_lat()),Double.parseDouble(response.body().get(i).getOrigin_lng()))));
                    myTripDestiny.add(getAdress(new LatLng(Double.parseDouble(response.body().get(i).getDestiny_lat()),Double.parseDouble(response.body().get(i).getDestiny_lng()))));
                    Date date = new Date((Long.parseLong(response.body().get(i).getArrival_time()))*1000L);
                    DateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss aa");
                    format.setTimeZone(TimeZone.getDefault());
                    String formatted = format.format(date);
                    myTripHour.add(formatted);


                }

                //Send the array to constructor
                ListAdapter userAdapter = new CustomAdapterRoute(getApplicationContext(), myTripOrigin,myTripDestiny,myTripHour,myTripSchedule,myTripBools);
                ListView userListView = (ListView) findViewById(R.id.listViewRoute);
                userListView.setAdapter(userAdapter);

            }

here is the custom adapter Route:

public class CustomAdapterRoute extends ArrayAdapter<String>{

    private ArrayList<String> itemFrom = new ArrayList<>();
    private ArrayList<String> itemTo = new ArrayList<>();
    private ArrayList<String> itemHour = new ArrayList<>();
    private ArrayList<boolean[]> itemSchedule = new ArrayList<>();
    private ArrayList<Boolean> itemRole = new ArrayList<>();

    //Send arrays
    public CustomAdapterRoute(Context context, ArrayList<String> stringArray, ArrayList<String> stringTo, ArrayList<String> stringHour, ArrayList<boolean[]> stringSchedule,ArrayList<Boolean> boolRole){
        super(context, R.layout.custom_row_route,stringArray);

        //save the arrays in global var
        this.itemFrom = stringArray;
        this.itemTo = stringTo;
        this.itemHour = stringHour;
        this.itemSchedule = stringSchedule;
        this.itemRole = boolRole;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        LayoutInflater customInflater = LayoutInflater.from(getContext());

        final View customView = customInflater.inflate(R.layout.custom_row_route, parent, false);

        String singleFrom = itemFrom.get(position);
        String singleTo = itemTo.get(position);
        String singleHour = itemHour.get(position);
        boolean singleRole = itemRole.get(position);

        //Get a string chain from the booleans routine from each user
        //String singleSchedule = ScheduleText(itemSchedule.get(position));

        //Get TextView from the custom row
        TextView customText = (TextView) customView.findViewById(R.id.fromText);
        TextView customTextTo = (TextView) customView.findViewById(R.id.toText);
        TextView customTextHour = (TextView) customView.findViewById(R.id.hourText);
        ImageView RoleImage = (ImageView) customView.findViewById(R.id.ImageRole);
        //TextView customTextSchedule = (TextView) customView.findViewById(R.id.repeatText);

        //set the image of the role
        if(singleRole) {RoleImage.setImageResource(R.mipmap.steer3);}
        else{ RoleImage.setImageResource(R.mipmap.hand3);}


        //Give values from each position of the array
        customText.setText(singleFrom);
        customTextTo.setText(singleTo);
        customTextHour.setText(singleHour);


        //User want to edit a route
        ImageButton buttonEditTrip = (ImageButton) customView.findViewById(R.id.ImageRole);

        return customView;

    }

}

Upvotes: 0

Views: 142

Answers (2)

FranMowinckel
FranMowinckel

Reputation: 4343

In the first piece of code, you're calling response.body() all the time (why don't get the body into a variable and reuse it?).

And then in the adapter, you are not reusing the convertView:

View customView;
if(convertView == null) {
   view = customInflater.inflate(R.layout.custom_row_route, parent, false);
} else {
   customView = convertView;
}

Furthermore, you could also store the LayoutInflater as an instance variable of your adapter.

BTW the custom prefix is quite redundant, since everything you code is going to be custom by definition...

And the smooth scrolling link of the other answer (from @Gavriel) is important as well.

Upvotes: 1

Gavriel
Gavriel

Reputation: 19247

Just as I thought. You should use the holder pattern: http://developer.android.com/training/improving-layouts/smooth-scrolling.html to make it faster

Upvotes: 3

Related Questions