java - 来自服务器的 Android 搜索建议

标签 java android

我需要在我的 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;
}

}

最佳答案

好的,简单直接:

  1. 实现观察者模式以从服务器请求数据。

    //通知请求客户端有关检索到的数据的接口(interface)

     public interface GetData{
         public void onSuccess(Array[data]);
         public void onError();
      }
    
  2. 保持从服务器获取数据的逻辑方便,就像当我给你输入 XXX 时,你的逻辑应该从服务器检索数据,一旦通过另一个工作线程从服务器检索数据,就会使用通知回客户端>onSuccessonError

    // 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
      }
    
  3. 基本系统已准备就绪,现在了解 android 中的 AutoCompleteTextView here它有一个与之关联的适配器,通过该适配器,建议会以下拉列表的形式呈现给用户。

  4. 现在,当用户在文本框中键入内容时,您可以调用此逻辑,例如用户键入 XXX,一旦通过 onSuccess 检索数据,您就会启动用于获取数据的逻辑。准备一个适配器并将其设置为您的 AutoCompeteView。

完成实现here

关于java - 来自服务器的 Android 搜索建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25301729/

相关文章:

Android studio (Gradle) 不创建签名的 apk

android - 我应该在 SQLiteDatabase 和 SQLiteOpenHelper 上调用 .close()

java - 任务不可序列化 - Java 1.8 和 Spark 2.1.1

java - 如何将平面 map 转换为复杂的 JSONObject?

java - 为什么Hibernate 5.0.6 发布包中不包含事务实现jar?

java - 将本地数据与 Firebase 数据库合并并在 recyclerview 中显示

android - adb连接错误:An existing connection was forcibly closed by the remote host

java - 切换到 targetSdkVersion 28 (Android) 时出现 DefaultHttpClient NoClassDefFoundError

java - 如何更新 Spring WebFlow 模型中的数据并将其传递回同一 View

java - 如何获取 div 中子项的计数甚至文本列表