安卓android的地方阿比提供了自动自我暗示?暗示、阿比、自我、地方

2023-09-05 07:04:38 作者:百鬼南行

我很新的谷歌Android地图我写了下面的程序在Android的显示自动sugesstion当我键入自动完成文本框是输入要去的URL文本,但放出来没有显示在节目中,请您及时看一次,让我知道我做了错误的。

  ExampleApp.java
包com.example.exampleapp;

进口java.io.IOException异常;
进口java.io.InputStreamReader中;
进口java.net.HttpURLConnection中;
进口java.net.MalformedURLException;
进口的java.net.URL;
进口java.net.URLEn codeR;
进口的java.util.ArrayList;

进口org.json.JSONArray;
进口org.json.JSONException;
进口org.json.JSONObject;

进口android.app.Activity;
进口android.content.Context;
进口android.os.Bundle;
进口android.view.LayoutInflater.Filter;
进口android.view.View;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.ArrayAdapter;
进口android.widget.AutoCompleteTextView;

进口android.widget.Filterable;
进口android.widget.Toast;

公共类ExampleApp中扩展了活动实施OnItemClickListener {

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        AutoCompleteTextView autoCompView =(AutoCompleteTextView)findViewById(R.id.autocomplete);
        autoCompView.setAdapter(新PlacesAutoCompleteAdapter(这一点,R.layout.list_item));
    }

    @覆盖
    公共无效onItemClick(适配器视图<>为arg0,查看ARG1,INT ARG2,长ARG3){
        // TODO自动生成方法存根
        字符串str =(字符串)arg0.getItemAtPosition(ARG2);
        Toast.makeText(这一点,STR,Toast.LENGTH_SHORT).show();
    }


}
类PlacesAutoCompleteAdapter扩展ArrayAdapter<字符串>实现过滤的{
    私人的ArrayList<字符串> resultList;

    公共PlacesAutoCompleteAdapter(上下文的背景下,INT textViewResourceId){
        超(背景下,textViewResourceId);
    }
    私有静态最后弦乐PLACES_API_BASE =htt​​ps://maps.googleapis.com/maps/api/place;
    私有静态最后弦乐TYPE_AUTOCOMPLETE =/自动完成;
    私有静态最后弦乐OUT_JSON =/ JSON;

    私有静态最后弦乐API_KEY =;
    @覆盖
    公众诠释getCount将(){
        返回resultList.size();
    }

    @覆盖
    公共字符串的getItem(INT指数){
        返回resultList.get(指数);
    }

    @覆盖
    公共android.widget.Filter用getFilter(){
        过滤器过滤器=新的过滤器(){
            保护FilterResults performFiltering(CharSequence的约束){
                FilterResults filterResults =新FilterResults();
                如果(约束!= NULL){
                    //获得自动完成的结果。
                    resultList =自动完成(constraint.toString());

                    //分配数据到FilterResults
                 // filterResults.values​​ = resultList;
                  // filterResults.count = resultList.size();
                }
                返回filterResults;
            }

            私人的ArrayList<字符串>自动完成(字符串输入){
                ArrayList的<字符串> resultList = NULL;

                HttpURLConnection的康恩= NULL;
                StringBuilder的jsonResults =新的StringBuilder();
                尝试 {
                    StringBuilder的SB =新的StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON);
                    sb.append(传感器=假放大器;关键=+ API_KEY);
                    sb.append(&放大器;组件=国家:英国);
                    sb.append(&放大器;输入=+ URLEn coder.en code(输入,UTF8));

                    网址URL =新的URL(sb.toString());
                    康恩=(HttpURLConnection类)url.openConnection();
                    InputStreamReader中的=新的InputStreamReader(conn.getInputStream());

                    //将成果转化为一个StringBuilder
                    INT读取;
                    的char [] BUFF =新的char [1024];
                    而((读= in.read(BUFF))!= -1){
                        jsonResults.append(BUFF,0,读);
                    }
                }赶上(MalformedURLException异常E){
                   // Log.e(LOG_TAG,错误处理宿API URL,E);
                    返回resultList;
                }赶上(IOException异常E){
                    //Log.e(LOG_TAG,错误连接的地方API,E);
                    返回resultList;
                } 最后 {
                    如果(conn将!= NULL){
                        conn.disconnect();
                    }
                }

                尝试 {
                    //创建一个从结果的JSON对象层次
                    JSONObject的jsonObj =新的JSONObject(jsonResults.toString());
                    JSONArray predsJsonArray = jsonObj.getJSONArray(predictions);

                    //从提取的结果将说明
                    resultList =新的ArrayList<字符串>(predsJsonArray.length());
                    的for(int i = 0; I< predsJsonArray.length();我++){
                        resultList.add(predsJsonArray.getJSONObject(我).getString(说明));
                    }
                }赶上(JSONException E){
                   // Log.e(LOG_TAG,无法处理JSON的结果,E);
                }

                返回resultList;
            }
            保护无效publishResults(CharSequence的约束,FilterResults结果){
                如果(结果!= NULL){
                    notifyDataSetChanged();
                }
                其他 {
                    notifyDataSetInvalidated();
                }
            }

            @覆盖
            公共布尔onLoadClass(类为arg0){
                // TODO自动生成方法存根
                返回false;
            }};
        返回(android.widget.Filter)过滤器;
    }
}

FilterResults.java

包com.example.exampleapp;

进口的java.util.ArrayList;

公共类FilterResults {

    受保护的ArrayList<字符串>值;
    受保护的诠释计数;

}


我上面写的code,但在应用程序关闭。请参阅一次,让我知道我在做错误的上述code?
提前致谢..........
 

解决方案

下面为c片段的$ C $我都用当我有类似的问题。我给你一个建议提出,D'NT把你的整个code只有一个Java文件。使单独的方法/类在code使用的是每一个不同的任务。它会帮助你更容易地跟踪它。

这是我的PlacesAutoCompleteAdapter.java文件。

 包com.inukshk.adapter;

进口的java.util.ArrayList;

进口android.content.Context;
进口android.widget.ArrayAdapter;
进口android.widget.Filter;
进口android.widget.Filterable;

进口com.inukshk.CreateInukshk.CreateInukshk;

公共类PlacesAutoCompleteAdapter扩展ArrayAdapter<字符串>器物
        可过滤{
    私人的ArrayList<字符串> resultList;

    公共PlacesAutoCompleteAdapter(上下文的背景下,INT textViewResourceId){
        超(背景下,textViewResourceId);
    }

    @覆盖
    公众诠释getCount将(){
        返回resultList.size();
    }

    @覆盖
    公共字符串的getItem(INT指数){
        返回resultList.get(指数);
    }

    @覆盖
    公共过滤用getFilter(){
        过滤器过滤器=新的过滤器(){
            @覆盖
            保护FilterResults performFiltering(CharSequence的约束){
                FilterResults filterResults =新FilterResults();
                如果(约束!= NULL){
                    //获得自动完成的结果。

                    resultList = CreateInukshk.autocomplete(约束
                            的ToString());

                    //分配数据到FilterResults
                    filterResults.values​​ = resultList;
                    filterResults.count = resultList.size();
                }
                返回filterResults;
            }

            @覆盖
            保护无效publishResults(CharSequence的约束,
                    FilterResults结果){
                如果(结果= NULL和放大器;!&安培; results.count> 0){
                    notifyDataSetChanged();
                } 其他 {
                    notifyDataSetInvalidated();
                }
            }
        };
        返回过滤器;
    }
}
 
谷歌发布安卓各版本占比 Android 9 Pie 系统份额超过 10

下面是你必须做什么内部的OnCreate()的活动。

  autoCompView =(AutoCompleteTextView)findViewById(R.id.editloc);
        autoCompView.setAdapter(新PlacesAutoCompleteAdapter(这一点,
                R.layout.list_item));
 

下面是静态的字符串我已经使用的应用程序。

 私有静态最后弦乐PLACES_API_BASE =htt​​ps://maps.googleapis.com/maps/api/place;
    私有静态最后弦乐TYPE_AUTOCOMPLETE =/自动完成;
    私有静态最后弦乐OUT_JSON =/ JSON;
    私有静态最后弦乐API_KEY =你的API密钥;
 

和最后在这里,您将在适配器使用的方法。

 公共静态的ArrayList<字符串>自动完成(字符串输入){

        ArrayList的<字符串> resultList = NULL;

        HttpURLConnection的康恩= NULL;
        StringBuilder的jsonResults =新的StringBuilder();
        尝试 {
            StringBuilder的SB =新的StringBuilder(PLACES_API_BASE
                    + TYPE_AUTOCOMPLETE + OUT_JSON);
            sb.append(传感器=假放大器;关键=+ API_KEY);
            // sb.append(&放大器;组件=国家:英国);
            sb.append(&放大器;输入=+ URLEn coder.en code(输入,UTF8));

            网址URL =新的URL(sb.toString());
            康恩=(HttpURLConnection类)url.openConnection();
            InputStreamReader中的=新的InputStreamReader(conn.getInputStream());

            //将成果转化为一个StringBuilder
            INT读取;
            的char [] BUFF =新的char [1024];
            而((读= in.read(BUFF))!= -1){
                jsonResults.append(BUFF,0,读);
            }
        }赶上(MalformedURLException异常E){
            Log.e(TAG,错误处理宿API URL,E);
            返回resultList;
        }赶上(IOException异常E){
            Log.e(TAG,错误连接的地方API,E);
            返回resultList;
        } 最后 {
            如果(conn将!= NULL){
                conn.disconnect();
            }
        }

        尝试 {
            //创建一个从结果的JSON对象层次
            JSONObject的jsonObj =新的JSONObject(jsonResults.toString());
            JSONArray predsJsonArray = jsonObj.getJSONArray(predictions);

            //从提取的结果将说明
            resultList =新的ArrayList<字符串>(predsJsonArray.length());
            的for(int i = 0; I< predsJsonArray.length();我++){
                resultList.add(predsJsonArray.getJSONObject(我).getString(
                        描述));
            }

        }赶上(JSONException E){
            Log.e(TAG,无法处理JSON的结果,E);
        }

        返回resultList;
    }
 

编辑:

我想您所指定的list_item.xml如下图所示。

 < XML版本=1.0编码=UTF-8&GT?;
< TextView中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT/>
 

尝试一下。希望它会帮助你。

更新:

 您必须手动附加调试器在仿真写着等待调试器附加。在Netbean的菜单栏中,单击调试,然后选择连接调试......。你必须从流程选择你的包下拉列表中,然后单击连接。这应该这样做。
这在Netbeans的7反正。
 

或者你可以尝试下面还的事情。

 你有两个选择:连接调试器,或者杀死进程。

看你的logcat,你有尝试连接到每个应用程序,这哪里是忽略第二调试器的消息都来自至少两种不同的桌面应用程序。

如果,比如说,是用ADT插件运行Eclipse会发生这种情况,并且独立DDMS同时。我不知道你正在运行或什么NetBeans插件做什么,但我会搞清楚,如果你有争夺控制两个不同的东西开始。
 

编辑: 如何文本AutoCompleteTextView的

只是写这篇code段。

  autoCompView.setOnItemClickListener(新OnItemClickListener(){

            @覆盖
            公共无效onItemClick(适配器视图<>为arg0,查看ARG1,INT ARG2,
                    长ARG3){
                // TODO自动生成方法存根
                YOURTEXT = autoCompView.getText()的toString()。
                //新AsyncGetAutoPlace()。执行(autoCompView.getText()
                //的ToString()修剪())。
            }
        });
 

I am very new to android Google maps i write the following program for displaying the auto sugesstion in the android when i am type the text in the Autocomplete text box it is going the input to the url but the out put is not showing in the program .please see once and let me know where i am doing the mistake.

   ExampleApp.java
package com.example.exampleapp;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater.Filter;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

import android.widget.Filterable;
import android.widget.Toast;

public class ExampleApp extends Activity implements OnItemClickListener{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        AutoCompleteTextView autoCompView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
        autoCompView.setAdapter(new PlacesAutoCompleteAdapter(this, R.layout.list_item));
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        String str = (String) arg0.getItemAtPosition(arg2);
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }


}
class PlacesAutoCompleteAdapter extends ArrayAdapter<String> implements Filterable {
    private ArrayList<String> resultList;

    public PlacesAutoCompleteAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }
    private static final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api/place";
    private static final String TYPE_AUTOCOMPLETE = "/autocomplete";
    private static final String OUT_JSON = "/json";

    private static final String API_KEY = "";
    @Override
    public int getCount() {
        return resultList.size();
    }

    @Override
    public String getItem(int index) {
        return resultList.get(index);
    }

    @Override
    public android.widget.Filter getFilter() {
        Filter filter = new Filter() {
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults filterResults = new FilterResults();
                if (constraint != null) {
                    // Retrieve the autocomplete results.
                    resultList = autocomplete(constraint.toString());

                    // Assign the data to the FilterResults
                 //   filterResults.values = resultList;
                  //  filterResults.count = resultList.size();
                }
                return filterResults;
            }

            private ArrayList<String> autocomplete(String input) {
                ArrayList<String> resultList = null;

                HttpURLConnection conn = null;
                StringBuilder jsonResults = new StringBuilder();
                try {
                    StringBuilder sb = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON);
                    sb.append("?sensor=false&key=" + API_KEY);
                    sb.append("&components=country:uk");
                    sb.append("&input=" + URLEncoder.encode(input, "utf8"));

                    URL url = new URL(sb.toString());
                    conn = (HttpURLConnection) url.openConnection();
                    InputStreamReader in = new InputStreamReader(conn.getInputStream());

                    // Load the results into a StringBuilder
                    int read;
                    char[] buff = new char[1024];
                    while ((read = in.read(buff)) != -1) {
                        jsonResults.append(buff, 0, read);
                    }
                } catch (MalformedURLException e) {
                   // Log.e(LOG_TAG, "Error processing Places API URL", e);
                    return resultList;
                } catch (IOException e) {
                    //Log.e(LOG_TAG, "Error connecting to Places API", e);
                    return resultList;
                } finally {
                    if (conn != null) {
                        conn.disconnect();
                    }
                }

                try {
                    // Create a JSON object hierarchy from the results
                    JSONObject jsonObj = new JSONObject(jsonResults.toString());
                    JSONArray predsJsonArray = jsonObj.getJSONArray("predictions");

                    // Extract the Place descriptions from the results
                    resultList = new ArrayList<String>(predsJsonArray.length());
                    for (int i = 0; i < predsJsonArray.length(); i++) {
                        resultList.add(predsJsonArray.getJSONObject(i).getString("description"));
                    }
                } catch (JSONException e) {
                   // Log.e(LOG_TAG, "Cannot process JSON results", e);
                }

                return resultList;
            }
            protected void publishResults(CharSequence constraint, FilterResults results) {
                if (results != null) {
                    notifyDataSetChanged();
                }
                else {
                    notifyDataSetInvalidated();
                }
            }

            @Override
            public boolean onLoadClass(Class arg0) {
                // TODO Auto-generated method stub
                return false;
            }};
        return (android.widget.Filter) filter;
    }
}

FilterResults.java

package com.example.exampleapp;

import java.util.ArrayList;

public class FilterResults {

    protected ArrayList<String> values;
    protected int count;

}


i write the above code but the application closed .please see once and let me know where i am doing mistake in the above code ?
Thanks in Advance..........

解决方案

Here is the Code Snippet Which i have Used when i have similar problem. i give you one suggestion that D'nt put your whole code in only single java file. Make the separate methods/class for each different task you are using in your code. it will help you easier to trace it.

here is my PlacesAutoCompleteAdapter.java File.

package com.inukshk.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;

import com.inukshk.CreateInukshk.CreateInukshk;

public class PlacesAutoCompleteAdapter extends ArrayAdapter<String> implements
        Filterable {
    private ArrayList<String> resultList;

    public PlacesAutoCompleteAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

    @Override
    public int getCount() {
        return resultList.size();
    }

    @Override
    public String getItem(int index) {
        return resultList.get(index);
    }

    @Override
    public Filter getFilter() {
        Filter filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults filterResults = new FilterResults();
                if (constraint != null) {
                    // Retrieve the autocomplete results.

                    resultList = CreateInukshk.autocomplete(constraint
                            .toString());

                    // Assign the data to the FilterResults
                    filterResults.values = resultList;
                    filterResults.count = resultList.size();
                }
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence constraint,
                    FilterResults results) {
                if (results != null && results.count > 0) {
                    notifyDataSetChanged();
                } else {
                    notifyDataSetInvalidated();
                }
            }
        };
        return filter;
    }
}

here is What you have to do inside OnCreate() of your activity.

autoCompView = (AutoCompleteTextView) findViewById(R.id.editloc);
        autoCompView.setAdapter(new PlacesAutoCompleteAdapter(this,
                R.layout.list_item));

here are the static Strings i have used in app.

private static final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api/place";
    private static final String TYPE_AUTOCOMPLETE = "/autocomplete";
    private static final String OUT_JSON = "/json";
    private static final String API_KEY = "YOUR API KEY";

and Finally here is the Method that you will use in Adapter.

public static ArrayList<String> autocomplete(String input) {

        ArrayList<String> resultList = null;

        HttpURLConnection conn = null;
        StringBuilder jsonResults = new StringBuilder();
        try {
            StringBuilder sb = new StringBuilder(PLACES_API_BASE
                    + TYPE_AUTOCOMPLETE + OUT_JSON);
            sb.append("?sensor=false&key=" + API_KEY);
            // sb.append("&components=country:uk");
            sb.append("&input=" + URLEncoder.encode(input, "utf8"));

            URL url = new URL(sb.toString());
            conn = (HttpURLConnection) url.openConnection();
            InputStreamReader in = new InputStreamReader(conn.getInputStream());

            // Load the results into a StringBuilder
            int read;
            char[] buff = new char[1024];
            while ((read = in.read(buff)) != -1) {
                jsonResults.append(buff, 0, read);
            }
        } catch (MalformedURLException e) {
            Log.e(TAG, "Error processing Places API URL", e);
            return resultList;
        } catch (IOException e) {
            Log.e(TAG, "Error connecting to Places API", e);
            return resultList;
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }

        try {
            // Create a JSON object hierarchy from the results
            JSONObject jsonObj = new JSONObject(jsonResults.toString());
            JSONArray predsJsonArray = jsonObj.getJSONArray("predictions");

            // Extract the Place descriptions from the results
            resultList = new ArrayList<String>(predsJsonArray.length());
            for (int i = 0; i < predsJsonArray.length(); i++) {
                resultList.add(predsJsonArray.getJSONObject(i).getString(
                        "description"));
            }

        } catch (JSONException e) {
            Log.e(TAG, "Cannot process JSON results", e);
        }

        return resultList;
    }

EDITED :

i guess you have specified the list_item.xml as below.

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" android:layout_height="wrap_content" />

try it out. Hope it will helps you.

UPDATE :

You have to manually attach the debugger while the emulator says "Waiting for debugger to attach". In Netbean's menu bar, click "Debug", then "Attach Debugger...". You have to select your package from the "Process" drop down list, and then click "Attach". That should do it.
This works in Netbeans 7 anyway.

or you can try out below thing also.

You have two choices: connect a debugger, or kill the process.

Looking at your logcat, you have at least two different desktop applications that are trying to connect to each application process, which is where the "Ignoring second debugger" messages are coming from.

This would happen if you were, say, running Eclipse with the ADT plugin, and the stand-alone DDMS at the same time. I don't know what you're running or what the netbeans plugin does, but I would start by figuring out if you have two different things fighting for control.

EDITED: HOW TO GET TEXT OF AutoCompleteTextView

just write this code snippet.

autoCompView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                YOURTEXT = autoCompView.getText().toString();
                //new AsyncGetAutoPlace().execute(autoCompView.getText()
                //      .toString().trim());
            }
        });