【发布时间】:2014-09-30 05:20:19
【问题描述】:
我正在制作一个安卓应用程序。自动完成存在问题。当我将自动完成功能与这样的变量连接时,自动完成功能正在工作
String[] languages = { "C","C++","Java","C#","PHP","JavaScript","jQuery","AJAX","JSON" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_singlechoice, languages);
//Find TextView control
AutoCompleteTextView acTextView = (AutoCompleteTextView) findViewById(R.id.languages);
//Set the number of characters the user must type before the drop down list is shown
acTextView.setThreshold(1);
//Set the adapter
acTextView.setAdapter(adapter);
但我也想将它连接到生成 JSON 的网页我试图将它连接到我的网页但它不起作用。
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://example.com/test.php");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("Pass 1", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 1", e.toString());
}
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(is,"iso-8859-8"),8);
StringBuilder sb=new StringBuilder();
while((line=br.readLine())!=null)
{
sb.append(line+"\n");
}
is.close();
result=sb.toString();
Log.e("Pass 2", "Success");
}
catch(Exception e)
{
Log.e("Fail 2", e.toString());
}
try
{
JSONArray ja=new JSONArray(result);
JSONObject jo=null;
String[] str=new String[ja.length()];
for(int i=0;i<ja.length();i++)
{
jo=ja.getJSONObject(i);
str[i]=jo.getString("name");
}
MultiAutoCompleteTextView auto=(MultiAutoCompleteTextView) findViewById(R.id.names);
auto.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
ArrayAdapter<String> adp= new ArrayAdapter<String>(getBaseContext(),
android.R.layout.simple_dropdown_item_1line,str);
adp.setDropDownViewResource(android.R.layout.simple_expandable_list_item_1);
auto.setThreshold(1);
auto.setAdapter(adp);
}
catch(Exception e)
{
Log.e("Fail 2", e.toString());
}
}
我只想将我的自动完成功能与我的网页连接起来,并在搜索时检索名称字符串。我还在我的 Android Manifest 中添加了 Internet 权限。
【问题讨论】:
-
您是否想说在您的
AutocompleteTextView中输入的任何值都应该在您的网络服务器上传递,您会得到相应的结果? -
是的,例如我搜索国家名称 Like USA 并在 json 文件中自动完成搜索 USA
-
这似乎是一项非常繁琐的工作,因为每次都需要解析整个 json 文件,这需要时间,所以我认为这不是一个好习惯。您可以解析json文件并将其保存在arraylist中,然后在autocompletetextview中使用它。
-
有没有办法像我在语言变量中那样在字符串变量中获取和保存 json 数据,如果你在代码中显示它会很有帮助
标签: android json autocomplete autocompletetextview