2

I need to build a search functionality in my android app which relies on json response from the server. The users will enter the search query in a searchview located in the actionbar. Based on what the user types a query will be made to the server and the server returned response should appear as drop down suggestion. How should I go about it . Based on the docs i have read I need to implement a content provider. What would be neatest way of implementing app search ?

this is my 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);
}
}

this is my 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 Answers1

0

Alright, simple and straight forward:

  1. Implement Observer pattern for requesting data from server.

    // Interface to notify back to requesting client, about retrieved data

     public interface GetData{
         public void onSuccess(Array[data]);
         public void onError();
      }
    
  2. keep your data fetch logic from server handy, like when i give you input XXX your logic should retrieve data for it from server, once data is retrieved from server through another worker thread notify back to client by using onSuccess or 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
      }
    
  3. Out basic system is ready, now read about AutoCompleteTextView in android here it has an adapter associated with it, through which suggestions appears as drop down to user.

  4. Now, you can invoke this logic when user types somthing in your text box, like if user typed XXX, you fire up logic for fetching data, once data is retrived through onSuccess you prepare an adater and set it your AutoCompeteView.

complete implementation here

Narendra Baratam
  • 828
  • 1
  • 12
  • 26
Techfist
  • 4,314
  • 6
  • 22
  • 32
  • will you tell me with example i am very new to android... i know the basic concept of android.. please give example it will be very helpfull... thanks for reply. – abdul khadar Aug 14 '14 at 07:08
  • here you go, full implementation read it here https://developers.google.com/places/training/autocomplete-android – Techfist Aug 14 '14 at 07:11
  • sir i have written a web service for getting the data from solr. just i need to call that web service using AJAX. same way i have to call for search suggestion web service also... – abdul khadar Aug 14 '14 at 07:21
  • sir even my problem is same http://stackoverflow.com/questions/21375449/android-implement-app-search-suggestion-based-on-server-response.. – abdul khadar Aug 14 '14 at 07:28
  • Which part you are not able to implement? – Techfist Aug 14 '14 at 09:05
  • Sir i am unable to start itself i have created two files one is SearchActivity and SearchResultActivity.java. where exactly i should start coding... – abdul khadar Aug 14 '14 at 10:25
  • code you have posted has no code for any thing like downloading data etc. you said you want to get data from server? do you have API of server you need to call to get required data? do you have any sample output json of that api? – Techfist Aug 14 '14 at 10:47
  • sir i dont have API i have to call just URL. API is written somewhere they have given just url.. – abdul khadar Aug 14 '14 at 10:52
  • Based on what the user types a query will be made to the server and the server returned response should appear as drop down suggestion. – abdul khadar Aug 14 '14 at 10:58
  • would you care to share the api details, as you are not sharing anything I requested like JSON output etc, I will look by myself. – Techfist Aug 14 '14 at 11:03