INTRODUCTION:-


In this video tutorial, you will learn how to refresh the listview by swiping down the page i.e the android activity. In my previous video tutorial, i have shown you how to refresh the listview using a button. As the user has demanded to work on swipe down to listview so, I have decided to make one. Maximum of the application uses this swipe down to refresh the page concept that's why I made this tutorial. As I am using the activity to load it offline so it will not take much time to get reloaded so you must focus on the application to see the effect. The advantage of using the swipe down to refresh the listview will avoid the refresh button and will also make your app look awesome.

PLATFORM USED:-

  • Android Studio.

PROCEDURE:-
  1. Open up your android studio and create a new project with the appropriate name of your own.
  2. Now search for beginnertopro.in in your browser and select the tutorial "how to populate data in the listview and refresh on update" and go to the bottom of the page and download the full source code of that because we are going to modify those files a little bit.
  3. After downloading extract that rar file which contains five android files named as "activity_main","listview_adapter","listview_contacts","mainactivity","row_layout" and open thos files to edit.
  4. After extracting those files create those five files in the android studio with the same file name as downloaded from our website so that you can directly copy and paste the source code.
  5. As there is no work of button now so we will remove that and will add a swipe down to refresh widget and inside that widget, we will put the listView.
  6. After removing the button place the ListView inside the "swipe refresh layout" so that on sliding down it will refresh the listview.
  7. Now please copy properly those source code "listview_contacts" to your defined class. It will get the data passed from the "main_activity" to the  .listview_contacts which will further pass the data to the listview_adapter.
  8. Now after passing the data to the listview_adapter, it will create layout for the "activity_main" so as to put the data in the sequence.
  9. Now create the instance of the "swipe refresh layout" to access the object of swipe refresh because we are going to add a listener which will call the function when user will swipe down.
  10. Now implement the main_activity with "swipe refresh layout on refresh listener" to run the refresh function on swiping down the activity.
  11. Again create a function named as On refresh and assigned the refresh function inside that so as to refresh the listview, whenever there will be any new entry the listview will get updated.
  12. Make sure to remove the tools context and other dimension properties from the activity_main.xml file if it's showing you the problem otherwise it's not required.
  13. Now you are all set with code!!! Let's go and check the code on your device. Now connect your android device and have a try.

SOURCE CODE:-

listview_adapter-
package com.example.nigamasis.beginnertopro;

import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ArrayAdapter;

import android.widget.TextView;



import java.util.ArrayList;

import java.util.List;



/**

* Created by NIGAMASIS on 15-Mar-17.

*/



public class listview_adapter extends ArrayAdapter {



List list = new ArrayList();



public listview_adapter(Context context, int resource) {

super(context, resource);

}



@Override

public void add(Object object) {

super.add(object);

list.add(object);

}



@Override

public int getCount() {

return list.size();

}



public Object getItem(int position) {

return list.get(position);

}



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



View row;

row = convertView;

Contactholder contactholder;



if(row==null)

{

LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

row = layoutInflater.inflate(R.layout.row_layout,parent,false);



contactholder = new Contactholder();



contactholder.names = (TextView)row.findViewById(R.id.names);



row.setTag(contactholder);

}

else

{

contactholder = (Contactholder)row.getTag();

}



listview_contacts contacts = (listview_contacts)this.getItem(position);

contactholder.names.setText(contacts.getNames());

return row;

}



static class Contactholder

{

TextView names;

}



@Override

public void notifyDataSetChanged() {

super.notifyDataSetChanged();

}



public void clear()

{

list.clear();

}

}

listview_contacts-
package com.example.nigamasis.beginnertopro;



/**

* Created by NIGAMASIS on 15-Mar-17.

*/



public class listview_contacts {



private String names;



public listview_contacts(String names) {

this.names = names;

}



public String getNames() {

return names;

}



public void setNames(String names) {

this.names = names;

}

}



ATTACHED FILES:-


FULL SOURCE CODE:-


VIDEO TUTORIAL:-

THANKS FOR WATCHING THIS VIDEO TUTORIAL
PLEASE LIKE SHARE AND SUBSCRIBE TO OUR CHANNEL
HAVE A NICE DAY