Im very new to android and Im trying to load/persist values from my customized DialogPreference. Currently, this fails because findViewById returns null. Is the way I (try) to do it correct? How do I get access to my EditText widgets in the code?
public class AddressDialogPreference extends DialogPreference {
public AddressDialogPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setDialogLayoutResource(R.layout.address_dialog);
}
@Override
protected void onBindDialogView(View view) {
EditText idField = (EditText) view.findViewById(R.id.hostID);
EditText ipField = (EditText) view.findViewById(R.id.hostIP);
SharedPreferences pref = getSharedPreferences();
idField.setText(pref.getString(getKey() + "_id","ExampleHostname"));
ipField.setText(pref.getString(getKey() + "_ip","192.168.1.1"));
super.onBindDialogView(view);
}
@Override
protected void onDialogClosed(boolean positiveResult) {
if(!positiveResult)
return;
Dialog myDial = getDialog();
EditText idField = (EditText) myDial.findViewById(R.id.hostID);
EditText ipField = (EditText) myDial.findViewById(R.id.hostIP);
SharedPreferences.Editor editor = getEditor();
editor.putString(getKey() + "_id",idField.getText().toString());
editor.putString(getKey() + "_ip",ipField.getText().toString());
}
address_dialog.xml:
<TextView
android:text="Insert IP address"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/hostIP" />
<TextView
android:text="Insert identifier"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_cont开发者_StackOverflow中文版ent"
android:id="@+id/hostID" />
Ok I found it out myself. Well, I still do not know what caused the error, but I did a lot of changes to the layout and code and suddenly it just worked. I tried to revert to the code that I posted here, but I cannot reproduce the error. Im posting my working code, so anybody who runs into this problem, may use it.
An admin may also choose to delete this post, as it may be not possible to reproduce the error.
Here is the layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:text="Insert IP address"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/AddressBox" />
<TextView
android:text="Insert identifier"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/HostnameBox" />
</LinearLayout>
and the AddressDialogPreference.java:
public class AddressDialogPreference extends DialogPreference {
private EditText ipBox;
private EditText hostBox;
public AddressDialogPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setDialogLayoutResource(R.layout.address_dialog);
}
@Override
protected void onBindDialogView(View view) {
ipBox = (EditText) view.findViewById(R.id.AddressBox);
hostBox = (EditText) view.findViewById(R.id.HostnameBox);
SharedPreferences pref = getSharedPreferences();
hostBox.setText(pref.getString(getKey() + "_host","ExampleHostname"));
ipBox.setText(pref.getString(getKey() + "_ip","192.168.1.1"));
super.onBindDialogView(view);
}
@Override
protected void onDialogClosed(boolean positiveResult) {
if(!positiveResult)
return;
SharedPreferences.Editor editor = getEditor();
editor.putString(getKey() + "_host",hostBox.getText().toString());
editor.putString(getKey() + "_ip",ipBox.getText().toString());
editor.commit();
super.onDialogClosed(positiveResult);
}
}
精彩评论