Mehmet Ethem SULAN :: Android :: Android ListPreference OnPreferenceChangeListener Sample

Android ile PreferenceScreen içinde ListPreference içinde seçilen değer değiştiğinde gerçekleşen olay için basit bir örnek yazdım. findViewById() metoduna benzer bir erişim metodu var.

listPreference=(ListPreference) findPreference("customPreferences");

PreferenceScreen içinde aşağıdaki resimde görüldüğü gibi bazı özellikler tanımlayabiliyoruz. Android settings bununla yazılmış.

Çalıştırınca

MyPreferences.java

package com.preference.sam;

import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceChangeListener;
import android.util.Log;
import android.widget.Toast;

public class MyPreferences extends PreferenceActivity {

	ListPreference listPreference;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//R.xml.pref ekliyoruz.
		addPreferencesFromResource(R.xml.pref);
//android:key="customPreferences" xml/pref.xml içindeki isim
		listPreference=(ListPreference) findPreference("customPreferences");
		listPreference.setOnPreferenceChangeListener(onPreferenceChangeListener);

	}

	OnPreferenceChangeListener onPreferenceChangeListener=new OnPreferenceChangeListener() {

		@Override
		public boolean onPreferenceChange(Preference preference, Object newValue) {
//Seçilen isme ait numara toast olarak görünüyor ve toast indeksi de log attiriyorum.
//pref.xml içinde android:entries="@array/names" ile görünecek bir dizi atiyoruz ve
//android:entryValues="@array/stud_num" kod ile de o diziye ait değerleri yazıyoruz.
//Bu iki dizi values/strings.xml içinde String array olarak tanımlanmıştır.
			Toast.makeText(getApplicationContext(), "Secilen ogrencinin okul numarasi : "+newValue, Toast.LENGTH_LONG).show();
			int index=listPreference.findIndexOfValue((String) newValue);
			Log.e(">>>>>>>>>>>>>>>>", "Index of selected value :" + index);
			return false;
		}
	};

}

pref.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <PreferenceCategory android:title="Categori 1">
    	<ListPreference android:title="List Of Name"
    		android:key="customPreferences"
    		android:entries="@array/names"
    		android:entryValues="@array/stud_num">
    	</ListPreference>
    </PreferenceCategory>

</PreferenceScreen>

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, PreferenceSamActivity!</string>
    <string name="app_name">PreferenceSam</string>
    <string-array name="names">
        <item >Ethem</item>
        <item >Isil</item>
    </string-array>
    <string-array name="stud_num">
        <item >060401029</item>
        <item > 1121000300</item>
    </string-array>

</resources>

Bunu main activity yaptım.
AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.preference.sam"
    android:versionCode="1"
    android:versionName="1.0" >

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity android:name="MyPreferences">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

    </application>

</manifest>

Download PreferenceSam.zip

Comments

There is one comment for this post.

  1. Kevlyn on Aralık 7, 2011 8:18 am

    Kewl you slohud come up with that. Excellent!

Write a Comment

Let me know what you think?