java - 如何通过 API 调用在 Android Studio 中实现 autocompletetextview?

标签 java android autocomplete autocompletetextview

我正在尝试使用 Android Studio 中的自动完成 TextView 为用户输入的每个字母提供建议。

每次输入一个字母,都会像这样进行一次API调用,

http://dev.markitondemand.com/MODApis/Api/v2/Lookup/json?input=app
http://dev.markitondemand.com/MODApis/Api/v2/Lookup/json?input=appl
http://dev.markitondemand.com/MODApis/Api/v2/Lookup/json?input=apple

从 API 调用返回的 JSON 数组填充在建议列表框中。

到目前为止,我得到了这样的activity_main.xml文件,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.raam.stockmarketviewer.MainActivity">

    <AutoCompleteTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/stocks"
        android:hint="@string/hint" />
</RelativeLayout>

在此之后,我应该如何构造 MainActivity.java 文件来实现自动建议功能?

最佳答案

只需创建一个简单的适配器并在每次获得结果时更新它

  List<String> suggestions = new ArrayList<>();
  ArrayAdapter<String> adapter ;
   .
   .
   .
   // in your onCreate

   autocomplete = (AutoCompleteTextView)findViewById(R.id.stocks);
   adapter = new ArrayAdapter<>(this,
            android.R.layout.simple_dropdown_item_1line, suggestions);
   autocomplete.setAdapter(arrayAdapter);

   autocomplete.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
          //retrieveData(s);
        }

        @Override
        public void afterTextChanged(Editable s) {
            retrieveData(s); //this will call your method every time the user stops typing, if you want to call it for each letter, call it in onTextChanged 

        }
    });
   .
   .
   .
   // where you get the data, I suppose in a list
   private void retrieveData(String s){
    //Do your stuff here with the String s and store the list of your results in the list suggestions
   suggestions = yourList;
   adapter.notifyDataSetChanged();

   }

关于java - 如何通过 API 调用在 Android Studio 中实现 autocompletetextview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36799746/

相关文章:

jquery - 根据返回的记录将字段呈现为 jQuery 自动完成或下拉列表

android - 如何在 google maps API for android 中为自动完成预测添加样式?

android - Quickblox FCM 通知不起作用

java - 使用 jSTL 格式化日期时间以显示时区偏移

java - Java Object Reference 发布不当

java - 分享 Instagram Story 的贴纸坏了?

c# - 尝试使用 GoogleApis 库连接到 Google 时获取 "Unexpected character ' <' at line 1, column 0."

android - LG Revolution 和 Android 开发

Python:自动完成可以用于列表中的元素吗?

java - com.coinbase.api.exception.UnauthorizedException