Android Programming -12 ArrayAdapter

Hı guys in this tutorial  We will see the use of arrayadapters in android programming.

In my previous tutorial, I talked about the  listview component. If you have not read it yet, I recommend you read it first.

https://ittutorial.org/android-programming-11-listview-component/

If you remember from the previous tutorial, we added the string values we wanted to show in listview into the arraylist. Today we will do the part showing the values we added the arraylist in listview. we will do this with ArrayAdapter.

package com.example.mac.makale;

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

public class MainActivity extends AppCompatActivity {

ListView listView;
ArrayList<String> arrayList;
ArrayAdapter<String> arrayAdapter;

@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..");
      
      arrayAdapter=new ArrayAdapter<>(YeniSayfaActivity.this,
                                      android.R.id.simple_list_item_1,
                                      arrayList);

listView.setAdapter(arrayAdapter);

}
}

If we examine the code;

We define te global variables first , then we start writing our code in the oncreate method.We add the string value we want into add into the arraylist, than we define the adapter. we create the adapter with 3 parameter.

The first parameter is the context, the second parameter is the part where the adjustments are made about how each line of the list will appear, and the last part is the part where the arraylist we want to show in listview is specified.

After setting the adapter according to the parameter values mentioned above, we set it with the adapter variable we set thanks to the setAdapter method of listview.

About Kübra Hebeş

Bilgisayar Mühendisi

One comment

  1. It’s rare to see a post like this, that shows the writer has common sense! You honestly made me think! TY-I hadn’t considered things from that angle otherwise. Will share this…

Leave a Reply

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