Ich habe an Googles Kurs Sonnenschein App gearbeitet und wollte meine persönliche Note in es setzen, so dass ich den Benutzer seine Stadt mit einem Hybrid von EditTextPreference und AutoCompleteTextView in gezeigt angegeben hier:android - getDialog gibt keine Instanz von AlertDialog zurück
public class AutoCompleteEditTextPreference extends EditTextPreference {
private static String[] list;
private boolean isValid = true;
private Dialog dialog;
public AutoCompleteEditTextPreference(Context context) {
super(context);
}
public AutoCompleteEditTextPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AutoCompleteEditTextPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
/**
* the default EditTextPreference does not make it easy to
* use an AutoCompleteEditTextPreference field. By overriding this method
* we perform surgery on it to use the type of edit field that
* we want.
*/
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
// find the current EditText object
final EditText editText = (EditText) view.findViewById(android.R.id.edit);
// copy its layout params
ViewGroup.LayoutParams params = editText.getLayoutParams();
ViewGroup vg = (ViewGroup) editText.getParent();
String curVal = editText.getText().toString();
// remove it from the existing layout hierarchy
vg.removeView(editText);
// construct a new editable autocomplete object with the appropriate params
// and id that the TextEditPreference is expecting
mACTV = new AutoCompleteTextView(getContext());
mACTV.setLayoutParams(params);
mACTV.setId(android.R.id.edit);
mACTV.setText(curVal);
Arrays.sort(list);
isValid = isValid(mACTV.getText().toString());
mACTV.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
isValid = isValid(s.toString());
validate();
}
});
mACTV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
isValid = isValid(mACTV.getText().toString());
validate();
}
});
ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(),
android.R.layout.simple_dropdown_item_1line, list);
mACTV.setAdapter(adapter);
// add the new view to the layout
vg.addView(mACTV);
}
private boolean isValid(CharSequence text) {
return !text.equals("") && Arrays.binarySearch(list, text.toString()) > 0;
}
@Override
protected void showDialog(Bundle state) {
super.showDialog(state);
validate();
}
private void validate() {
dialog = getDialog();
Toast.makeText(getContext(), Boolean.toString(dialog instanceof AlertDialog), Toast.LENGTH_SHORT).show();
if (dialog instanceof AlertDialog) {
Button btn = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
btn.setEnabled(isValid);
}
}
/**
* Because the baseclass does not handle this correctly
* we need to query our injected AutoCompleteTextView for
* the value to save
*/
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (positiveResult && mACTV != null) {
String value = mACTV.getText().toString();
if (callChangeListener(value))
setText(value);
}
}
static void prepareCountriesList(Context context) {
List<String> lines = new ArrayList<>();
try {
InputStream inputStream = context.getAssets().open("cities.txt");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = bufferedReader.readLine()) != null) {
lines.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}
list = lines.toArray(new String[lines.size()]);
}
/**
* again we need to override methods from the base class
*/
public EditText getEditText() {
return mACTV;
}
private AutoCompleteTextView mACTV = null;
private final String TAG = "AutoCompleteEditTextPreference";
}
so ging alles super bis zum letzten Teil, wo ich die oK-Taste
private void validate() {
dialog = getDialog();
Toast.makeText(getContext(), Boolean.toString(dialog instanceof AlertDialog), Toast.LENGTH_SHORT).show();
if (dialog instanceof AlertDialog) {
Button btn = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
btn.setEnabled(isValid);
}
}
so versuche ich die Methode getDialog() deaktivieren wollte;
und es gibt einen Dialog, der nicht null und nicht eine Instanz von Alertdialog ist
Anyhelp auf Sie bitte den Dialog richtig oder eine andere Art und Weise bekommt die OK-Taste programmatisch