Hai guys, pada kesempatan kali ini gue akan share tutorial cara membuat ListView pada Android Studio. ListView sendiri merupakan widget bawaan android yang berfungsi untuk menampilkan data ke dalam sebuah daftar,secara otomatis dapat di scroll bila jumlah datanya banyak. Untuk memasukkan data ke dalam sebuah ListView digunakan adapter yang umumnya dari Array atau dari Database.
Membuat projek baru
Membuat projek baru
Buat Project baru di Android Studio, Beri nama Project misal "Listview Contoh", untuk package name terserah kalian saja, untuk minimum SDK disini gue memilih Android Kitkat serta Activitinya pilih Empty Activity supaya codenya clean, kita pakai nama bawaan MainActivity saja. Tunggu sampai proses gradle selesai.
Membuat Layout
Buka activity_main.xml, tambahkan ListView kedalam xml nya.
Membuat Layout
activity_main.xml
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:background="#fff" 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.baperdev.listviewcontoh.MainActivity"> <ListView android:id="@+id/listview_fruit" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </RelativeLayout>
Kemudian baru kita edit MainActivity, kita isi sebagai berikut.
MainActivity.java
MainActivity.java
package com.baperdev.listviewcontoh; 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; public class MainActivity extends AppCompatActivity { ListView lv; String[] buah = { "apple","apricot","avocado","banana","bell pepper","bilberry","blackberry","blackcurrant","blood orange","blueberry","boysenberry","breadfruit","canary melon","cantaloupe","cherimoya","cherry","chili pepper","clementine","cloudberry","coconut","cranberry","cucumber","currant","damson","date","dragonfruit","durian","eggplant","elderberry","feijoa","fig","goji berry","gooseberry","grape","grapefruit","guava","honeydew","huckleberry","jackfruit","jambul","jujube","kiwi fruit","kumquat","lemon","lime","loquat","lychee","mandarine","mango","mulberry","nectarine","nut","olive","orange","pamelo","papaya","passionfruit","peach","pear","persimmon","physalis","pineapple","plum","pomegranate","pomelo","purple mangosteen","quince","raisin","rambutan","raspberry","redcurrant","rock melon","salal berry","satsuma","star fruit","strawberry","tamarillo","tangerine","tomato","ugli fruit","watermelon" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.listview_fruit); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, buah); lv.setAdapter(adapter); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { Toast.makeText(getApplicationContext(),"Anda memilih : " + buah[i], Toast.LENGTH_SHORT).show(); } }); } }
Sekarang coba jalankan projectnya bila berhasil nanti akan tampil seperti ini:
Lampiran:
Source Code Project : Android ListView Dasar