Android tarafında listview kontrolü ile nasıl çalışırız bu sorularınızın cevabını bu makalede bulabileceksiniz. ListView nedir diye aklınıza geliyor olabilir. Android tarafında verileri liste halinde göstermek istediğimiz zaman kullandığımız bir kontroldür. ListView örnekleri aşağıdaki görselde gösterilmiştir.
Artık uygulamalara bu gözle bakarsak hemen hemen birçok uygulamada kullandığımız sonucuna varabiliriz.
Peki Nasıl ekleriz?
Öncelikle her android uygulamasında olduğu gibi sayfamızda bir listview barındırmamız gerekecektir. Android uygulamamızda Ekran Tasarımı tarafına bir listview ekleyerek başlıyor olmamız gerekecektir.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout 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:id="@+id/listview" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" > | |
</ListView> | |
</LinearLayout> |
Ekrana eklediğimiz listview üzerindeki kontrolün nasıl görüneceği kısmını belirlemek için de aşağıdaki gibi bir layout tasarımı oluşturalım. Layout adı activity_listview.xml adında olsun.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<TextView xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/label" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
android:padding="10dip" | |
android:textSize="25dip" | |
android:textStyle="bold" > | |
</TextView> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.deney; | |
import androidx.appcompat.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.widget.ArrayAdapter; | |
import android.widget.ListView; | |
public class MainActivity extends AppCompatActivity { | |
String[] mobileArray = {"Android", "IPhone", "WindowsMobile", "Blackberry", | |
"WebOS", "Ubuntu", "Windows7", "Max OS X"}; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
ArrayAdapter adapter = new ArrayAdapter<String> | |
(this, | |
R.layout.activity_listview, | |
mobileArray); | |
ListView listView = (ListView) findViewById(R.id.listview); | |
listView.setAdapter(adapter); | |
} | |
} |