Android Open Source - android-grid-image-search Search Activity






From Project

Back to project page android-grid-image-search.

License

The source code is released under:

MIT License

If you think the Android project android-grid-image-search listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.gemmakbarlow.gridimagesearch;
/*www .j  av  a2  s.  c  o m*/
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;

import com.gemmakbarlow.gridimagesearch.model.ImageResult;
import com.gemmakbarlow.gridimagesearch.model.SearchQuery;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;

public class SearchActivity extends Activity {

  EditText etQuery;
  GridView gvResults;
  Button btnSearch;
  ArrayList<ImageResult> imageResults = new ArrayList<ImageResult>();
  ImageResultArrayAdapter imageAdapter;
  SearchQuery searchQuery;
  AsyncHttpClient client;
  
  private static int ADVANCED_FILTER_REQUEST_CODE = 100;

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        
        setupViews();
        setupAdapter();
        setupSearchQuery();
        
        client = new AsyncHttpClient();
    }


    @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;
    }
    
  
    @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      if(requestCode == ADVANCED_FILTER_REQUEST_CODE && resultCode == RESULT_OK) {
        searchQuery = (SearchQuery)data.getSerializableExtra("searchQuery");
      }
  }
    
    
    public void setupViews() {
      etQuery = (EditText)findViewById(R.id.etQuery);
      btnSearch = (Button)findViewById(R.id.btnSearch);
      setupGridView();
    }
    
    private void setupGridView() {
      gvResults = (GridView)findViewById(R.id.gvResults);
      gvResults.setOnScrollListener(new EndlessScrollListener() {
          @Override
          public void onLoadMore(int page, int totalItemsCount) {
                    // Whatever code is needed to append new items to your AdapterView
                    // probably sending out a network request and appending items to your adapter. 
                    // Use the page or the totalItemsCount to retrieve correct data.
            customLoadMoreFromActivity(page); 
          }
            });
    }
    
    public void setupAdapter() {
       imageAdapter = new ImageResultArrayAdapter(this, imageResults);
         gvResults.setAdapter(imageAdapter);
         gvResults.setOnItemClickListener(new OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> arg0, View parent, int position,
          long arg3) {
        Intent i = new Intent(getApplicationContext(), ImageDisplayActivity.class);
        ImageResult imageResult = imageResults.get(position);
        i.putExtra("imageResult", imageResult);
        startActivityForResult(i, ADVANCED_FILTER_REQUEST_CODE);
        }
           
         });
    }
    
    protected void customLoadMoreFromActivity(int page) {
      int startIndex = page * searchQuery.getResultsPerPage();
      searchQuery.setCurrentPageStartIndex(startIndex);
      executeQueryAndPopulateResults();  
    }


  protected void setupSearchQuery() {
      if(searchQuery == null) {
        searchQuery = new SearchQuery();  
      }
    }

    public void onImageSearch(View v) {
      client.cancelRequests(getApplicationContext(), true);
      
      imageAdapter.clear(); 
      setupSearchQuery();
      
      executeQueryAndPopulateResults();
    }
    
    private void executeQueryAndPopulateResults() {
      String query = etQuery.getText().toString().trim();
      searchQuery.setQueryText(query);

      client.get(searchQuery.getQueryUrl(), new JsonHttpResponseHandler() {
    
        public void onSuccess(JSONObject response) {
          JSONArray imageJsonResults = null;
          
          try {
            
            JSONObject imageJson = response.getJSONObject("responseData");
            
            if(imageJson!= null) {
              imageJsonResults = imageJson.getJSONArray("results");
                  imageAdapter.addAll(ImageResult.fromJSONArray(imageJsonResults));
            }
          }
          catch(JSONException e) {
            e.printStackTrace();
          }
          
        }
      });
    }

    public void showAdvancedFilter(MenuItem mi) {
      Intent i = new Intent(getApplicationContext(), AdvancedFilterActivity.class);
      i.putExtra("searchQuery", this.searchQuery);
      startActivityForResult(i, ADVANCED_FILTER_REQUEST_CODE);
    }
}




Java Source Code List

com.gemmakbarlow.gridimagesearch.AdvancedFilterActivity.java
com.gemmakbarlow.gridimagesearch.EndlessScrollListener.java
com.gemmakbarlow.gridimagesearch.ImageDisplayActivity.java
com.gemmakbarlow.gridimagesearch.ImageResultArrayAdapter.java
com.gemmakbarlow.gridimagesearch.SearchActivity.java
com.gemmakbarlow.gridimagesearch.model.ImageResult.java
com.gemmakbarlow.gridimagesearch.model.SearchQuery.java