How To Implement Simple ListView In Android
To display element in a list in mobile application common approach uses is ListView.
Today I will demonstrate how we can implement a list view in android. Let's begin
Step 1. Open Android Studio, click on a new project and select an empty layout then enter your project name then hit finish.
Step 2. Now take list view in your main activity layout as bellow
<ListView
android:layout_below="@+id/tv_title"
android:id="@+id/lv_items"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
Or you can copy the below code and past on your activity_mail.xml file.
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">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Android List View"
android:textAlignment="center"
android:layout_marginBottom="10dp"/>
<ListView
android:layout_below="@+id/tv_title"
android:id="@+id/lv_items"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ ListView>
</RelativeLayout>
Step 3. Now open your MainActivity.java file and initialize an array list of an item which we will show on list view as below.
ArrayList listItems=new ArrayList<>();
Now add some data to the array list as below.
public void load_items(){
listItems.add("Andoid");
listItems.add("Java");
listItems.add("C");
listItems.add("C++");
listItems.add("Nodejs");
listItems.add("AngularJs");
}
Now simply call the load_items() function on your onCreate method and to bind the data with the view we need an adapter, here we will use an ArrayAdapter as below
final ArrayAdapter stringArrayAdapter=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,listItems);
yourlistview.setAdapter(stringArrayAdapter);
If you want to display the items name on click of an item then we can implement the onitemclick method as below
yourlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),stringArrayAdapter.getItem(position),Toast.LENGTH_SHORT).show();
}
});
Any confusion just copy the below code and past on your MainActivity.java class
package com.sahebul.simplelistview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList listItems=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView itemListView=(ListView)findViewById(R.id.lv_items);
//load the item list to display
load_items();
//set a string adapter
final ArrayAdapter stringArrayAdapter=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,listItems);
itemListView.setAdapter(stringArrayAdapter);
//set onclick listener to display the click item name in a toast
itemListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),stringArrayAdapter.getItem(position),Toast.LENGTH_SHORT).show();
}
});
}
public void load_items(){
listItems.add("Andoid");
listItems.add("Java");
listItems.add("C");
listItems.add("C++");
listItems.add("Nodejs");
listItems.add("AngularJs");
}
}
0 Comments
Leave a reply
Your email address will be private. Required fields are marked with *







