gpt4 book ai didi

java - AutoCompleteTextView 和异步类

转载 作者:太空宇宙 更新时间:2023-11-03 10:26:05 24 4
gpt4 key购买 nike

我是 Android 领域的菜鸟,我基于 2.1 Google API 构建了一个小型培训软件。

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

从那以后,我用异步类修复了它,以便我的网络访问适合 4.0 Google API。

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

它是关于 ville(法语中的“城镇”)字段上的 AutoCompleteTextView


之前 (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) { }
}

100% 完美运行:

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

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


在 (4.0) 之后

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));
}
}
}

运行有问题:

-> 你必须输入 (enoughToFilter + 1) caractères 来显示城镇列表:BAD

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


事实上 enoughToFilter 运行良好,它启动了 RemplirVille 类,该类运行正常,只是它不显示列表!

但是,如果你再输入 1 个字符:-> enoughToFilter 仍然运行良好-> RemplirVille 再次带来数据....但这次选择列表显示良好。

对那个话题有什么想法吗?我想这是一个上下文问题,但即使使用 GetApplicationCOntext 我也无法得到它。

谢谢。

最佳答案

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

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();
}
//...
}

如果没有这个,下拉菜单直到输入下一个字符才会显示,这会产生 (enoughToFilter + 1) 问题。

关于java - AutoCompleteTextView 和异步类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12351962/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com