我需要在我的 Android 应用程序中构建一个搜索功能,该功能依赖于服务器的 json 响应。用户将在位于操作栏中的搜索 View 中输入搜索查询。根据用户输入的内容,将向服务器发出查询,服务器返回的响应应显示为下拉建议。我该怎么办呢。根据我读过的文档,我需要实现一个内容提供程序。实现应用搜索的最巧妙方式是什么?
这是我的 SearchActivity.java
package com.accure.health;
import android.os.Bundle;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.view.Menu;
import android.view.MenuInflater;
import android.widget.SearchView;
import android.widget.TextView;
public class SearchActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
TextView tv = (TextView) findViewById(R.id.user_label);
tv.setText(" Welcome, " + getIntent().getExtras().getString("name"));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.search, menu);
//return true;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =(SearchManager)getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.patient_search)
.getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
}
这是我的SearchResultActivity.java
package com.accure.health;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;
public class SearchResultsActivity extends Activity {
TextView txtQuery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_results);
ActionBar actionBar = getActionBar();
// Enabling Back navigation on Action Bar icon
actionBar.setDisplayHomeAsUpEnabled(true);
txtQuery = (TextView) findViewById(R.id.txt_Query);
handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
/**
* Handling intent data
*/
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
/**
* Use this query to display search results like
* 1. Getting the data from SQLite and showing in listview
* 2. Making webrequest and displaying the data
* For now we just display the query only
*/
txtQuery.setText("Search Query: " + query);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.search_results, menu);
return true;
}
}
最佳答案
好的,简单直接:
实现观察者模式以从服务器请求数据。
//通知请求客户端有关检索到的数据的接口(interface)
public interface GetData{ public void onSuccess(Array[data]); public void onError(); }
保持从服务器获取数据的逻辑方便,就像当我给你输入 XXX 时,你的逻辑应该从服务器检索数据,一旦通过另一个工作线程从服务器检索数据,就会使用
通知回客户端>onSuccess
或onError
// possible API public void fetchData(String text, GetData listener){ //... bla bla bla, you fire up and async task here new DataTask(listener).execute(text); // bla bla bla } DataTask extends AsyncTask<String,Integer,Integer>{ public DataTask(GetData listener){ .. } ... onPostExecute(){ listener.success or error }
基本系统已准备就绪,现在了解 android 中的
AutoCompleteTextView
here它有一个与之关联的适配器,通过该适配器,建议会以下拉列表的形式呈现给用户。现在,当用户在文本框中键入内容时,您可以调用此逻辑,例如用户键入 XXX,一旦通过
onSuccess
检索数据,您就会启动用于获取数据的逻辑。准备一个适配器并将其设置为您的 AutoCompeteView。
完成实现here
关于java - 来自服务器的 Android 搜索建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25301729/