Android Programming -11 Listview Component

Hi guys in this tutorial  we will talk about the listview component.

In my previous tutorial, I talked about the sending and receiving parameters between two activities. If you have not read it yet, I recommend you read it first.

https://ittutorial.org/android-programming-10-sending-parameters-between-two-activities/

First of all, I want to talk about the situations in which we use this component. Think of your application as you want to show a lot of text, but using textview for all of them is a waste of time and meaningless.

In such cases, we write the text to a list, thanks to the listview component in the interface of our application we have shown. For example, you are using an application where you are looking for the most suitable hotel. a lot of hotels are on the list. for each hotel we don’t use a textview. we just use one listview and an arraylist.

if we learned why we need listview component, we can start the coding of xml for listview.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    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=".MainActivity">

<ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listview_exmple"
        android:layout_marginTop="20dp"
        ></ListView>

</RelativeLayout>

The coding of this component is very close to the components we have seen before.  First  we determine the indispensabl hight and weight attribute of listview component.   Then we specify the id value for us to operate at java class on this component. And finally, since I want a space of 20 dp between the listview and the top of the screen. One of the most important points in XML coding is that every opened tag should be closed.

package com.example.mac.makale;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    
    
ListView listView;
ArrayList<String> arrayList;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
      listView=(ListView)findviewByid(R.id.listview_exmple);
      
      arrayList=new ArrayList<>();
      
      arrayList.add("Bu");
      arrayList.add("bir");
      arrayList.add("Android");
      arrayList.add("Eğitim");
      arrayList.add("Serisidir..");
      
      
    }
}

If we look at our Java codes.First of all, we define the global variable of listview that we will use in java encoding. We also create the Arraylist variable. We write the values that we want to show on this component in Listview and set the listview as such. In this lesson we will only see how to assign value to Arraylist. I will explain how connected the listview with arraylist in our next tutorial.

If we continue to review the code, With the findviewbyid method, we connect the id value that we set in xml coding and the listview variable we define globally. then we call the arraylist’s constraction function to create it empty. And then we add the string values that we want to add to the arraylist thanks to the add method.

If you want to create your arraylist with a different type of data, you have to do this during the first definition.

About Kübra Hebeş

Bilgisayar Mühendisi

Leave a Reply

Your email address will not be published. Required fields are marked *