使用Retrofit的Android自动完成视图

3
我正在尝试理解如何使用“chips”实现搜索(因此应该有添加多个值的机会),并且建议列表应该来自使用Retrofit的Web服务。我想要实现的示例是在Pinterest应用中的搜索。
我在线阅读了许多帖子,其中有人使用AutoCompleteTextView和其他SearchView,老实说,我非常困惑应该采取哪种方法。
请指引我走向正确的方向。 谢谢。
1个回答

0

这里是使用 RetrofitAutocompleteEditText 的代码示例

public class MainActivity extends Activity {

    private interface GooglePlacesClient {

        @GET("/maps/api/place/autocomplete/json")
        Observable<PlacesResult> autocomplete(
            @Query("key") String key,
            @Query("input") String input);
    }

    private class PlacesResult {
        @Expose
        List<MainActivity.Prediction> predictions;
        @Expose
        String status;
    }

    private class Prediction {
        @Expose
        String description;
    }

    private static final String LOG_TAG = "RxRetrofitAutoComplete";
    private static final String GOOGLE_API_BASE_URL = "https://maps.googleapis.com";
    private static final String API_KEY = "XXX";
    private static final int DELAY = 500;

    GooglePlacesClient mGooglePlacesClient;

    @InjectView(R.id.editText1)
    EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);

        if (API_KEY.length()<10) {
            Toast.makeText(this, "API KEY is unset!", Toast.LENGTH_LONG).show();
            return;
        }

        if (mGooglePlacesClient == null) {
            mGooglePlacesClient = new RestAdapter.Builder()
                    .setConverter(new GsonConverter(new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create()))
                    .setEndpoint(GOOGLE_API_BASE_URL)
                    .setLogLevel(RestAdapter.LogLevel.FULL).build()
                    .create(GooglePlacesClient.class);
        }

        Observable<EditText> searchTextObservable = ViewObservable.text(editText);
        searchTextObservable.debounce(DELAY, TimeUnit.MILLISECONDS)
                .map(new Func1<EditText, String>() {
                    @Override
                    public String call(EditText editText) {
                        return editText.getText().toString();
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        Log.d(LOG_TAG, s);
                        try {
                            mGooglePlacesClient
                                    .autocomplete(API_KEY, URLEncoder.encode(s, "utf8"))
                                    .observeOn(AndroidSchedulers.mainThread())
                                    .subscribe(new Action1<PlacesResult>() {
                                        @Override
                                        public void call(PlacesResult placesResult) {
                                            List<String> strings = new ArrayList<String>();
                                            for (MainActivity.Prediction p : placesResult.predictions) {
                                                strings.add(p.description);
                                            }
                                            ListView listView = (ListView) findViewById(R.id.listView1);
                                            if (listView != null) {
                                                listView.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, strings));
                                            }
                                        }
                                    }, new Action1<Throwable>() {
                                        @Override
                                        public void call(Throwable throwable) {
                                            throwable.printStackTrace();
                                        }
                                    });
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        throwable.printStackTrace();
                    }
                });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onStop() {
        super.onStop();
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接