Android Open Source - Weather Auto Complete Preference






From Project

Back to project page Weather.

License

The source code is released under:

Apache License

If you think the Android project Weather listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.volitic.weather;
/*from w w  w  .  ja va  2  s .  com*/
import android.content.Context;
import android.database.Cursor;
import android.preference.EditTextPreference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.CursorAdapter;
import android.widget.EditText;


public class AutoCompletePreference extends EditTextPreference {

    private AutoCompleteTextView mAutoComplete = null;

    public AutoCompletePreference(Context context) {
        super(context);
    }

    public AutoCompletePreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AutoCompletePreference(Context context, AttributeSet attrs,
                                  int defStyle) {
        super(context, attrs, defStyle);
    }


    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 container = (ViewGroup) editText.getParent();
        String curVal = editText.getText().toString();
        // remove it from the existing layout hierarchy
        container.removeView(editText);
        // construct a new editable autocomplete object with the appropriate params
        // and id that the TextEditPreference is expecting
        mAutoComplete = new AutoCompleteTextView(getContext()){
            @Override
            protected CharSequence convertSelectionToString(Object selectedItem) {
                Cursor c = (Cursor) selectedItem;
                return c.getString(c.getColumnIndex(DatabaseHelper.COL_CITY));
            }
        };
        //mAutoComplete.setLayoutParams(params);
        mAutoComplete.setId(android.R.id.edit);
        mAutoComplete.setText(curVal);

        AutoCompleteCursorAdapter adapter = new AutoCompleteCursorAdapter(
                getContext(),
                android.R.layout.simple_list_item_1,
                null,
                new String[]{DatabaseHelper.COL_CITY},
                new int[]{android.R.id.text1},
                CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

        mAutoComplete.setAdapter(adapter);

        // add the new view to the layout
        container.addView(mAutoComplete);
    }

    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);

        if (positiveResult && mAutoComplete != null) {
            String value = mAutoComplete.getText().toString();
            if (callChangeListener(value)) {
                setText(value);
            }
        }
    }

    public EditText getEditText() {
        return mAutoComplete;
    }
}




Java Source Code List

com.volitic.weather.AboutDialog.java
com.volitic.weather.AutoCompleteCursorAdapter.java
com.volitic.weather.AutoCompletePreference.java
com.volitic.weather.DatabaseHelper.java
com.volitic.weather.MainActivity.java
com.volitic.weather.Network.java
com.volitic.weather.SettingsActivity.java
com.volitic.weather.Time.java
com.volitic.weather.Weather.java
com.volitic.weather.XmlParser.java