Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.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
Java 自动完成文本视图&;异步类_Java_Android_Autocompletetextview - Fatal编程技术网

Java 自动完成文本视图&;异步类

Java 自动完成文本视图&;异步类,java,android,autocompletetextview,Java,Android,Autocompletetextview,我是Android世界的新手,我建立了一个基于2.1谷歌API的小型培训软件 当时我还不知道主线程和工作线程,所以我把所有代码都放在主线程中 从那时起,我用异步类为我的netwkork访问修复了它,以适应4.0 Google API 好吧,但最后一件事困扰着我,我就是找不到任何线索 它是关于一个field ville(法语中的“城镇”)上的自动完成文本视图 之前(2.1): public void onTextChanged(CharSequence s, int start, int bef

我是Android世界的新手,我建立了一个基于2.1谷歌API的小型培训软件

当时我还不知道主线程和工作线程,所以我把所有代码都放在主线程中

从那时起,我用异步类为我的netwkork访问修复了它,以适应4.0 Google API

好吧,但最后一件事困扰着我,我就是找不到任何线索

它是关于一个field ville(法语中的“城镇”)上的自动完成文本视图


之前(2.1):

public void onTextChanged(CharSequence s, int start, int before, int count) 
{
String result = null;
InputStream is = null;
List<String> r = new ArrayList<String>();

    if (ville.enoughToFilter())
    {
        is = connexionHttp(BASE_URL + "ville.php?ville=" + ville.getText());
        result = lectureData(is);

        try
        {           
            JSONArray jArray = new JSONArray(result);
            JSONObject json_data=null;
            for(int i=0;i<jArray.length();i++)
            {
                   json_data = jArray.getJSONObject(i);  
                   r.add(json_data.getString("VILLE"));
                   a_idVil.add(json_data.getString("CLEF_VILLE"));      
            }
            ville.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_selectable_list_item,r));
            ville.setOnItemSelectedListener(new villeListener());

        }
        catch(JSONException e1)
        {
                Toast.makeText(getBaseContext(),e1.toString() ,Toast.LENGTH_LONG).show();
                Log.d("***** TestActivity/onTextChanged: JSONException *****", "--"+e1.toString()+"--");
        } 
        catch(ParseException e1) 
        {
                Toast.makeText(getBaseContext(),e1.toString() ,Toast.LENGTH_LONG).show();
                Log.d("***** TestActivity/onTextChanged: ParseException *****", "--"+e1.toString()+"--");
        }
    }
}
public class villeListener implements OnItemSelectedListener 
{
    public void onItemSelected(AdapterView<?> parent, View v, int pos, long row) 
    {
        villePosition = pos;    
    }
    public void onNothingSelected(AdapterView<?> arg0)  {   }
}

public void onTextChanged(字符序列、int start、int before、int count)
{
字符串结果=null;
InputStream=null;
列表r=新的ArrayList();
if(ville.enoughToFilter())
{
is=connexionHttp(BASE_URL+“ville.php?ville=“+ville.getText());
结果=数据(is);
尝试
{           
JSONArray jArray=新JSONArray(结果);
JSONObject json_data=null;
对于(int i=0;i arg0){}
}

运行100%完美:

->在第4个字符之后,查询在MySql上运行,以查找以4个给定字母开头的所有城镇,并显示选择列表以选择正确的城镇:OK

->听者给出所选城镇的索引:OK


在(4.0)之后

public void onTextChanged(字符序列、int start、int before、int count)
{           
if(ville.enoughToFilter())
{           
新建RemplirVille().execute(BASE_URL+“ville.php?ville=“+ville.getText());
Log.d(“**********”,“先锋”);
ville.SetonimselectedListener(新villeListener());
Log.d(“**********”,“APRES”);
}
}
公共类villeListener实现了OnItemSelectedListener
{
已选择公共位置(AdapterView父视图、视图v、内部位置、长行)
{
绒毛位置=位置;
Log.d(“*************999999”、“1111111111”);
}
未选择的公共无效(AdapterView arg0){}
}
类RemplirVille扩展了异步任务
{
整数errorMsgId;
字符串错误MsgParam;
受保护列表doInBackground(字符串…参数)
{
List listaffichageville=new ArrayList();
ArrayList nameValuePairs=新的ArrayList();
尝试
{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(参数[0]);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
if(response.getStatusLine().getStatusCode()<400)
{
HttpEntity=response.getEntity();
字符串entityStr=EntityUtils.toString(实体);
JSONArray json_数组=新的JSONArray(entityStr);
对于(int i=0;i)你必须输入(enoughToFilter+1)caractères来显示城镇列表:糟糕

->监听器甚至不再运行:糟糕


事实上,一个足够的过滤器工作得很好,它启动了运行正常的RemplirVille类,只是它不显示列表

但是,如果再输入1个字符: ->足够的过滤器仍能正常工作 ->Remprirville再次带来数据……但这次选择列表显示得很好

你知道那个话题吗? 我想这是一个上下文问题,但即使使用GetApplicationCOntext,我也无法获得它


谢谢。

调用
AutoCompleteTextView.setAdapter()
不会自动显示下拉列表,但您可以使用
AutoCompleteTextView.showDropDown()
强制显示下拉列表

受保护的void onPostExecute(列表){
//...
setAdapter(新的ArrayAdapter(TestActivity.this,android.R.layout.simple_可选_列表_项,列表));
if(ville.isInputMethodTarget()){
ville.showDropDown();
}
//...
}

如果没有这个选项,下拉列表直到输入下一个字符才显示,这就产生了(enoughToFilter+1)问题。

我正在做类似的事情!!!
public void onTextChanged(CharSequence s, int start, int before, int count) 
{           
if (ville.enoughToFilter())
{           
    new RemplirVille().execute(BASE_URL + "ville.php?ville=" + ville.getText());
    Log.d("***********","AVANT");  
    ville.setOnItemSelectedListener(new villeListener());
    Log.d("***********","APRES");           
}

}
public class villeListener implements OnItemSelectedListener 
{
public void onItemSelected(AdapterView<?> parent, View v, int pos, long row) 
{
    villePosition = pos;
    Log.d("*************9999999", "1111111111");
}
public void onNothingSelected(AdapterView<?> arg0)  {   }
}

class RemplirVille extends AsyncTask<String, String, List<String>>
{
Integer errorMsgId;
String  errorMsgParam;

protected List<String> doInBackground(String... param)
{
    List<String> listeAffichageVille = new ArrayList<String>();
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

    try 
    {
             HttpClient httpclient = new DefaultHttpClient();      
             HttpPost httppost = new HttpPost(param[0]);
             httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
             HttpResponse response = httpclient.execute(httppost);     
             if (response.getStatusLine().getStatusCode() < 400) 
             {
                  HttpEntity entity = response.getEntity();
                  String entityStr = EntityUtils.toString(entity);
                  JSONArray json_array = new JSONArray(entityStr);

                  for(int i=0;i<json_array.length();i++) 
                  {
                      JSONObject json_ligne = json_array.getJSONObject(i);                            
                      listeAffichageVille.add(json_ligne.getString("VILLE"));
                      a_idVil.add(json_ligne.getString("CLEF_VILLE"));
                  }
            } 
            else 
            {
                Log.d("***** TestActivity/ASYNC RemplirVille: EXCEPTION http error *****", "--"+response.getStatusLine().toString()+"--"); 
                this.errorMsgId = R.string.http_site_error;
                listeAffichageVille = null;
            }
    } 
    catch (Exception ex) 
    {
            Log.d("***** TestActivity/ASYNC RemplirVille: EXCEPTION decode error *****", "--"+ex.toString()+"--");
            this.errorMsgId = R.string.http_decode_error;
            this.errorMsgParam = ex.getLocalizedMessage();
            listeAffichageVille = null;
    }
    return listeAffichageVille;
}

protected void onProgressUpdate(String... item) {   }
protected void onPreExecute(List<String> list)  {   }
protected void onPostExecute(List<String> list)     
{               
    if (list == null)
    {
          if (this.errorMsgId != null) 
          {
              String msg = TestActivity.this.getString(this.errorMsgId);
              Toast.makeText(TestActivity.this,msg,Toast.LENGTH_LONG).show();
          }    
    }
    else            
    {
        ville.setAdapter(new ArrayAdapter<String>(TestActivity.this,android.R.layout.simple_selectable_list_item,list));
    }
}   
}
protected void onPostExecute(List<String> list){
    //...
    ville.setAdapter(new ArrayAdapter<String>(TestActivity.this,android.R.layout.simple_selectable_list_item,list));
    if(ville.isInputMethodTarget()){
        ville.showDropDown();
    }
    //...
}