Android Open Source - bv-android-sdk Products Activity






From Project

Back to project page bv-android-sdk.

License

The source code is released under:

Apache License

If you think the Android project bv-android-sdk 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.bazaarvoice.example.browseproducts;
/*from ww w.j  a  v a  2  s . c  om*/
import java.util.ArrayList;

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

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

/**
 * ProductsActivity.java <br>
 * BrowseProductExample<br>
 * 
 * <p>
 * This is a product display screen. It loads the products according to the
 * search term submitted in the previous activity and utilizes the Statistics
 * API to display average rating.
 * 
 * <p>
 * Created on 7/3/12. Copyright (c) 2012 BazaarVoice. All rights reserved.
 * 
 * @author Bazaarvoice Engineering
 */
public class ProductsActivity extends Activity {
  protected final String TAG = "ProductsActivity";

  private ListView productList;
  private ProductAdapter listAdapter;
  private ArrayList<BazaarProduct> products;
  private ProgressDialog progDialog;
  private TextView noResult;

  /**
   * Queries for products if this is a new instance or retrieves the product
   * list if this is a recycled instance.
   */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.products);

    if (savedInstanceState == null) {
      products = new ArrayList<BazaarProduct>();
      initializeViews();
      runSearchQuery();
    } else {
      products = savedInstanceState.getParcelableArrayList("products");
      initializeViews();
    }
  }

  /**
   * Saves the list of products.
   */
  @Override
  public void onSaveInstanceState(Bundle bundle) {
    super.onSaveInstanceState(bundle);
    bundle.putParcelableArrayList("products", products);
  }

  /**
   * Sets up the views and sets an OnItemClickListener on the product list to
   * launch the next activity.
   */
  private void initializeViews() {
    productList = (ListView) findViewById(R.id.productList);
    listAdapter = new ProductAdapter(getBaseContext(), products);
    productList.setAdapter(listAdapter);

    productList.setOnItemClickListener(new OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> aView, View view,
          int position, long id) {
        BazaarProduct selectedProduct = products.get(position);
        Intent intent = new Intent(getBaseContext(),
            ReviewsActivity.class);
        intent.putExtra("product", selectedProduct);
        startActivity(intent);
      }

    });

    progDialog = new ProgressDialog(this);
    noResult = (TextView) findViewById(R.id.noResult);
  }

  /**
   * Sends off a request to retrieve the product list depending on the user's
   * search term. When the response comes, it makes a second request for
   * statistics for the products.
   */
  private void runSearchQuery() {
    Intent myIntent = getIntent();
    String searchTerm = myIntent.getStringExtra("searchTerm");
    BazaarFunctions.runProductSearchQuery(searchTerm,
        new BazaarUIThreadResponse(this) {

          @Override
          public void onUiResponse(JSONObject json) {
            Log.i(TAG, "Response = \n" + json);
            try {
              JSONArray results = json.getJSONArray("Results");

              if (results.length() == 0) {
                noResult.setVisibility(View.VISIBLE);
                progDialog.dismiss();
              }

              for (int i = 0; i < results.length(); i++) {
                BazaarProduct newProd = new BazaarProduct(
                    results.getJSONObject(i));

                products.add(newProd);
                listAdapter.notifyDataSetChanged();

                // dismiss dialog on last iteration to
                // save another call to
                // runOnUiThread()
                if (products.size() == results.length())
                  progDialog.dismiss();

              }
            } catch (JSONException exception) {
              Log.e(TAG, "Error = " + exception.getMessage()
                  + "\n" + Log.getStackTraceString(exception));
            }

          }

        });
    progDialog.setMessage("Loading products...");
    progDialog.show();
  }

}




Java Source Code List

com.bazaarvoice.BazaarException.java
com.bazaarvoice.BazaarParams.java
com.bazaarvoice.BazaarRequest.java
com.bazaarvoice.DisplayParams.java
com.bazaarvoice.Media.java
com.bazaarvoice.OnBazaarResponse.java
com.bazaarvoice.SubmissionMediaParams.java
com.bazaarvoice.SubmissionParams.java
com.bazaarvoice.example.browseproducts.BazaarFunctions.java
com.bazaarvoice.example.browseproducts.BazaarProduct.java
com.bazaarvoice.example.browseproducts.BazaarReview.java
com.bazaarvoice.example.browseproducts.BazaarUIThreadResponse.java
com.bazaarvoice.example.browseproducts.ImageDownloader.java
com.bazaarvoice.example.browseproducts.MainActivity.java
com.bazaarvoice.example.browseproducts.OnImageDownloadComplete.java
com.bazaarvoice.example.browseproducts.ProductAdapter.java
com.bazaarvoice.example.browseproducts.ProductsActivity.java
com.bazaarvoice.example.browseproducts.ReviewAdapter.java
com.bazaarvoice.example.browseproducts.ReviewDisplayActivity.java
com.bazaarvoice.example.browseproducts.ReviewsActivity.java
com.bazaarvoice.example.reviewsubmission.BazaarFunctions.java
com.bazaarvoice.example.reviewsubmission.BazaarReview.java
com.bazaarvoice.example.reviewsubmission.CameraUtils.java
com.bazaarvoice.example.reviewsubmission.ImageDownloader.java
com.bazaarvoice.example.reviewsubmission.MainActivity.java
com.bazaarvoice.example.reviewsubmission.OnImageDownloadComplete.java
com.bazaarvoice.example.reviewsubmission.OnImageUploadComplete.java
com.bazaarvoice.example.reviewsubmission.RatingActivity.java
com.bazaarvoice.example.reviewsubmission.RatingPreviewActivity.java
com.bazaarvoice.example.reviewsubmission.ReviewSubmissionApp.java
com.bazaarvoice.intentexample.BazaarFunctions.java
com.bazaarvoice.intentexample.BazaarUIThreadResponse.java
com.bazaarvoice.intentexample.CameraUtils.java
com.bazaarvoice.intentexample.MainActivity.java
com.bazaarvoice.types.Action.java
com.bazaarvoice.types.ApiVersion.java
com.bazaarvoice.types.Equality.java
com.bazaarvoice.types.FeedbackContentType.java
com.bazaarvoice.types.FeedbackType.java
com.bazaarvoice.types.FeedbackVoteType.java
com.bazaarvoice.types.IncludeStatsType.java
com.bazaarvoice.types.IncludeType.java
com.bazaarvoice.types.MediaParamsContentType.java
com.bazaarvoice.types.RequestType.java
com.chute.android.photopickerplus.adapter.AlbumsAdapter.java
com.chute.android.photopickerplus.adapter.PhotoSelectCursorAdapter.java
com.chute.android.photopickerplus.adapter.PhotosAdapter.java
com.chute.android.photopickerplus.app.AlbumsActivity.java
com.chute.android.photopickerplus.app.ChooseServiceActivity.java
com.chute.android.photopickerplus.app.GridActivity.java
com.chute.android.photopickerplus.app.PhotoPickerPlusApp.java
com.chute.android.photopickerplus.dao.MediaDAO.java
com.chute.android.photopickerplus.util.AppUtil.java
com.chute.android.photopickerplus.util.Constants.java
com.chute.android.photopickerplus.util.NotificationUtil.java
com.chute.android.photopickerplus.util.intent.AlbumsActivityIntentWrapper.java
com.chute.android.photopickerplus.util.intent.IntentUtil.java
com.chute.android.photopickerplus.util.intent.IntentWrapper.java
com.chute.android.photopickerplus.util.intent.PhotoActivityIntentWrapper.java
com.chute.android.photopickerplus.util.intent.PhotoPickerPlusIntentWrapper.java
com.chute.android.photopickerplus.util.intent.PhotosIntentWrapper.java
com.example.productwidgetexample.BazaarFunctions.java
com.example.productwidgetexample.BazaarProduct.java
com.example.productwidgetexample.BazaarReview.java
com.example.productwidgetexample.BazaarUIThreadResponse.java
com.example.productwidgetexample.ImageDownloader.java
com.example.productwidgetexample.OnImageDownloadComplete.java
com.example.productwidgetexample.ProductWidgetProvider.java
com.example.productwidgetexample.ReviewAdapter.java
com.example.productwidgetexample.ReviewsActivity.java