Before moving further, please refer to the article series on Android:

Introduction:

In this article I will show you how to show data in ListView. There are many methods to show a data in ListView, but I have made custom adapter because in this adapter you can easily customize the rows.

What is ListView

Step 1:

  1. Create a layout activity_product_list.xml
  2. Create ListView in activity_product_list.xml

    xml

Design

output

Step 2:

What is ListRow

Code of ListRow name as row_product_list.xml?
  1. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="fill_parent"
  3. android:layout_height="wrap_content"
  4. android:background="#665544"
  5. android:orientation="horizontal"
  6. android:padding="2dp">
  7. <LinearLayout
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:background="@color/black"
  11. android:padding="2dp">
  12. <!--image-->
  13. <ImageView
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:adjustViewBounds="true"
  17. android:scaleType="fitXY"
  18. android:src="@drawable/ic_lion" />
  19. <!--title-->
  20. </LinearLayout>
  21. <LinearLayout
  22. android:layout_width="match_parent"
  23. android:layout_height="match_parent"
  24. android:background="#4433ff"
  25. android:orientation="vertical"
  26. android:padding="10dp">
  27. <TextView
  28. android:id="@+id/row_product_name"
  29. android:layout_width="match_parent"
  30. android:layout_height="wrap_content"
  31. android:text="Name here"
  32. android:textColor="@color/black"
  33. android:textSize="20sp" />
  34. <TextView
  35. android:id="@+id/row_product_price"
  36. android:layout_width="match_parent"
  37. android:layout_height="wrap_content"
  38. android:text="Price"
  39. android:textColor="@color/white"
  40. android:textSize="15sp" />
  41. </LinearLayout>
  42. </LinearLayout>
output

Now you can see that we made one row and all the ListRow follows this design.

Step 3:

Go to the ProductListActivity.class and paste the following code,

  1. package com.khawarislam.customclass;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.view.View;
  8. import android.widget.AdapterView;
  9. import android.widget.ListView;
  10. import android.widget.Toast;
  11. import java.util.ArrayList;
  12. public class ProductListActivityextends Activity
  13. {
  14. ListViewmListView;
  15. Context context;
  16. ArrayList < Product > arrayList;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState)
  19. {
  20. super.onCreate(savedInstanceState);
  21. context = this;
  22. setContentView(R.layout.activity_product_list);
  23. arrayList = new ArrayList < > ();
  24. final ProductDatasourcemProductDatasource = new ProductDatasource();
  25. arrayList = mProductDatasource.getItemarrayList();
  26. mListView = (ListView) findViewById(R.id.product_listview);
  27. ProductAdaptarmProductAdaptar = new ProductAdaptar(context, arrayList);
  28. mListView.setAdapter(mProductAdaptar);
  29. mListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
  30. {
  31. @Override
  32. public void onItemClick(AdapterView < ? > parent, View view, intposition, long id)
  33. {
  34. Product mProduct = arrayList.get(position);
  35. mProductDatasource.insert(mProduct);
  36. Toast.makeText(context, mProduct.getProduct_name(), Toast.LENGTH_SHORT).show();
  37. }
  38. });
  39. }
  40. }
Step 4:

What is Adapter
?

What is ArrayAdapter?

Default Vs Custom Adapter

Default Adapter:

Custom Adapter:

Create a custom adapter class ProductAdapter.java,

java

Now as we can see in this image we declared two methods: one method is to make object of arraylist of product and another we declare two textview and assign it into datasource.

What is LayoutInflater

For Example:
  1. LayoutInflaterinflater = (LayoutInflater) context .getSystemService(Context. LAYOUT_INFLATER_SERVICE);
  2. View rowView = inflater.inflate(R.layout.row_product_list, parent, false);

Read more articles on Android: