android - 如何将 MultiAutoCompleteTextView 中的结果设置为来自 Web 源而不是静态或数据库结果?

标签 android android-widget autocompletetextview

我有一个带有 MultiAutoCompleteTextView 的应用程序,我需要将结果设置为来自 Web 源 JSON/XML 或任何格式,我该如何执行它,或者更清楚地说明我在创建自己的应用程序时需要更改什么自动完成的自定义适配器?

最佳答案

好的,我在这里找到了一种方法:

这是我的布局:

activity_main.xml

<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" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="135dp"
        android:text="Button" />

    <MultiAutoCompleteTextView
        android:id="@+id/multiAutoCompleteTextView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="5dp"
        android:completionThreshold="1"
        android:ems="10" >

        <requestFocus />
    </MultiAutoCompleteTextView>

</RelativeLayout>

这是主要 Activity ,它是从 linkedin JSON 获取技能的示例

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.MultiAutoCompleteTextView;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ArrayAdapter;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

public class MainActivity extends Activity {


    public MultiAutoCompleteTextView MultiAutocomplete1;
    public String data;
    public List<String> suggest;
    public ArrayAdapter<String> aAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        suggest = new ArrayList<String>();
        // multiAutoCompleteTextView1
        MultiAutocomplete1 = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1);


        // MultiAutocomplete1.setAdapter(adapter);
        MultiAutocomplete1
                .setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

        MultiAutocomplete1.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable editable) {
                // TODO Auto-generated method stub

            }

            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                String newText = s.toString();
                String[] parts = newText.split(",");
                if(parts[parts.length -1].length() > 1){                    
                    new getJson().execute( parts[parts.length -1] );
                }
            }

        });

        // button1
        final Button btn1 = (Button) findViewById(R.id.button1);
        btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(
                        MainActivity.this,
                        String.valueOf("Your Input : "
                                + MultiAutocomplete1.getText().toString()),
                        Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    class getJson extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... key) {
            String newText = key[0];
            newText = newText.trim();
            newText = newText.replace(" ", "+");
            try {
                HttpClient hClient = new DefaultHttpClient();
                HttpGet hGet = new HttpGet("http://www.linkedin.com/ta/skill?query="+newText);

                ResponseHandler<String> rHandler = new BasicResponseHandler();
                data = hClient.execute(hGet, rHandler);
                suggest = new ArrayList<String>();
                JSONObject jobj = new JSONObject(data);
                JSONArray jArray = jobj.getJSONArray("resultList");
                for (int i = 0; i < jArray.length(); i++) {
                    String SuggestKey = jArray.getJSONObject(i).getString("displayName");
                    suggest.add(SuggestKey);
                }

            } catch (Exception e) {
                Log.w("Error", e.getMessage());
            }
            runOnUiThread(new Runnable() {
                public void run() {
                    aAdapter = new ArrayAdapter<String>(
                            getApplicationContext(),
                            android.R.layout.simple_dropdown_item_1line,
                            suggest);
                    MultiAutocomplete1.setAdapter(aAdapter);
                    aAdapter.notifyDataSetChanged();
                }
            });

            return null;
        }

    }

}

关于android - 如何将 MultiAutoCompleteTextView 中的结果设置为来自 Web 源而不是静态或数据库结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13191804/

相关文章:

android - 如何解决 java.lang.IndexOutOfBoundsException : Invalid index 0, 大小为 0?

Android AutoCompleteTextView 建议替代方案

python - 如何在 Android 应用程序中集成 python 库?

android - 如何将 gradle 配置为仅将 logback-classic 用于 Android 中的单元测试?

android - 是否可以在 AlertDialog 中自定义正负按钮?

android - 如何在 View 之间的主窗口小部件中创建简单的分隔符/分隔符

android - JSON数组在android中自动完成 TextView

php - 将 Android 应用程序与 php 和 mysql 连接

android - 无法从设备访问服务器,在模拟器中工作

android - LayoutInflater.inflate 和 findViewById 的区别