Android listpreference not showing default value



I'm trying to show the default value of a ListPreference in my Preferencescreen, but it is not working, I don't know what I'm missing here, and here is what I tried so far:



pData = (ListPreference) findPreference("prefData");

pData.setLayoutResource(R.layout.pref_data);

pData.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

// @Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
SharedPreferences customSharedPreference = getSharedPreferences("prefData", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = customSharedPreference.edit();
editor.putString("listentries", (String) newValue);
editor.commit();

SecurePreferences mSessao = new SecurePreferences(getApplicationContext(), "sessao");
mSessao.put("filtro_pedidos_data", (String) newValue);

TextView textValue = (TextView) findViewById(R.id.escolhaData);

int index = pData.findIndexOfValue(newValue.toString());


if (index != -1) {
textValue.setText(pData.getEntries()[index]);
}

System.out.println("Escolheu >>> " + mSessao.getString("filtro_pedidos_data"));

return true;
}
});


XML:



<ListPreference


android:dialogTitle="@string/fDataTitulo"
android:summary="@string/fDataSummary"
android:key="prefData"
android:entries="@array/listentries"
android:entryValues="@array/listvalues"
android:defaultValue="Mais Antiga"/>


String-array:



<string-array name="listentries">

<item>Mais Recente</item>
<item>Mais Antiga</item>

</string-array>

<string-array name="listvalues">
<item>Mais Recente</item>
<item>Mais Antiga</item>

</string-array>


Pref_data_xml:



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2"
android:gravity="center"
android:paddingTop="10dp"
android:paddingBottom="10dp">


<TextView
android:id="@+id/filtrarData"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Refinar Data"
android:textColor="@color/black"
android:textSize="@dimen/detalhe_cliente"
android:entries="@array/listentries"
android:entryValues="@array/listvalues"
android:layout_weight="1"
android:padding="10dp"
android:gravity="left" />

<TextView
android:id="@+id/escolhaData"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text=""
android:textColor="@color/blue_dark"
android:textSize="@dimen/detalhe_cliente"
android:entries="@array/listentries"
android:entryValues="@array/listvalues"
android:layout_weight="1"
android:padding="10dp"
android:gravity="right" />





</LinearLayout>



</LinearLayout>

No comments:

Post a Comment