自动完成的机器人不工作与动态数据人不、自动完成、机器、动态

2023-09-07 16:15:10 作者:男人╮我戒了

我现在面临的问题,自动完成的机器人。相反,在活动本身硬编码的数据,我试图从其他应用程序动态读取它相匹配的每个键preSS的数据。请在下面找到我的程序,并建议我在哪里,我错了。

请注意:在第一次按键preSS,结果得到填充。如果我清除输入的文本,并输入不同的字符后,结果都没有得到显示在自动完成下拉框。我从其他应用程序上的每一个按键preSS获得,但没有得到填充自动完成下拉框。

我试着用textView.showDropDown();和adapter.setNotifyOnChange(真正的);选项​​。但是没有用。

 包com.util;

进口java.io.BufferedReader中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.InputStreamReader中;
进口的java.util.ArrayList;
进口的java.util.List;

进口org.apache.http.HttpEntity;
进口org.apache.http.Htt presponse;
进口org.apache.http.StatusLine;
进口org.apache.http.client.ClientProtocolException;
进口org.apache.http.client.HttpClient;
进口org.apache.http.client.methods.HttpGet;
进口org.apache.http.impl.client.DefaultHttpClient;
进口org.json.JSONArray;
进口org.json.JSONObject;

进口android.app.Activity;
进口android.os.Bundle;
进口android.text.Editable;
进口android.text.TextWatcher;
进口android.util.Log;
进口android.widget.ArrayAdapter;
进口android.widget.AutoCompleteTextView;

公共类HelloAutoCompleteActivity延伸活动{
名单的国家=新的ArrayList();
    字符串URL =; //一些应用程序URL
AutoCompleteTextView TextView的;
ArrayAdapter<字符串>适配器;

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    TextView的=(AutoCompleteTextView)findViewById(R.id.autocomplete_country);
    textView.addTextChangedListener(新CostomTextWatcher());
    textView.setThreshold(1);
    适配器=新的ArrayAdapter<字符串>(这一点,R.layout.list_item,国家);
    textView.setAdapter(适配器);
}

私有类CostomTextWatcher实现TextWatcher {

    @覆盖
    公共无效afterTextChanged(编辑S){
    }

    @覆盖
    公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
            之后INT){
    }

    @覆盖
    公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,
            诠释计数){
        如果(s.length()大于0){
            countries.clear();
            串readTwitterFeed = readFeed(s.toString());
            的System.out.println(readTwitterFeed);
            尝试 {
                JSONObject的menuObject =新的JSONObject(readTwitterFeed);
                JSONArray menuitemArray = menuObject
                        .getJSONArray(objectNameList);
                的for(int i = 0; I< menuitemArray.length();我++){
                    的System.out.println(menu​​itemArray.get(一));
                    countries.add(menu​​itemArray.get(ⅰ)的ToString());
                }
                // adapter.notifyDataSetChanged();
                // adapter.setNotifyOnChange(真正的);
                // textView.setAdapter(适配器);
                // textView.setThreshold(1);
                // textView.setAdapter(适配器);
                // textView.showDropDown();
                // adapter.setNotifyOnChange(真正的);
            }赶上(例外五){
                e.printStackTrace();
            }
        }
    }
}

公共字符串readFeed(字符串VAL){

    StringBuilder的建设者=新的StringBuilder();
    HttpClient的客户端=新DefaultHttpClient();
    HTTPGET HTTPGET =新HTTPGET(
            URL);
    尝试 {
        HTT presponse响应= client.execute(HTTPGET);
        状态行状态行= response.getStatusLine();
        INT状态code = statusLine.getStatus code();
        如果(状态code == 200){
            的System.out.println(200);
            HttpEntity实体= response.getEntity();
            InputStream的内容= entity.getContent();
            的BufferedReader读卡器=新的BufferedReader(
                    新InputStreamReader的(内容));
            串线;
            而((行= reader.readLine())!= NULL){
                builder.append(线);
            }
        } 其他 {
            的System.out.println(else块);
            Log.e(HelloAutoCompleteActivity.class.toString(),
                    无法下载文件);
        }
    }赶上(ClientProtocolException E){
        e.printStackTrace();
    }赶上(IOException异常E){
        e.printStackTrace();
    }
    返回builder.toString();
}
 

}

解决方案 为何双足机器人不直接拷贝人类行动时的数据

您可以使用Filter接口来实现这一点。原来Filter.performFiltering()被调用关闭UI线程只是为这种类型的目的。下面是一些code我用它来做到这一点:

 过滤器过滤器=新的过滤器(){

    @覆盖
    公众的CharSequence convertResultToString(对象resultValue){
        返回resultValue.toString();
    }

    @覆盖
    保护FilterResults performFiltering(CharSequence中的CharSequence){
        如果(CharSequence的== NULL)返回NULL;
        尝试 {
            //此调用打服务器,我正在寻找的名称和解析JSON返回的第25结果
            PagedResult结果= searchByName(charSequence.toString(),1,25,真);
            FilterResults filterResults =新FilterResults();
            filterResults.values​​ = results.getResults();
            filterResults.count = results.getResults()的大小()。
            返回filterResults;
        }赶上(JSONException E){
            返回新FilterResults();
        }
    }

    @覆盖
    保护无效publishResults(CharSequence中的CharSequence,FilterResults filterResults){
        如果(filterResults!= NULL){
            adapter.clear();
            adapter.addAll((名单<为MyObject>)filterResults.values​​);
        }
    }
};
 

然后使用过滤器:

 私人AutoCompleteTextView beverageName;
    ...

    beverageName = findViewById(R.id.beverageName);
    ListAdapter适配器= ...
    adapter.setFilter(过滤器);
    beverageName.setAdapter(适配器);
 

或U可以使用这个链接也

http://www.grobmeier.de/android-autocomplete-with-json-data-served-by-struts-2-05122011.html

I am facing problem with auto complete in android. Instead of hard coding data in Activity itself, I tried to read the data dynamically from other application on every key press which is matched. Please find below my program and suggest me where I am wrong.

Note: at the first time key press, the results are getting populated. After that if I clear the entered text and enter different character, the results are not getting shown in the auto complete drop down box. I am getting from other application on every key press but not getting populated in auto complete drop down box.

I tried with textView.showDropDown(); and adapter.setNotifyOnChange(true); options. But no use.

package com.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class HelloAutoCompleteActivity extends Activity {
List countries = new ArrayList();
    String url = ""; //some application url
AutoCompleteTextView textView;
ArrayAdapter<String> adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
    textView.addTextChangedListener(new CostomTextWatcher());
    textView.setThreshold(1);
    adapter = new ArrayAdapter<String>(this, R.layout.list_item, countries);
    textView.setAdapter(adapter);
}

private class CostomTextWatcher implements TextWatcher {

    @Override
    public void afterTextChanged(Editable s) {
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        if (s.length() > 0) {
            countries.clear();
            String readTwitterFeed = readFeed(s.toString());
            System.out.println(readTwitterFeed);
            try {
                JSONObject menuObject = new JSONObject(readTwitterFeed);
                JSONArray menuitemArray = menuObject
                        .getJSONArray("objectNameList");
                for (int i = 0; i < menuitemArray.length(); i++) {
                    System.out.println(menuitemArray.get(i));
                    countries.add(menuitemArray.get(i).toString());
                }
                // adapter.notifyDataSetChanged();
                // adapter.setNotifyOnChange(true);
                // textView.setAdapter(adapter);
                // textView.setThreshold(1);
                // textView.setAdapter(adapter);
                // textView.showDropDown();
                // adapter.setNotifyOnChange(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

public String readFeed(String val) {

    StringBuilder builder = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(
            url);
    try {
        HttpResponse response = client.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
            System.out.println("200");
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
        } else {
            System.out.println("else block");
            Log.e(HelloAutoCompleteActivity.class.toString(),
                    "Failed to download file");
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return builder.toString();
}

}

解决方案

You can use the Filter interface to implement this as well. Turns out Filter.performFiltering() is called off the UI thread just for this type of purpose. Here is some code I use to do this:

 Filter filter = new Filter() {

    @Override
    public CharSequence convertResultToString(Object resultValue) {
        return resultValue.toString();
    }

    @Override
    protected FilterResults performFiltering(CharSequence charSequence) {
        if( charSequence == null ) return null;
        try {
            // This call hits the server with the name I'm looking for and parses the JSON returned for the first 25 results
            PagedResult results = searchByName( charSequence.toString(), 1, 25, true);
            FilterResults filterResults = new FilterResults();
            filterResults.values = results.getResults();
            filterResults.count = results.getResults().size();
            return filterResults;
        } catch (JSONException e) {
            return new FilterResults();
        }
    }

    @Override
    protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
        if( filterResults != null ) {
            adapter.clear();
            adapter.addAll( (List<MyObject>)filterResults.values );
        }
    }
};

Then using the Filter:

    private AutoCompleteTextView beverageName;
    ...

    beverageName = findViewById( R.id.beverageName );
    ListAdapter adapter = ...
    adapter.setFilter(filter);
    beverageName.setAdapter(adapter);

or u can use this link also

http://www.grobmeier.de/android-autocomplete-with-json-data-served-by-struts-2-05122011.html