Erkan KESER

ArrayList ile ListView Kullanımı - Java ile Android Studio Kullanımı

 Android studio ve java kullanılarak ArrayList ile ListView kullanımı anlatılmaktadır.

Uygulama: 2 Activity'den oluşan uygulamada 1.activity'de veri ekleme ve listview de listeleme yapılmaktadır.  Listview deki veriye tıklanarak 2.activitye geçilebilir. 

Main.Xml Dosyası

<?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:id="@+id/main"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   android:padding="20dp"
   tools:context=".MainActivity">
   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal">
       <EditText
           android:id="@+id/editText1"
           android:layout_width="wrap_content"
           android:layout_height="50dp"
           android:layout_weight="1"
           android:ems="10"
           android:hint="İsim Gir"
           android:inputType="text" />
       <Button
           android:id="@+id/button1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:text="Ekle" />
   </LinearLayout>
   <ListView
       android:id="@+id/listView1"
       android:layout_width="match_parent"
       android:layout_height="match_parent" />
</LinearLayout>

Main.Java Dosyası

public class MainActivity extends AppCompatActivity {
   Button btnEkle; EditText etAd; ListView lvisimler;
   ArrayList<String> isimListesi = new ArrayList<>();
   ArrayAdapter<String> adapter;
   
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       EdgeToEdge.enable(this);
       setContentView(R.layout.activity_main);
       ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
           Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
           v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
           return insets;
       });
      
       btnEkle = findViewById(R.id.button1);
       etAd = findViewById(R.id.editText1);
       lvisimler = findViewById(R.id.listView1);
       adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, isimListesi);
       lvisimler.setAdapter(adapter);
       
       btnEkle.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               String ad = etAd.getText().toString();
               isimListesi.add(ad);
               adapter.notifyDataSetChanged();
               etAd.getText().clear();
           }
       });
       
       lvisimler.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               Intent  detaySayfasi = new Intent(getApplication(), Detay.class);
               detaySayfasi.putExtra("isim", isimListesi.get(position));
               startActivity(detaySayfasi);
           }
       });

       lvisimler.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
           @Override
           public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
               isimListesi.remove(position);
               adapter.notifyDataSetChanged();
               return false;
           }
       });
   } // oncreate
}//main

Detay.Xml Dosyası

<?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:id="@+id/main"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:gravity="center_horizontal"
   android:orientation="vertical"
   android:padding="20dp"
   tools:context=".Detay">
   <TextView
       android:id="@+id/textView2"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_marginTop="30dp"
       android:layout_marginBottom="20dp"
       android:gravity="center"
       android:text="Sonuç Alanı"
       android:textSize="30sp" />
   <Button
       android:id="@+id/button2"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="Geri Dön" />
</LinearLayout>

Detay.Java Dosyası

public class Detay extends AppCompatActivity {
   TextView metin; Button btnGeri;
   
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       EdgeToEdge.enable(this);
       setContentView(R.layout.activity_detay);
       ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
           Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
           v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
           return insets;
       });
       
       metin = findViewById(R.id.textView2);
       btnGeri = findViewById(R.id.button2);
       Intent anasayfa = getIntent();
       String isim =  anasayfa.getStringExtra("isim");
       metin.setText(isim);
       
       btnGeri.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent geri = new Intent(getApplication(), MainActivity.class);
               startActivity(geri);
           }
       	});
   	  } // oncreate
   
}//detay