Introduction:-

In this video tutorial, you will learn how to deal with refreshing the data inside the listview. Because many of the people I have seen asking how to refresh after the data update inside the listview. It is also a major part while fetching any of the data or items from the database or server. So here in this tutorial, i have shown you by adding the data and refreshing those after the addition.

Platform Used:-

  • Android Studio.

Steps To Be Followed:-

  1. Open up your Android studio or eclipse and create a project of your own.
  2. After creating the new project move on to "activity_main.xml" to create the desired widgets.
  3. Now create a listview and assign it with the id of some name and give it divider color and height to make it colorful and attractive.
  4. After that create a button which will submit new data and refresh the listview after the update of the listview and assign it with your desired id and now you are ready with your XML file.
  5. Now open up your java file and create the instance of your widgets with your desired variables.
  6. After that create an ArrayList of data type "String" which will store the data of String type, into which am storing the names of some of my friends.
  7. After that now declare those instances with their respective id's defined previously in the XML file.
  8. Now, we are going to create two more classes for the ListView named as - listview_contacts, listview_adapter and one XML named as the row_layout file to display the style of the content.
  9. Now open up that XML file and create a Textview widget in which those names of the friends will be displayed and assign it to your desired id.
  10. Now open up your class previously created named as listview_contacts and press Alt+O to override methods and first of all select to create a constructor and then again select Getter and setter method to automatically create the functions of getter and set method.
  11. After this, we are done with the listview_contacts and now open up your listview adapter create the instance of the row_layout in this class and assign them with their respective id's previously assigned in the row_layout.
  12. In listview_adapter now create a constructor with the argument as "Context context and int resource".
  13. After creating the constructor now create a List to store the values passed to the listview_contacts in the List to put those values in the row_layout to get displayed.
  14. after that create add function with the argument as "Object object" to receive those names of your friends from the "MainActivity.java"
  15. after creating the add function now create some of the functions with following names to receive data from listview_contacts- getcount(),getitem(),getview() and notifydatasetchanged().
  16. Using getcount() we will be able to get the total no of entries and with the getitem() we will be able to get the list of items and with the getview() we will be able to put those data to the row_layout to get displayed.
  17. And the most important function is notifydatasetchanged() which will inform the listview that the data is now updated and its time to get refreshed and fetch the new set of data.
  18. And in the last define another function named as clear() which am using to avoid multiple entries of the same data because am refreshing the same data multiple times.
  19. now create a static class contactholder in which we are going to declare the instance of the TextView previously defined in the row_layout.
  20. Now we are going to move towards the getview() in which we are using a LayoutInflater which will add the layouts to the ListView not the simple data entries to make it look beautiful.
  21. For that LayoutInflater am using a convertview and row to get the id from the row_layout and assign with the set data received from the "MainActivity.java"
  22. After that assigning of those id's created in the "row_layout" now set the value to the TextView with the function setText() and return the getview to the MainActivity.java file.
  23. After this you are now all set with the listview_adapter now we will move towards the MainActivity.java file to start connecting that ListView to the listview_adapter to populate the layouts inside the ListView.
  24. now create an object of the listview_adapter.
  25. after creating the object of the listview_adapter now pass the MainActivity.java and the layout file to the constructor previously created in the listview_adapter.
  26. now add the names of your friends using the add() to the ArrayList created in the MainActivity.java file.
  27. After adding those names using add() now create a loop to populate the data to the array i.e listview_contacts which will hold all the value.
  28. now pass the object of the listview_contacts to the listview_adapter by using the add() which we have previously created in the listview_adapter which will only accept the argument as the object.
  29. Now set the adapter of the listview using setadapter() which will connect that layoutInflater to the ListView to populate the layouts to the ListView.
  30. after that call, the clear() defined previously in the listview_adapter which will avoid multiple entries of the data.
  31. after that add few more names inside the clickListener of the button in which by pressing, the button it will add few more data and will refresh the ListView.
  32. And at last call, the notifydatasetchanged() to check whether the data has been successfully added to the listview or not if add then refresh the Listview.
  33. After this, you are all set with the whole code.
  34. Now it's time to execute the whole program so connect your android device and run the apk.
  35. Below is the given Java codes and XML files from where you can download and run it directly.

Codes:-

listview_contacts.java:-
package com.example.nigamasis.beginnertopro;


/**
* Created by NIGAMASIS on 20-Jan-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;


}


}

listview_adapter.java:-

package com.example.nigamasis.beginnertopro;



import android.content.Context;


import android.support.annotation.NonNull;


import android.support.annotation.Nullable;


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 20-Jan-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();


}


@Nullable


@Override



public Object getItem(int position) {


return list.get(position);


}


@NonNull


@Override



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();


}

}

Downloadable Files:-


Full Source Code Zip:-

Video Tutorial:-

Thank you For Watching This Video Tutorial
Please Like, Share And Subscribe
Have A Nice Day....