Custom Class And Datasource - Day Five

Introduction:

Today, I am starting with custom classes and data sources and furthermore how we show fake data in list view using the adapter after the configuration of GIT version control and create a new Android project and set it.

  • Project name, CustomClass
  • Minimum SDK API 19
  • Choose Blank Activity

Refer to the article series below:

Step 1:

Make a simple project with empty activity and then in main activity design one button and add one layout and one activity for showing a product list.

xml

Here you see the code of listview button.

xml

Step 2:

Then create the following classes into a Java folder and add one layout in layout section.

section

Layout Section

xml

Step 3:

Now add one method in MainActivity.Class because when user clicks on listview button then it moves on second activity and the name of second activity is ListViewActivity.Class

code

Step 4:

Now In Product.Class add some attributes like product name, price, image and description and then make a getter and setter methods of those attributes.

code

Step 5:

In ProductDatasource.java class we make an arraylist because the benefit of arraylist is that it makes an array size at runtime and in arraylist we declare a loop because we show multiple items in listview, not one, in this way we declare loop.

Why data source?

  • All data related functions.
  • Separate data manipulation class.
  • All business logics present here.

Its important functions:

  • get List ()
  • Insert ()
  • Update ()
  • Delete ()

What is ArrayList?

  • Supports dynamic arrays that can grow as needed.
  • Standard Java arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you must know in advance how many elements an array will hold.
  • Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk.

Items can add by

add () and remove by
remove ()

code

Step 6:

Now in make ProductListActivity.java we declare a data source and connect with this class,

code

Read more articles on Android:

Up Next
    Ebook Download
    View all
    Learn
    View all