- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是 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/
当用户点击 ACTV 并找到他正在搜索的内容时,他再次选择 ACTV 的唯一原因是搜索其他内容。我正在寻找一种方法,当用户在 ACTV 中进行搜索并且搜索文本位于 ACTV 字段中(建议菜单已关闭)时
当我单击 AutoCompleteTextView 的建议时,我希望光标移动到下一个可用的 EditBox 或 AutoCompleteTextView。 我在下面附上了我的布局和我的应用图像文件。在
我有一个 AppCompatAutoCompleteTextView,其 anchor 比 AppCompatAutoCompleteTextView 更靠下。这是设计使然,无法更改。当我在下拉菜单上
我正在使用 3 个 AutocompleteTextViews 来建议来自数据库的条目。我对 AutocompleteTextView 进行了子类化处理,以便在单击时将默认文本设置为 null,并在移
我正在使用 AutoCompleteTextView 创建下拉菜单。如何删除列表顶部和底部的默认边距? 重新创建: List dropdownItems
我正在尝试让 TextWatcher 与 AutoCompleteTextView 一起工作,但遇到了一些问题。我面临的问题是,当我开始输入文本时,如果数据输入的长度只有一个字符,我在 AutoCom
即使我没有在下拉列表项布局中设置边距,下拉列表也带有左右边距。我希望它与 autocompletetextview 的宽度相匹配。我怎样才能做到这一点? 这是我的 AutoCompleteTextVi
我的应用实现了一个 HashMap,这样当在 AutoCompleteTextView editText 中键入 key 时,并且用户单击下拉项,该值将显示在textView上。 唯一的问题是,单击的
这里是 Android 菜鸟,之前是 iOS、.Net、C 和 Fortran。 如果我有一个带有 1000 多个字符串的 ArrayAdapter,我如何修改 AutoCompleteTextVie
嗨,我刚刚开始学习 android Studio,我想要一个 AutoCompletetextView 和一个按钮,如果用户输入未在数组列表中列出,则添加用户输入。我的 AutoCompleteTex
我正在开发一个使用 AutoCompleteTextView 的应用程序,但遇到了一些问题。请在下面查找问题的详细信息。 数据中存在以下值: 1)曼尼什·洛根·杰恩 2)M.J.(洛根·费恩) 3)洛
我正在尝试根据另一个 View (微调器)中选择的选项来更改 AutoCompleteTextView 的建议列表。不幸的是,这个 TextView 的适配器似乎没有从另一个 View 的 setOn
我有这个布局: 使用这种风格: @color/text_input_layout_outlined_box_stroke @color/green_2 我试图在自动
我正在尝试使用 AutoCompleteTextView 进行过滤,但过滤器出现问题。它返回 ArrayList 中的所有项目,而不是过滤后的项目。下面是我的代码: Filter nameFilte
我想制作一个 AutoCompleteTextview ,它将从内部存储加载以前保存的建议。我成功地将字符串从内部存储加载到字符串数组(我使用日志记录来检查......)。 然后,当我将字符串数组加载
我有一个 AutoCompleteTextView,我想在它上面设置一个过滤器,我遇到的问题是当我去输入一些东西时它只会让我在文本字段中输入 1 个字符。如果我从代码中删除 textView.setF
我有两个自动完成 TextView 例如,如果任何人选择自动完成 TextView “HUB ID”位置#3,则另一个自动完成 TextView “HUBName”必须强制位于位置 3即希望它们都处于
您好 Android 开发人员, 我有一个关于自动完成 TextView 的问题。我在平板电脑上开发,我很好地定制了它。当我在平板电脑上运行它时,它看起来像这样: 这实际上是我想要的 - 所有可见的东
我正在处理一项要求,其中使用自动完成 TextView 来获取匹配的Localities。用户必须输入几个字符,然后从服务器返回与这些字符匹配的 Localities 列表。自动完成 TextView
使用 Bootstrap 的 Web 应用程序,有这样的输入区域。 在 Android 中有没有像这样实现 EditText 的库? 最佳答案 是的,完成的泡泡在Android中被称为“筹码”。工作解
我是一名优秀的程序员,十分优秀!