Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google maps 带地址的AutocompleteTextView_Google Maps_Android Asynctask_Autocompletetextview_Street Address - Fatal编程技术网

Google maps 带地址的AutocompleteTextView

Google maps 带地址的AutocompleteTextView,google-maps,android-asynctask,autocompletetextview,street-address,Google Maps,Android Asynctask,Autocompletetextview,Street Address,我已经用AutcompleteTextView创建了简单的活动。我正在尝试使用谷歌地理编码api从输入字符串中获取地址。我在日志中获取地址,但无法在自动完成视图中获取地址:/我做错了什么 public class SearchFragment extends Fragment implements OnClickListener { private ListView mListView; private Button mSearchButton; private Aut

我已经用AutcompleteTextView创建了简单的活动。我正在尝试使用谷歌地理编码api从输入字符串中获取地址。我在日志中获取地址,但无法在自动完成视图中获取地址:/我做错了什么

public class SearchFragment extends Fragment implements OnClickListener {

    private ListView mListView;
    private Button mSearchButton;
    private AutoCompleteTextView mSearchView;
    private ArrayAdapter<String> mArrayAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_search, container,
                false);

        mSearchButton = (Button) rootView
                .findViewById(R.id.fragment_search_button);
        mSearchButton.setOnClickListener(this);
        mListView = (ListView) rootView.findViewById(R.id.fragment_search_list);
        mSearchView = (AutoCompleteTextView) rootView.findViewById(R.id.fragment_search_input);
        mArrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1);
        mArrayAdapter.setNotifyOnChange(true);

        mSearchView.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {


            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                 mArrayAdapter.clear();
                 new FetchAutocompleteSuggestions().execute(buildSearchableUrl(s.toString()));

            }

            @Override
            public void afterTextChanged(Editable s) {


            }
        });

        return rootView;
    }

    private String buildSearchableUrl(String insert){
        String result = "";
        insert = insert.replace(" ", "+");
        String intro = "http://maps.googleapis.com/maps/api/geocode/json?address=";
        String outro = "&sensor=true&components=country:sk";
        result = intro + insert + outro;
        return result;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.fragment_search_button:
            new FetchAutocompleteSuggestions().execute(buildSearchableUrl(mSearchView.getText().toString()));
            break;
        default:
            break;
        }
    }

    private class FetchAutocompleteSuggestions extends AsyncTask<String, String, ArrayAdapter<String>>{

        private JSONParser jsonParser;


        @Override
        protected ArrayAdapter<String> doInBackground(String... params) {
            jsonParser = new JSONParser();
            String mPreparedUrl = params[0];
            JSONObject jobj = jsonParser.getJSONFromUrl(mPreparedUrl, null);
            Log.i("", mPreparedUrl);

            try{
                JSONArray array = jobj.getJSONArray("results");
                for (int i = 0; i < array.length(); i++) {
                    JSONObject childObject = array.getJSONObject(i);
                    String pom = childObject.getString("formatted_address");
                    Log.d("", pom);
                    mArrayAdapter.add(pom);
                }

            } catch (Exception e){
                Log.e(e.toString(), "Chyba");
            }           
            return mArrayAdapter;
        }

        @Override
        protected void onPostExecute(ArrayAdapter<String> result) {
            mSearchView.setAdapter(result);
        }
    }

}
public类SearchFragment扩展了片段实现OnClickListener{
私有列表视图;
专用按钮mSearchButton;
私有自动完成文本视图mSearchView;
私人ArrayaAdapter MarrayaAdapter;
@凌驾
创建时的公共void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图根视图=充气机。充气(R.layout.fragment\u搜索,容器,
假);
mSearchButton=(按钮)根视图
.findviewbyd(R.id.fragment\u search\u按钮);
mSearchButton.setOnClickListener(此);
mListView=(ListView)rootView.findViewById(R.id.fragment\u search\u list);
mSearchView=(AutoCompleteTextView)rootView.findViewById(R.id.fragment\u search\u input);
mArrayAdapter=newarrayadapter(getActivity(),android.R.layout.simple\u list\u item\u 1);
mArrayAdapter.setNotifyOnChange(true);
mSearchView.addTextChangedListener(新的TextWatcher(){
@凌驾
public void onTextChanged(字符序列、int start、int before、int count){
}
@凌驾
更改前的公共无效(字符序列、整数开始、整数计数、,
整数后){
mArrayAdapter.clear();
新的FetchAutocompleteSuggestions().execute(buildSearchableUrl(s.toString());
}
@凌驾
公共无效后文本已更改(可编辑){
}
});
返回rootView;
}
私有字符串buildSearchableUrl(字符串插入){
字符串结果=”;
insert=insert.replace(“,“+”);
字符串简介=”http://maps.googleapis.com/maps/api/geocode/json?address=";
字符串outro=“&sensor=true&components=country:sk”;
结果=介绍+插入+输出;
返回结果;
}
@凌驾
公共void onClick(视图v){
开关(v.getId()){
案例R.id.fragment\u搜索按钮:
新的FetchAutocompleteSuggestions().execute(buildSearchableUrl(mSearchView.getText().toString());
打破
违约:
打破
}
}
私有类FetchAutocompleteSuggestions扩展异步任务{
私有JSONParser JSONParser;
@凌驾
受保护的阵列适配器doInBackground(字符串…参数){
jsonParser=新的jsonParser();
字符串mPreparedUrl=params[0];
JSONObject jobj=jsonParser.getJSONFromUrl(mPreparedUrl,null);
Log.i(“,mPreparedUrl);
试一试{
JSONArray数组=jobj.getJSONArray(“结果”);
对于(int i=0;i
好的,我通过设置带有过滤器的自定义ArrayAdapter:)解决了这个问题,但我仍然存在一个严重的问题。我在我的工作地点得到了糟糕的展示:日志:04-05 23:53:53.593:D/(13976):维索科·科尔科夫,齐利纳,斯洛伐克这看起来应该像维索科·科尔科夫,伊利纳,斯洛伐克请给我时间回答你自己的问题,如果你不知道,可以给你分数!