Last active
March 20, 2019 15:37
-
-
Save cemtopkaya/b62b83c0a5b25107a71951e116b31f53 to your computer and use it in GitHub Desktop.
Android Spinner Adapter Örneği
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
<!-- src/main/res/values/activity_main.xml --> | |
<?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" | |
android:orientation="vertical" | |
tools:context=".MainActivity"> | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:labelFor="@id/spnUniversiteler" | |
android:text="Üniversitenizi Seçiniz" | |
android:textAppearance="@style/TextAppearance.AppCompat.Display1" /> | |
<Spinner | |
android:id="@+id/spnUniversiteler" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:dropDownWidth="wrap_content" | |
android:spinnerMode="dropdown" /> | |
</LinearLayout> |
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.cemtopkaya.spinner_tutorial; | |
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.Button; | |
import android.widget.Spinner; | |
import android.widget.Toast; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
public class MainActivity extends AppCompatActivity { | |
ArrayList<String> arrayAdapter_in_verisi; | |
// Views | |
private Spinner spnUniversiteler; | |
private Button btnVeriyiDegistir; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
initCoponents(); | |
ArrayAdapter adp = createAdapter(); | |
spnUniversiteler.setAdapter(adp); | |
spnUniversiteler.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { | |
@Override | |
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { | |
Toast.makeText( | |
MainActivity.this, | |
"Tıklandı: " + parent.getAdapter().getItem(position).toString(), | |
Toast.LENGTH_LONG).show(); | |
} | |
@Override | |
public void onNothingSelected(AdapterView<?> parent) { | |
} | |
}); | |
} | |
private void initCoponents() { | |
spnUniversiteler = findViewById(R.id.spnUniversiteler); | |
btnVeriyiDegistir = findViewById(R.id.btn); | |
btnVeriyiDegistir.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
arrayAdapter_in_verisi.add("Sonradan eklenen üni: İnönü Üni."); | |
} | |
}); | |
} | |
private ArrayAdapter createAdapter() { | |
/** | |
* Eğer DATA değişmeyecekse resource'dan çekilebilir String[] | |
* R.array.sarrUniversiteler, // DATA | |
* | |
* 1) Çalışma zamanında değişecekse bu dizinin elemanlarını ArrayList değişkenine almalı | |
* ArrayList<String> veri = new ArrayList<>( | |
* Arrays.asList( | |
* getResources().getStringArray(R.array.sarrUniversiteler) | |
* ) | |
* ); | |
* | |
* Böylece sonrasında veri.add("Yeni eleman") diyebilir, ArrayAdapter de listeyi günceller | |
* | |
* 2) Eğer ArratAdapter'in arkasındaki veri değişmeyecek ve resource içindeki veriyi kullanacaksa | |
* ArrayAdapter.createFromResource( | |
* this, // Context | |
* R.array.sarrUniversiteler, // DATA: String dizisi olarak depolanmış elemanlar | |
* android.R.layout.simple_spinner_item // VIEW: Android içinde gelen 2 spinner layoutundan biri | |
* ); | |
* | |
*/ | |
ArrayAdapter adp = degisebilir_veriye_sahip_arrayAdapter(); | |
//ArrayAdapter adp = resource_icindeki_degerlerle_oluturulan_degismez_veriye_sahip_arrayAdapter(); | |
/** Dilersek Adapter'in UI daki view'ını değiştirebiliriz | |
* adp.setDropDownViewResource(android.R.layout.simple_spinner_item); | |
* veya | |
* adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); | |
*/ | |
return adp; | |
} | |
private ArrayAdapter resource_icindeki_degerlerle_oluturulan_degismez_veriye_sahip_arrayAdapter() { | |
return ArrayAdapter.createFromResource( | |
this, // Context | |
R.array.sarrUniversiteler, // DATA: String dizisi olarak depolanmış elemanlar | |
android.R.layout.simple_spinner_item // VIEW: Android içinde gelen 2 spinner layoutundan biri | |
); | |
} | |
/** | |
* Bu adapter arkasındaki veri değiştirilebilir olsun diye yeni bir ArrayList yaratılmıştır. | |
* | |
* @return | |
*/ | |
private ArrayAdapter degisebilir_veriye_sahip_arrayAdapter() { | |
arrayAdapter_in_verisi = new ArrayList<>( | |
Arrays.asList( | |
getResources().getStringArray(R.array.sarrUniversiteler) | |
) | |
); | |
return new ArrayAdapter(this, android.R.layout.simple_spinner_item, arrayAdapter_in_verisi); | |
} | |
} |
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
<!-- src/res/values/strings.xml --> | |
<resources> | |
<string name="app_name">Spinner Tutorial</string> | |
<string-array name="sarrUniversiteler"> | |
<item>İ.T.Ü.</item> | |
<item>ODTÜ</item> | |
<item>YTÜ</item> | |
<item>Boğaziçi Üni.</item> | |
</string-array> | |
</resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment