The ListView is certainly one of the most used components by Android programmers in their applications.
Why? Because the concept is easy to understand and the result looks nice.
But a lot of tutorials are made with complicated ListView examples.
In this tutorial we are going to see how to use it in the most easy way.
We will display 2 ListViews, one with Strings and the other one with Integers.
For any list we need to use an Adapter which transforms our raw data (from the Array) into readable data for the ListView.
The BadprogMainActivity is the MainActivity class.
// BadproG.com package com.badprog.android.badproglistview; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; public class BadprogMainActivity extends AppCompatActivity { static private String[] arrayString = new String[] { "Alexander", "Barbara", "Charles", "Dave", "Eric", "Fred", "Greg", "Henry", "Iren", "James", "Kayle", "Lucy", "Mike", "Nao", "Olaf", "Patrik", "Quentin", "Roger", "Steven", "Tracy", "Ursulla", "Vera", "Wilfrid", "Xao", "Yoni", "Zian" }; private Integer[] arrayInt = new Integer[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; private ArrayAdapter<String> adapterString; private ArrayAdapter<Integer> adapterInt; private ListView listView1; private ListView listView2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); adapterString = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayString); listView1 = findViewById(R.id.list1); listView1.setAdapter(adapterString); adapterInt = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayInt); listView2 = findViewById(R.id.list2); listView2.setAdapter(adapterInt); } }
<?xml version="1.0" encoding="utf-8"?> <!-- BadproG.com --> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.badprog.android.badproglistview.BadprogMainActivity"> <ListView android:id="@+id/list1" android:layout_width="0dp" android:layout_height="178dp" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:background="@android:color/holo_orange_light" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <ListView android:id="@+id/list2" android:layout_width="0dp" android:layout_height="304dp" android:layout_marginBottom="8dp" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:background="@android:color/holo_green_dark" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/list1" app:layout_constraintVertical_bias="0.0" /> <android.support.constraint.Guideline android:id="@+id/guideline5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" app:layout_constraintGuide_percent="0.052083332" /> <android.support.constraint.Guideline android:id="@+id/guideline6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" app:layout_constraintGuide_begin="134dp" /> </android.support.constraint.ConstraintLayout>
Good job you did it!
Easy to create this ListView can be improved with a custom Adapter with tons of features.
Add new comment