Skip to main content

First Android Application Tutorial and Example


http://semycolon.blogspot.com/search/label/E%20Commerce%20App%20on%20Android



..

First Android Application Tutorial and Example

Welcome!!

This is a series of blogs and videos where you learn how to create a fully functional Android Application!!!

At the end of this series of blogs and videos, you would have learnt

1. Activity
2. Service
3. How to use Google SpeechRecognizer
4. How to user Zbar for barcode
5. AsyncTask
6. How to fetch data from server using AsyncTask
7. ViewPager for swipe views
8. Implementation of Fragments
9. Tab views
10. ExpandableListView implementation
11. ListView implementation
12. Menu in ActionBar
13. How to push data to server

Click the below link to take you to the series of posts.

E-Commerce-App-on-Android

Hope you find it useful !!!


Source Code - E Commerce App


Please support me in keeping this blog and YouTube channel going.

Request you to please pay a nominal charge of $2 for getting the entire Project code. Once payment is done, I will share the link to the project within 1 day.

What you get: The E Commerce App source code consists of working examples of how to achieve the following. The list is growing, we will keep adding more stuff onto this list.

1. Activity
2. Service
3. How to use Google SpeechRecognizer
4. How to user Zbar for barcode
5. AsyncTask
6. How to fetch data from server using AsyncTask
7. ViewPager for swipe views
8. Implementation of Fragments
9. Tab views
10. ExpandableListView implementation
11. ListView implementation
12. Menu in ActionBar
13. How to push data to server

This also has the server side of code - CodeIgniter Controller and model. 

Also, one more commitment. Any further additions that I do to the project is free. You can access the updated project file anytime in the future. Keep following my YouTube channel (https://www.youtube.com/channel/UCp8DiYSAzJJxx6SMy16pzRQ) for more information.

Price : $ 2 only :)


Enter your email address
 

First Android App: Step 1: ADT installation

Welcome!!! I am new to android and I have taken it as a task to learn application development in Android within the next 10 days. So I thought, why not document the steps in detail so that I can help others like me who are interested in picking up the tricks of the trade.

I have taken it upon myself to create a shopping Cart application which is fully functional with all features. This blog will trace my development one step at a time.

How did I begin?

Step 1: My first step was to go through http://www.tutorialspoint.com/android/

This tutorial is very good with respect to familiarizing you with the different components of an Android application. Although this will not help you much when it comes to developing an actual application. 

Take your time and go through it at least once. This greatly helped me understand the purpose of different components and how they are to be used. Don't think much about the syntax etc, they will come as you start coding.

Step 2: Download ADT and install it in your system. To download ADT, browse to http://developer.android.com/tools/sdk/eclipse-adt.html and download the latest version. Make sure you select the correct Bit ( 32 Bit or 64 Bit) depending on your Operating System.

Step 3: Once the ADT has been downloaded, extract the zip file and in the extracted folder, browse to /eclipse/eclipse.exe and open Eclipse.

Step 4: Now navigate to Windows / Android SDK Manager and Install all the packages that are being suggested. It is important that you install all the packages depending on your minimum API compatibility that you require.

First Android App | Step 2 : first Hello World Application

Now that the ADT is installed, you can browse to the adt / eclipse folder and open eclipse.exe.

Once eclipse has opened, it is time to create a new project.

Go to File / New / Android Application Project. A pop up window will open up like below.



Please enter an application Name. In our case it is Basket.
Project Name is automatically populated.
Package Name is typically the reverse of your domain name followed by project name.

So if you domain name is www.zing.com, and your project name is basket, then the package name is typically kept as com.zing.basket. Remember, you can put more words to package name followed by . (dot) but convention is to keep it to 3 words only.

Next we need to select the Minimum Required SDK, Target SDK, Compile With and Theme.

For more detailed explanation of Min Required SDK, Target SDK and Compile with, please visit another detailed blog post of mine. Click here...

In our case, we are selecting Minimum Required SDK as API 14, Target SDK and compile with as API 18. The theme we selected is Holo Light with Dark Action Bar.

Next we need to select if we want to create a custom launcher icon and also create an activity. Leave these configurations as default and click on next.

On clicking Next, we have to select the icon for our application. If you have created an icon, you can browse and select it, alternatively you can select any icon for the time being and you can change it to another icon later on.

These icon file has the name ic_launcher and they are placed inside the res/drawable folders. These can be replaced later on if required.

Next, select blank activity and click next.

In our project, the first activity which will get called is named StartScreen.java. So change the name as StartScreen, the layout name will be activity_start_screen, let navigation type be none. On clicking next, your project will be created along with the following files

1. src / com.zing.basket / StartScreen.java

2. res/layout/activity_start_screen.xml

3. res/menu/start_screen.xml

4. res/values/string.xml

5. res/values/dimens.xml

6. res/values/styles.xml

7. res/drawable folders/ic_launcher.jpg

8. AndroidManifest.xml


Now you can rightclick on the Project Name 'Basket' on the left side menu and run it as Android Application. Make sure you have connected your Android phone to your computer using USB and also that you have activated Debugger Options in your Android Phone.

You can watch the entire tutorial through our Youtube Video as well.






First Android App | Step 3 | Getting data from Server using AsyncTask JSON

Welcome back!! We are well on our way to building our first android app - a complete E-Commerce Application.

I assume you would have gone through Step 1 and Step 2 becoming coming here. If you haven't, please go through them as well.

In this step, we are trying to create the Splash screen for our Application, create database and tables and also fetch data from Server in JSON format through AsyncTask.

Please watch our video to get a detailed understanding of how the code works.



The source code is attached.

The RESTful API which is being called is http://lawgo.in/lawgo/city?format=json


JSONParser.java

package com.zing.basket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONException;
import org.json.JSONObject;

public class JSONParser {
    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";
 
    // constructor
    public JSONParser() {
 
    }
    
    public JSONObject getJSONFromUrl(String url) throws Exception {
      
     // Making HTTP request
      try {
          
         HttpParams httpParameters = new BasicHttpParams();
         int timeoutConnection = 20000;
         HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
         // Set the default socket timeout (SO_TIMEOUT) 
         // in milliseconds which is the timeout for waiting for data.
         int timeoutSocket = 25000;
      // defaultHttpClient
         DefaultHttpClient httpClient = new DefaultHttpClient();
         httpClient.setParams(httpParameters);
         //HttpPost httpPost = new HttpPost(url);
        HttpGet httpPost = new HttpGet(url);
         HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

          HttpResponse httpResponse = httpClient.execute(httpPost);
          
          HttpEntity httpEntity = httpResponse.getEntity();
          is = httpEntity.getContent();          
       
          
          /*HttpClient httpclient = new DefaultHttpClient();
          HttpPost httppost = new HttpPost(url);
          HttpResponse response = httpclient.execute(httppost);
          HttpEntity entity = response.getEntity();
          is = entity.getContent();*/

      } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
      } catch (ClientProtocolException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
       
      try {
          BufferedReader reader = new BufferedReader(new InputStreamReader(
                  is, "iso-8859-1"), 8);
          StringBuilder sb = new StringBuilder();
          String line = null;
          while ((line = reader.readLine()) != null) {
              sb.append(line + "\n");
          }
          is.close();
          json = sb.toString();
          //Log.d("string","string is "+json);
      } catch (Exception e) {
          //Log.e("Buffer Error", "Error converting result " + e.toString());
      }

      // try parse the string to a JSON object
      try {
          jObj = new JSONObject(json);
      } catch (JSONException e) {
          //Log.e("JSON Parser", "Error parsing data " + e.toString());
      }

      // return JSON String
      return jObj;
  }
 
}


activity_start_screen.xml



 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#B22222"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".StartScreen">

    <TextView
        android:id="@+id/SelectCityText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="107dp"
        android:gravity="center"
        android:text="@string/app_name"
        android:textColor="#F8F8FF"
        android:textSize="40sp"/>

<RelativeLayout>


StartScreen.java


package com.zing.basket;

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

import android.app.ActionBar;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

public class StartScreen extends Activity {
 
    SQLiteDatabase sqLite;
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_start_screen);
  
 //initializing the action bar and hiding it. 
 ActionBar actionBar = getActionBar();
 actionBar.hide();
  
   
 //create a database if it doesnt exist, if it exists, it will open the database.
  
 sqLite=this.openOrCreateDatabase("basketbuddy", MODE_PRIVATE, null);
  
 sqLite.execSQL("CREATE TABLE IF NOT EXISTS USER (USER_ID VARCHAR,NAME VARCHAR,EMAIL VARCHAR)");
 sqLite.execSQL("CREATE TABLE IF NOT EXISTS CITY_LIST (ID INTEGER primary key autoincrement,CITY_NAME VARCHAR)" );
 sqLite.execSQL("CREATE TABLE IF NOT EXISTS USER_PREF (ID INTEGER primary key autoincrement,CITY_NAME VARCHAR default 'NONE', VOICE_ON VARCHAR, NOTIF VARCHAR)" );
 sqLite.execSQL("CREATE TABLE IF NOT EXISTS CHECKLIST (ID INTEGER primary key autoincrement, PRODUCT_NAME VARCHAR, PRODUCT_CHECKED VARCHAR)");
 sqLite.execSQL("CREATE TABLE IF NOT EXISTS PRODUCT_MASTER (ID INTEGER primary key autoincrement, PRODUCT_CODE INTEGER, PRODUCT_DIVISION INTEGER, PRODUCT_DEPARTMENT INTEGER"+ 
 "PRODUCT_GF INTEGER, PRODUCT_F INTEGER, PRODUCT_SUBF INTEGER, PRODUCT_NAME VARCHAR, PRODUCT_BARCODE INTEGER, PRODUCT_GRAMMAGE VARCHAR, PRODUCT_MRP INTEGER, PRODUCT_BBPRICE INTEGER)");
 sqLite.execSQL("CREATE TABLE IF NOT EXISTS CART (ID INTEGER primary key autoincrement, PRODUCT_CODE INTEGER, PRODUCT_DIVISION INTEGER, PRODUCT_DEPARTMENT INTEGER"+ 
 "PRODUCT_GF INTEGER, PRODUCT_F INTEGER, PRODUCT_SUBF INTEGER, PRODUCT_NAME VARCHAR, PRODUCT_BARCODE VARCHAR, PRODUCT_GRAMMAGE VARCHAR, PRODUCT_MRP INTEGER, PRODUCT_BBPRICE INTEGER, PRODUCT_QTY INTEGER, PRODUCT_VALUE INTEGER)");
  
 //call asynctask to fetch list of city to populate the dropdown. 
  
 updateList ul=new updateList();
 ul.execute();
  
    }
 
    class updateList extends AsyncTask<Void, Void, String>
    {
 JSONParser jParser;
 String url=new String();
  
 @Override
 protected void onPreExecute() {
    super.onPreExecute();
    url="http://lawgo.in/lawgo/city?format=json";
    jParser = new JSONParser();
 }
   
   
 @Override
 protected String doInBackground(Void... params) 
 {
     try
       {
  JSONObject json=jParser.getJSONFromUrl(url);
  Cursor sample;
  try 
  {
      JSONArray jar=json.getJSONArray("city");
        Log.d("arindam", ""+jar.length());
      
                    for(int i=0;i<jar.length();i++)
        {
      
   JSONObject j=jar.getJSONObject(i);
     
   //check if the city is already there in citylist.
   sample=sqLite.rawQuery("SELECT CITY_NAME FROM CITY_LIST WHERE CITY_NAME='"+j.getString("cityname")+"'",null);
   
   if(sample.moveToFirst())
   {
    continue;
   }
   else
   {
    sqLite.execSQL("insert into CITY_LIST (CITY_NAME) VALUES ('"+j.getString("cityname")+"')");
    Log.d("arindam",j.getString("cityname"));
   }
      
      }
  } catch (JSONException e) 
  {
      // TODO Auto-generated catch block
        return "perror";
  }
    
    }
    catch(Exception e)
    {
  e.printStackTrace();
  return "error";
    }
   
    return "success";
        }
  
 protected void onPostExecute(String result) 
 { 
    super.onPostExecute(result);
    if(result.equals("perror"))
    {
   moveTaskToBack(true);
   Toast.makeText(getApplicationContext(), "Error!!! Oops..that was humiliating. Could you please try again", Toast.LENGTH_LONG).show();
    }
    else if(result.equals("error"))
    {
   moveTaskToBack(true);
   Toast.makeText(getApplicationContext(), "Please Check your Internet Connection", Toast.LENGTH_LONG).show();
    }
    else
    {
   new Handler().postDelayed(new Runnable(){
   @Override
   public void run()
   {
        
    Cursor c=sqLite.rawQuery("SELECT CITY_NAME FROM USER_PREF",null); 
    Log.d("arindam" ,"c count"+c.getCount());
    if (c.getCount()== 0)
   {
     sqLite.execSQL("INSERT INTO USER_PREF (CITY_NAME, VOICE_ON, NOTIF) VALUES('NONE','Y','Y')");
   }
       
    
    Cursor d=sqLite.rawQuery("SELECT CITY_NAME FROM USER_PREF",null);
    Log.d("arindam" ,"d count"+d.getCount());
       
    if(d.moveToFirst())
    { 
     Log.d("arindam" ,"d NONE"+d.getString(0));
     if(d.getString(0).equals("NONE"))
        {
         //Intent intent=new Intent(StartScreen.this,CityScreen.class);
         //startActivity(intent);
        }
        else
        {
         //Intent intent=new Intent(StartScreen.this,HomeScreen.class);
         //startActivity(intent);
        }
     finish(); 
    }
      }
   },1000);
       }
     }
 }
 
 @Override
 protected void onPostCreate(Bundle savedInstanceState) {
  super.onPostCreate(savedInstanceState);
 }
 
 @Override
 public void onDestroy()
 {
  super.onDestroy();
  sqLite.close();
 }

}

AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.zing.basket"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="18" />
 
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.zing.basket.StartScreen"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
    </application>
    

</manifest>






First Android App | Step 4 - Spinner to select default city

Welcome Back!!

This is the step 4 on our way to build a fully functional e-commerce application from scratch.

In step 3, we created the Splash screen which downloaded the list of Cities from the server in JSON format using AsyncTask. In this post, we are going to build the City selection screen. This screen will only come once on first time login of the user post installation. Once the user selects the default city, next time onwards, the application will move directly to HomeScreen from the StartScreen and skip CityScreen completely.

The complete working of this part has been explained in my youtube video.



The final output will look like this.



The source code is as follows:

activity_city_screen.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#B22222"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".CityScreen" >

    <TextView
        android:id="@+id/SelectCityText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:text="@string/selectcity"
        android:layout_marginTop="120dp"
        android:textColor="#ffffff"
        android:textSize="28sp" />
    
    
    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:layout_below="@+id/SelectCityText"
        android:popupBackground="#470E0E" />

</RelativeLayout>


spinner_item.xml

<?xml version="1.0" encoding="utf-8"?>

<TextView  
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:textSize="20dip"
    android:gravity="left"  
    android:textColor="#ffffff"         
    android:padding="5dip"/>

CityScreen.java

package com.zing.basket;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;


public class CityScreen extends Activity {
 
 SQLiteDatabase sqLite;
 Spinner city_spinner;

 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_city_screen);
  
  Typeface type= Typeface.createFromAsset(getAssets(),"fonts/book.TTF");
  
  //This section is to hide the action bar. 
  ActionBar actionBar = getActionBar();
  actionBar.hide();
  
  //Ideally SQL should be handled in a separate helper, but for ease of understanding to start off, I
  //have kept the code here. 
  sqLite=this.openOrCreateDatabase("basketbuddy", MODE_PRIVATE, null);
  
  Cursor c=sqLite.rawQuery("SELECT CITY_NAME FROM CITY_LIST",null);
  
  //ideally atleast 1 city should be there in city_name. As I have already synced this with the server
  // in StartScreen.java
  
  if (c.getCount()== 0)
  {
   Toast.makeText(getApplicationContext(), "Oh ho..Some unexpected problem. Please restart the application", Toast.LENGTH_LONG).show();
   
  }
  
  TextView city_selection=(TextView) findViewById(R.id.SelectCityText);
  city_selection.setTypeface(type);
  
  //defining the array that will hold the City Names. 
  String[] city_name_db=new String[(c.getCount()+1)];
    
    
  //by default, the first entry for city list is "Choose City". We will understand why 
  //this is necessary later.
  city_name_db[0] = "Choose City";
  
  //moving the city names from sqlite to an array city_name_db
  if(c.moveToFirst())
  {
   int count=1;
   do{
    city_name_db[count]=c.getString(0);
    count++;
   }while(c.moveToNext());
  }
  
  //creating an ArrayAdapter for the spinner and then associating the city ArrayAdapter
  //to the spinner.
  
  ArrayAdapter aa=new ArrayAdapter(getApplicationContext(),R.layout.spinner_item,city_name_db);
  city_spinner = (Spinner) findViewById(R.id.spinner1);
  city_spinner.setAdapter(aa);
  
  //There is an inherent problem with Spinners. Lets assume that there are 3 cities Delhi, Gurgaon and Noida. The
  //moment I populate these 3 cities to the spinner, by default Delhi will get selected as this is the 
  //first entry. OnItemSelectedListener will get triggered immediately with Delhi as selection and the code 
  //will proceed. Net net, even the default first value is taken as an ItemSelected trigger. 
  
  //the way to bypass this is to add a default value 'Choose City' in the ArrayAdapter list. And then
  //inside the onItemSelected method, ignore if 'Choose City' has been selected. 
  
  //SetOnItemSelectedListener listens for any change in item selected, if found thn it will call onItemSelected method. 
  
  city_spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

   @Override
   public void onItemSelected(AdapterView parent, View view,
     int position, long id) 
   {
    if (parent.getItemAtPosition(position).equals("Choose City"))
    {
     //do nothing.
     
    }
    else
    {
     // save the selected city as a default city for shopping
     //this city selection is saved in DB. We may even decide to send this data to server, 
     //however in this example, we are not doing so. 
     
     String city_name=city_spinner.getSelectedItem().toString();
     Cursor c=sqLite.rawQuery("SELECT CITY_NAME FROM USER_PREF",null);
     if (c.getCount()== 0)
     {
      sqLite.execSQL("insert into USER_PREF (CITY_NAME, VOICE_ON) VALUES ('"+city_name+"','Y','Y')");
     }
     if(c.moveToFirst())
     {
      sqLite.execSQL("update USER_PREF set CITY_NAME = '"+city_name+"'");
     }
     
     //Intent intent=new Intent(CityScreen.this,HomeScreen.class);
     //startActivity(intent);
     sqLite.close();
     finish();
    }
   }

   @Override
   public void onNothingSelected(AdapterView arg0) 
   {
    // TODO Auto-generated method stub
    
   }
  });
   
 }
 
 @Override
 protected void onPostCreate(Bundle savedInstanceState) {
  super.onPostCreate(savedInstanceState);
 }
 
 @Override
 public void onResume()
 {
  super.onResume();
  
 }
 
 @Override
 public void onPause()
 {
  super.onPause();
 }

}




Remove the commenting from these 2 lines of code from StartScreen.java inside the onPostExecute method of AsyncTask

Intent intent=new Intent(StartScreen.this,CityScreen.class);
startActivity(intent);

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.zing.basket"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="18" />
 
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.zing.basket.StartScreen"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <activity
            android:name="com.zing.basket.CityScreen"
            android:label="@string/city_screen"
             android:screenOrientation="portrait"
            android:launchMode="singleTask" >
        </activity>
    </application>
    

</manifest>

First Android App | Step 5 | HomeScreen with ViewPager | FragmentPagerAdapter | Action Bar with 3 tabs

Hellos!!

In this post we will discuss the construction of the HomeScreen for our E-Commerce Application. The HomeScreen will have an ActionBar with Menu and 3 tabs which we have implemented through a ViewPager and each of the tabs have been defined as Fragments.

The complete tutorial is available in the following youtube video.




The final result is as follows.




Source Code

activity_home_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.V4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="#ffffff">
    
    
</android.support.V4.view.ViewPager>

fragment_search.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" 
   android:background="#AAE6E6">
   
</RelativeLayout>

fragment_mycart.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:id="@+id/mychecklist"
   android:orientation="vertical" 
   android:background="#00FF33">

</LinearLayout>

fragment_quickorder.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical"
   android:background="#E6BBE6" >

</RelativeLayout>


HomeScreen.java

package com.zing.basket;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;

public class HomeScreen extends FragmentActivity implements ActionBar.TabListener 
{
 
ActionBar bar;
ViewPager viewpager;
FragmentPageAdapter ft;

  
 @Override
 protected void onCreate(Bundle savedInstanceState) 
 {
  super.onCreate(savedInstanceState);
  
  viewpager = new ViewPager(this);
        viewpager.setId(R.id.pager);
        setContentView(viewpager);
        ft = new FragmentPageAdapter(getSupportFragmentManager());
        viewpager.setAdapter(ft);
        
  final ActionBar bar = getActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
        
        bar.addTab(bar.newTab().setText("Search").setTabListener(this));
        bar.addTab(bar.newTab().setText("Cart").setTabListener(this));
        bar.addTab(bar.newTab().setText("Quick Order").setTabListener(this));
        
        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
   
   @Override
   public void onPageSelected(int arg0) {
    // TODO Auto-generated method stub
    bar.setSelectedNavigationItem(arg0);
   }
   
   @Override
   public void onPageScrolled(int arg0, float arg1, int arg2) {
    // TODO Auto-generated method stub
    
   }
   
   @Override
   public void onPageScrollStateChanged(int arg0) {
    // TODO Auto-generated method stub
    
   }
  });
        
        
 }
 
 
 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.start_screen, menu);
        return true;
    }
 
 
 @Override
 public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {
  // TODO Auto-generated method stub
  
 }


 @Override
 public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
  // TODO Auto-generated method stub
  viewpager.setCurrentItem(tab.getPosition());
 }


 @Override
 public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
  // TODO Auto-generated method stub
  
 }
}

FragmentPageAdapter.java

package com.zing.basket;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class FragmentPageAdapter extends FragmentPagerAdapter {

 public FragmentPageAdapter(FragmentManager fm) {
  super(fm);
  // TODO Auto-generated constructor stub
 }

 @Override
 public Fragment getItem(int arg0) {
 
  switch (arg0)
  {
  case 0:
    return new Search();
  case 1: 
    return new MyCart();
  case 2:
    return new QuickOrder();
  default:
    break;
  }
  return null;
 }

 @Override
 public int getCount() {
  // TODO Auto-generated method stub
  return 3;
 }

}


Search.java

package com.zing.basket;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class Search extends Fragment
{
 View myFragmentView;
 
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
 }
 
 
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
 Bundle savedInstanceState) 
 {
  
  myFragmentView = inflater.inflate(R.layout.fragment_search, container, false);  
     
  return myFragmentView;
 }  
 
}



QuickOrder.java

package com.zing.basket;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class QuickOrder extends Fragment {
 
 View myFragmentView;
 
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
 }
 
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
 {
  myFragmentView = inflater.inflate(R.layout.fragment_quickorder, container, false);
   
  return myFragmentView;
 }
}


MyCart.java

package com.zing.basket;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class MyCart extends Fragment {
 
 View myFragmentView;
 
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
 }
 
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
 Bundle savedInstanceState) {
   myFragmentView = inflater.inflate(R.layout.fragment_mycart, container, false);
   
   return myFragmentView;
  }
 
}


Remove the commenting of 2 lines of code in StartScreen.java, inside the onPostExecute method of AsyncTask.

Intent intent=new Intent(StartScreen.this,HomeScreen.class);
startActivity(intent);

Remove the commenting of 2 lines of code in CityScreen.java, where we are calling the HomeScreen.class through intent

Intent intent=new Intent(CityScreen.this,HomeScreen.class);
startActivity(intent);



AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.zing.basket"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="18" />
 
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.zing.basket.StartScreen"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <activity
            android:name="com.zing.basket.CityScreen"
            android:label="@string/city_screen"
             android:screenOrientation="portrait"
            android:launchMode="singleTask" >
        </activity>
        
        <activity
            android:name="com.zing.basket.HomeScreen"
            android:label="@string/home_screen"
             android:screenOrientation="portrait"
            android:launchMode="singleTask" >
        </activity>
        
        
    </application>
    

</manifest>


res/values/strings.xml - Add this line of code

<string name="home_screen">Home Screen</string>


First Android App | Step 6 | Custom Objects

Before we proceed, it is important that we understand the importance and need for custom objects..

The video for the same can be see on youtube



The code for custom object Product.java is as follows. This is to be placed inside com.zing.basket.util (a separate util folder created with /src/ for storing all the util codes)

package com.zing.basket.util;

public class Product{
 
 private String product_name;
 private String product_code;
 private String product_barcode;
 private String product_department;
 private String product_division;
 private String product_family;
 private String product_groupfamily;
 private String product_subfamily;
 private String product_grammage;
 private String product_mrp;
 private String product_bbprice;
 private String product_qty;
 private String product_value;
 
 public void setProductName (String product_name)
 {
     this.product_name = product_name;
 }
 
 public String getProductName()
 {
     return product_name;
 }

 public void setProductGrammage (String product_grammage)
 {
     this.product_grammage = product_grammage;
 }
 
 public String getProductGrammage()
 {
     return product_grammage;
 }
 
 public void setProductBarcode (String product_barcode)
 {
     this.product_barcode = product_barcode;
 }
 
 public String getProductBarcode()
 {
     return product_barcode;
 }
 
 public void setProductDivision (String product_division)
 {
     this.product_division = product_division;
 }
 
 public String getProductDivision()
 {
     return product_division;
 }
 
 public void setProductDepartment (String product_department)
 {
     this.product_department = product_department;
 }
 
 public String getProductDepartment()
 {
     return product_department;
 }
 
 public void setProductFamily (String product_family)
 {
     this.product_family = product_family;
 }
 
 public String getProductFamily()
 {
     return product_family;
 }
 
 public void setProductGroupFamily (String product_groupfamily)
 {
     this.product_groupfamily = product_groupfamily;
 }
 
 public String getProductGroupFamily()
 {
     return product_groupfamily;
 }
 
 public void setProductSubFamily (String product_subfamily)
 {
     this.product_subfamily = product_subfamily;
 }
 
 public String getProductSubFamily()
 {
     return product_subfamily;
 }
 
 public void setProductMRP (String product_mrp)
 {
     this.product_mrp = product_mrp;
 }
 
 public String getProductMRP()
 {
     return product_mrp;
 }
 
 public void setProductBBPrice (String product_bbprice)
 {
     this.product_bbprice = product_bbprice;
 }
 
 public String getProductBBPrice()
 {
     return product_bbprice;
 }
 
 public void setProductQty (String product_qty)
 {
     this.product_qty = product_qty;
 }
 
 public String getProductQty()
 {
     return product_qty;
 }
 
 public void setProductValue (String product_value)
 {
     this.product_value = product_value;
 }
 
 public String getProductValue()
 {
     return product_value;
 }
 
 public void setProductCode(String product_code)
 {
     this.product_code = product_code;
 }
 
 public String getProductCode()
 {
     return product_code;
 }
}


First Android App | Step 7 | Populate ListView with data from JSON | SearchView and Listview to display search results

In this blog, what we are trying to achieve is the search functionality.

There is a search box on the top. When user tries to enter some text, a restful API will be called on the server with this text as the search text. The returned data which is in JSON format will be parsed and pushed into an ArrayList. Then this ArrayList will be used to populate the ListView of the results.

Once the results are displayed, the user can scroll up or down and there would be a provision for user to add this element to cart as well.

The final result looks like this


The complete video on how to achieve this has been explained in the below video.



The source code is as follows

Update the fragment_search.xml with the following code.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" 
   android:background="#E6E6E6">

    <View
            android:id="@+id/view1"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_alignParentTop="true"
            android:background="#FFFFFF"
            android:layout_marginTop="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"/>
    
    <SearchView
        android:id="@+id/searchView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/imageButton1"
        android:layout_marginTop="3dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp">
        
    </SearchView>
    
    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginRight="0dp"
        android:layout_marginTop="3dp"
        android:layout_toLeftOf="@+id/imageButton2"
        android:src="@android:drawable/ic_btn_speak_now" />
    
    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="5dp"
        android:layout_marginTop="3dp"
        android:src="@drawable/barcode"/>
    
    
 <ListView
        android:id="@+id/listview_search"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
     android:layout_below="@+id/imageButton2"
        android:layout_marginTop="25dp" 
        android:dividerHeight="5dp"
        android:divider="#E6E6E6"/>
 
</RelativeLayout>

Please the following file listtwo_searchresults.xml in the res/layout folder

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="#FFFFFF"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_height="match_parent">
    
    
    <ImageView
        android:id="@+id/productImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="5dp"
        android:layout_marginLeft = "5dp"
        android:layout_alignParentTop="True"
        android:src="@drawable/ic_launcher" />
    
    <TextView
        android:id="@+id/product_name"
        android:layout_width="210dp"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/productImage"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="5dp"
        android:text="Product Name"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textSize="18dp" />
    
    <TextView
        android:id="@+id/product_mrp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/productImage"
        android:layout_below="@+id/product_name"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="5dp"
        android:text="MRP:"
        android:textColor="#000000"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="14dp" />
    
    <TextView
        android:id="@+id/product_mrpvalue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/product_mrp"
        android:layout_below="@+id/product_name"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="5dp"
        android:text="Rs 100"
        android:textColor="#000000"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="14dp" />
 
    <TextView
        android:id="@+id/product_bb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/product_mrpvalue"
        android:layout_below="@+id/product_name"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="20dp"
        android:text="BB Price:"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textSize="14dp" />
    
    <TextView
        android:id="@+id/product_bbvalue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/product_bb"
        android:layout_below="@+id/product_name"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="5dp"
        android:text="Rs 95"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textSize="14dp" />

    <Button
        android:id="@+id/add_cart"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_below="@+id/matching_product"
        android:layout_alignParentRight="true"
        android:layout_marginRight ="10dp"
        android:gravity="center"
        android:drawableLeft="@drawable/cart_low" 
        style="?android:attr/borderlessButtonStyle" />
    
</RelativeLayout>


Place the barcode icon in the res/drawable-hdpi folder. Change the name of the file as barcode.jpg





Place the cart icon in the res/drawable-hdpi folder. Change the name of the file as cart_low.jpg





Update the code for Search.java

package com.zing.basket;

package com.zing.basket;

import java.util.ArrayList;

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

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.TextView;
import android.widget.Toast;

import com.zing.basket.util.Product;


public class Search extends Fragment
{
 View myFragmentView;
 SearchView search;
 ImageButton buttonBarcode;
 ImageButton buttonAudio;
 Typeface type;
 ListView searchResults;
 String found = "N";
 
 
 //This arraylist will have data as pulled from server. This will keep cumulating.
 ArrayList<Product> productResults = new ArrayList<Product>();
 //Based on the search string, only filtered products will be moved here from productResults
 ArrayList<Product> filteredProductResults = new ArrayList<Product>();
 
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
 }
 
 
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
 Bundle savedInstanceState) 
 {
  //get the context of the HomeScreen Activity
  final HomeScreen activity = (HomeScreen) getActivity();
  
  //define a typeface for formatting text fields and listview. 
  
  type= Typeface.createFromAsset(activity.getAssets(),"fonts/book.TTF");
  myFragmentView = inflater.inflate(R.layout.fragment_search, container, false);  
     
  search=(SearchView) myFragmentView.findViewById(R.id.searchView1);
        search.setQueryHint("Start typing to search...");
        
  searchResults = (ListView) myFragmentView.findViewById(R.id.listview_search);
  buttonBarcode = (ImageButton) myFragmentView.findViewById(R.id.imageButton2);
  buttonAudio = (ImageButton) myFragmentView.findViewById(R.id.imageButton1);
  
  
  //this part of the code is to handle the situation when user enters any search criteria, how should the 
  //application behave?
  
  search.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() 
        {
      
      @Override
      public void onFocusChange(View v, boolean hasFocus) {
       // TODO Auto-generated method stub
        
       //Toast.makeText(activity, String.valueOf(hasFocus),Toast.LENGTH_SHORT).show();
      }
     });
  
  search.setOnQueryTextListener(new OnQueryTextListener() 
        {
       
      @Override
      public boolean onQueryTextSubmit(String query) {
       // TODO Auto-generated method stub
        
       return false;
      }
      
      @Override
      public boolean onQueryTextChange(String newText) {
       
        if (newText.length() > 3)
        {
         
         searchResults.setVisibility(myFragmentView.VISIBLE);
         myAsyncTask m= (myAsyncTask) new myAsyncTask().execute(newText);
        }
        else
        {
         
         searchResults.setVisibility(myFragmentView.INVISIBLE);
        }
       
       
               
       return false;
      }
      
     });
  return myFragmentView;
 }  
 
 //this filters products from productResults and copies to filteredProductResults based on search text 

 public void filterProductArray(String newText) 
 {

  String pName;
  
  filteredProductResults.clear();
  for (int i = 0; i < productResults.size(); i++)
  {
   pName = productResults.get(i).getProductName().toLowerCase();
   if ( pName.contains(newText.toLowerCase()) ||
     productResults.get(i).getProductBarcode().contains(newText))
   {
    filteredProductResults.add(productResults.get(i));

   }
  }
  
 }
 
 //in this myAsyncTask, we are fetching data from server for the search string entered by user.
 class myAsyncTask extends AsyncTask<String, Void, String> 
 {
  JSONParser jParser;
  JSONArray productList;
  String url=new String();
  String textSearch;
  ProgressDialog pd;


  @Override
  protected void onPreExecute() {
   super.onPreExecute();
   productList=new JSONArray();
   jParser = new JSONParser();
   pd= new ProgressDialog(getActivity());
   pd.setCancelable(false);
   pd.setMessage("Searching...");
   pd.getWindow().setGravity(Gravity.CENTER);
   pd.show();
  }

  @Override
  protected String doInBackground(String... sText) {
   
   url="http://lawgo.in/lawgo/products/user/1/search/"+sText[0];
   String returnResult = getProductList(url);
   this.textSearch = sText[0];
   return returnResult;
   
  }

  public String getProductList(String url)
  {
   
   Product tempProduct = new Product();
   String matchFound = "N";
   //productResults is an arraylist with all product details for the search criteria
   //productResults.clear();
   
   
   try {
    
    
    JSONObject json = jParser.getJSONFromUrl(url);
    
    productList = json.getJSONArray("ProductList");
  
    //parse date for dateList 
    for(int i=0;i<productList.length();i++)
    {
     tempProduct = new Product();
     
     JSONObject obj=productList.getJSONObject(i);
     
     tempProduct.setProductCode(obj.getString("ProductCode"));
     tempProduct.setProductName(obj.getString("ProductName"));
     tempProduct.setProductGrammage(obj.getString("ProductGrammage"));
     tempProduct.setProductBarcode(obj.getString("ProductBarcode"));
     tempProduct.setProductDivision(obj.getString("ProductCatCode"));
     tempProduct.setProductDepartment(obj.getString("ProductSubCode"));
     tempProduct.setProductMRP(obj.getString("ProductMRP"));
     tempProduct.setProductBBPrice(obj.getString("ProductBBPrice"));
     
     //check if this product is already there in productResults, if yes, then don't add it again. 
     matchFound = "N";
     
     for (int j=0; j < productResults.size();j++)
     {
      
      if (productResults.get(j).getProductCode().equals(tempProduct.getProductCode()))
      {
       matchFound = "Y";    
      }
     }
     
     if (matchFound == "N")
     {
       productResults.add(tempProduct);
     }
     
    }
    
    return ("OK");
    
   } catch (Exception e) {
    e.printStackTrace();
    return ("Exception Caught");
   }
  }
  
  @Override
  protected void onPostExecute(String result) {

   super.onPostExecute(result);
   
   if(result.equalsIgnoreCase("Exception Caught"))
   {
    Toast.makeText(getActivity(), "Unable to connect to server,please try later", Toast.LENGTH_LONG).show();

    pd.dismiss();
   }
   else
   {
  
    
    //calling this method to filter the search results from productResults and move them to 
    //filteredProductResults
    filterProductArray(textSearch);
    searchResults.setAdapter(new SearchResultsAdapter(getActivity(),filteredProductResults));
    pd.dismiss();
   }
  }

 }
}

class SearchResultsAdapter extends BaseAdapter
{
  private LayoutInflater layoutInflater;
  
  private ArrayList<Product> productDetails=new ArrayList<Product>();
  int count;
  Typeface type;
  Context context;
  
  //constructor method
 public SearchResultsAdapter(Context context, ArrayList<Product> product_details) {
 
  layoutInflater = LayoutInflater.from(context);
 
   this.productDetails=product_details;
   this.count= product_details.size();
   this.context = context;
   type= Typeface.createFromAsset(context.getAssets(),"fonts/book.TTF");
   
 }

 @Override
 public int getCount() {
  return count;
 }

 @Override
 public Object getItem(int arg0) {
  return productDetails.get(arg0);
 }

 @Override
 public long getItemId(int arg0) {
  return arg0;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) 
 {
  
   ViewHolder holder;
      Product tempProduct = productDetails.get(position);
      
   if (convertView == null) 
      {
       convertView = layoutInflater.inflate(R.layout.listtwo_searchresults, null);
          holder = new ViewHolder();
          holder.product_name = (TextView) convertView.findViewById(R.id.product_name);
          holder.product_mrp = (TextView) convertView.findViewById(R.id.product_mrp);
          holder.product_mrpvalue = (TextView) convertView.findViewById(R.id.product_mrpvalue);
          holder.product_bb = (TextView) convertView.findViewById(R.id.product_bb);
          holder.product_bbvalue = (TextView) convertView.findViewById(R.id.product_bbvalue);
          holder.addToCart = (Button) convertView.findViewById(R.id.add_cart);

    convertView.setTag(holder);
      } 
   else 
      {
             holder = (ViewHolder) convertView.getTag();
      }
  
       
   holder.product_name.setText(tempProduct.getProductName());
   holder.product_name.setTypeface(type);
      
   holder.product_mrp.setTypeface(type);
   
   holder.product_mrpvalue.setText(tempProduct.getProductMRP());
   holder.product_mrpvalue.setTypeface(type);
   
   holder.product_bb.setTypeface(type);
   
   holder.product_bbvalue.setText(tempProduct.getProductBBPrice());
   holder.product_bbvalue.setTypeface(type);
   
      return convertView;
 }
 
  static class ViewHolder 
  {         
         TextView product_name;
         TextView product_mrp;
         TextView product_mrpvalue;
         TextView product_bb;
         TextView product_bbvalue;
         TextView product_savings;
         TextView product_savingsvalue;
         TextView qty;
         TextView product_value;
         Button addToCart;
                
  }
    
}
 



First Android App | Step 8 | ZBar Barcode Scanning in Android Example

In this session, we are going to see how ZBar can be integrated an Android Application. ZBar is one of the most prominent Free Barcode Scanners available.

The following video explains to you step by step on how to implement the same.




Source Code

AndroidManifest.xml

 Add the following lines
Add another activity within 

      <activity 
            android:label="@string/barcode" 
            android:launchmode="singleTop" 
            android:name="com.zing.basket.Barcode" 
            android:screenorientation="portrait" 
            android:theme="@android:style/Theme.Dialog">
        </activity>

Add the following permissions
    
    <uses-permission android:name="android.permission.CAMERA">
    <uses-feature android:name="android.hardware.camera">
    <uses-feature android:name="android.hardware.camera.autofocus">


In res/values/strings.xml, add the following line.

<string name="barcode">Barcode Scanning...</string>

Create a new file in src folder called Barcode.java

package com.zing.basket;

import net.sourceforge.zbar.Config;
import net.sourceforge.zbar.Image;
import net.sourceforge.zbar.ImageScanner;
import net.sourceforge.zbar.Symbol;
import net.sourceforge.zbar.SymbolSet;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PreviewCallback;
import android.hardware.Camera.Size;
import android.os.Bundle;
import android.os.Handler;
import android.widget.FrameLayout;

public class Barcode extends Activity{

 private Camera mCamera;
    private CameraPreview mPreview;
    private Handler autoFocusHandler;
    
    ImageScanner scanner;
    private boolean barcodeScanned = false;
    private boolean previewing = true;
    
    public void onCreate(Bundle savedInstanceState) 
    {
        
        setContentView(R.layout.activity_barcode);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        
        //getActionBar().hide();
  
        autoFocusHandler = new Handler();
        mCamera = getCameraInstance();
        // Instance barcode scanner
        scanner = new ImageScanner();
        scanner.setConfig(0, Config.X_DENSITY, 400);
        scanner.setConfig(0, Config.Y_DENSITY, 400);
        scanner.setConfig(0, Config.ENABLE, 0);
        scanner.setConfig(Symbol.EAN13, Config.ENABLE,1);
        scanner.setConfig(Symbol.EAN8, Config.ENABLE,1);
        scanner.setConfig(Symbol.UPCA, Config.ENABLE,1);
        scanner.setConfig(Symbol.UPCE, Config.ENABLE,1);
        
        mPreview = new CameraPreview(this, mCamera, previewCb, autoFocusCB);
        FrameLayout preview = (FrameLayout)findViewById(R.id.cameraPreview);
        preview.addView(mPreview);
        
        super.onCreate(savedInstanceState);
    }

    
    /** A safe way to get an instance of the Camera object. */
    public static Camera getCameraInstance()
    {
        Camera c = null;
        try 
        {
            c = Camera.open();
        } catch (Exception e)
        {
         //nada
        }
        return c;
    }
 
    private void releaseCamera() 
    {
        if (mCamera != null) 
        {
            previewing = false;
            mCamera.setPreviewCallback(null);
            mCamera.release();
            mCamera = null;
        }
    }

   
    
    PreviewCallback previewCb = new PreviewCallback() 
    {
        public void onPreviewFrame(byte[] data, Camera camera) 
        {
            Camera.Parameters parameters = camera.getParameters();
            Size size = parameters.getPreviewSize();
            Image barcode = new Image(size.width, size.height, "Y800");
            barcode.setData(data);
            int result = scanner.scanImage(barcode);
            if (result != 0) 
            {
                previewing = false;
                mCamera.setPreviewCallback(null);
                mCamera.stopPreview();
                SymbolSet syms = scanner.getResults();
                for (Symbol sym : syms) 
                {
                    barcodeScanned = true;
                    
                    Intent returnIntent = new Intent();
                    returnIntent.putExtra("BARCODE", sym.getData());
                    setResult(1,returnIntent);
                    releaseCamera();
                    finish();
                }
            }
        }
    };
    
    // Mimic continuous auto-focusing
    AutoFocusCallback autoFocusCB = new AutoFocusCallback() 
    {
        public void onAutoFocus(boolean success, Camera camera) 
        {
            autoFocusHandler.postDelayed(doAutoFocus, 1000);
        }
    };
    
    private Runnable doAutoFocus = new Runnable() 
    {
        public void run() 
        {
            if (previewing)
                mCamera.autoFocus(autoFocusCB);
        }
    };
    
    public void onPause() 
    {
        super.onPause();
        releaseCamera();
    }

    @Override
    public void onBackPressed() {
     
     releaseCamera();
     Intent intent = new Intent();
        intent.putExtra("BARCODE","NULL");
        setResult(RESULT_OK, intent);
        super.onBackPressed();
    }

    
}


Create a new file in src folder called CameraPreview.java

package com.zing.basket;

import java.io.IOException;

import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PreviewCallback;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback 
{

    private SurfaceHolder mHolder;
    private Camera mCamera;
    private PreviewCallback previewCallback;
    private AutoFocusCallback autoFocusCallback;
   
    
    public CameraPreview(Context context, Camera camera,PreviewCallback previewCb,AutoFocusCallback autoFocusCb) 
    {
        super(context);
        mCamera = camera;
        previewCallback = previewCb;
        autoFocusCallback = autoFocusCb;
 
        // Set camera to continuous focus if supported, otherwise use
        // software auto-focus. Only works for API level >=9.
 
        // Camera.Parameters parameters = camera.getParameters();
        // for (String f : parameters.getSupportedFocusModes()) {
        //     if (f == Parameters.FOCUS_MODE_CONTINUOUS_PICTURE) {
        //         mCamera.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
        //         autoFocusCallback = null;
        //         break;
        //     }
        // }
         
        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.

        mHolder = getHolder();
        mHolder.addCallback(this);
 
        // deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    } 
 
    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, now tell the camera where to draw the preview.
        try 
        {
            mCamera.setPreviewDisplay(holder);
        } catch (IOException e) {
            Log.d("DBG", "Error setting camera preview: " + e.getMessage());
        }
    }
 
    public void surfaceDestroyed(SurfaceHolder holder) 
    {
        // Camera preview released in activity
    }
 
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        /*
         * If your preview can change or rotate, take care of those events here.
         * Make sure to stop the preview before resizing or reformatting it.
         */
        if (mHolder.getSurface() == null){
          // preview surface does not exist
          return;
        }
        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e){
          // ignore: tried to stop a non-existent preview
        }
        try {
            // Hard code camera surface rotation 90 degs to match Activity view in portrait
            mCamera.setDisplayOrientation(90);
 
            mCamera.setPreviewDisplay(mHolder);
            mCamera.setPreviewCallback(previewCallback);
            mCamera.startPreview();
            mCamera.autoFocus(autoFocusCallback);
        } catch (Exception e){
            Log.d("DBG", "Error starting camera preview: " + e.getMessage());
        }
    }
}


Make the changes in Search.java.

1. Add 2 methods onActivityResult to receive the scanned barcode back from Barcode Activity
2. buttonBarcode.setOnClickListener to call the Barcode Activity.

package com.zing.basket;

import java.util.ArrayList;

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

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.TextView;
import android.widget.Toast;

import com.zing.basket.util.Product;



public class Search extends Fragment
{
 View myFragmentView;
 SearchView search;
 ImageButton buttonBarcode;
 ImageButton buttonAudio;
 Typeface type;
 ListView searchResults;
 String found = "N";
 
 
 //This arraylist will have data as pulled from server. This will keep cumulating.
 ArrayList<Product> productResults = new ArrayList<Product>();
 //Based on the search string, only filtered products will be moved here from productResults
 ArrayList<Product> filteredProductResults = new ArrayList<Product>();
 
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
 }
 
 
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
 Bundle savedInstanceState) 
 {
  //get the context of the HomeScreen Activity
  final HomeScreen activity = (HomeScreen) getActivity();
  
  //define a typeface for formatting text fields and listview. 
  
  type= Typeface.createFromAsset(activity.getAssets(),"fonts/book.TTF");
  myFragmentView = inflater.inflate(R.layout.fragment_search, container, false);  
     
  search=(SearchView) myFragmentView.findViewById(R.id.searchView1);
        search.setQueryHint("Start typing to search...");
        
        search.setIconifiedByDefault(false);
        
  searchResults = (ListView) myFragmentView.findViewById(R.id.listview_search);
  buttonBarcode = (ImageButton) myFragmentView.findViewById(R.id.imageButton2);
  buttonAudio = (ImageButton) myFragmentView.findViewById(R.id.imageButton1);
  
  
  //this part of the code is to handle the situation when user enters any search criteria, how should the 
  //application behave?
  
  buttonBarcode.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View v) 
            {
             startActivityForResult(new Intent(activity, Barcode.class),1); 
            }
        });
  
  search.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() 
        {
      
      @Override
      public void onFocusChange(View v, boolean hasFocus) {
       // TODO Auto-generated method stub
        
       //Toast.makeText(activity, String.valueOf(hasFocus),Toast.LENGTH_SHORT).show();
      }
     });
  
  search.setOnQueryTextListener(new OnQueryTextListener() 
        {
       
      @Override
      public boolean onQueryTextSubmit(String query) {
       // TODO Auto-generated method stub
        
       return false;
      }
      
      @Override
      public boolean onQueryTextChange(String newText) {
       
        if (newText.length() > 3)
        {
         
         searchResults.setVisibility(myFragmentView.VISIBLE);
         myAsyncTask m= (myAsyncTask) new myAsyncTask().execute(newText);
        }
        else
        {
         
         searchResults.setVisibility(myFragmentView.INVISIBLE);
        }
       
       
               
       return false;
      }
      
     });
  return myFragmentView;
 }  
 
 //this captures the result from barcode and populates in the searchView. 
 @Override  
    public void onActivityResult(int requestCode, int resultCode, Intent data)  
    {  
          
  //Log.d("arindam","insideOnActivityResult");
  //super.onActivityResult(requestCode, resultCode, data);    
        if(requestCode==1)  
        {  
         String barcode=data.getStringExtra("BARCODE");
         if (barcode.equals("NULL"))
         {
          //that means barcode could not be identified or user pressed the back button
          //do nothing 
         }
         else
         {
          search.setQuery(barcode, true);
          search.setIconifiedByDefault(false);
         }
        }
          
    } 
 
 //this filters products from productResults and copies to filteredProductResults based on search text 

 public void filterProductArray(String newText) 
 {

  String pName;
  
  filteredProductResults.clear();
  for (int i = 0; i < productResults.size(); i++)
  {
   pName = productResults.get(i).getProductName().toLowerCase();
   if ( pName.contains(newText.toLowerCase()) ||
     productResults.get(i).getProductBarcode().contains(newText))
   {
    filteredProductResults.add(productResults.get(i));

   }
  }
  
 }
 
 //in this myAsyncTask, we are fetching data from server for the search string entered by user.
 class myAsyncTask extends AsyncTask<String, Void, String> 
 {
  JSONParser jParser;
  JSONArray productList;
  String url=new String();
  String textSearch;
  ProgressDialog pd;


  @Override
  protected void onPreExecute() {
   super.onPreExecute();
   productList=new JSONArray();
   jParser = new JSONParser();
   pd= new ProgressDialog(getActivity());
   pd.setCancelable(false);
   pd.setMessage("Searching...");
   pd.getWindow().setGravity(Gravity.CENTER);
   pd.show();
  }

  @Override
  protected String doInBackground(String... sText) {
   
   url="http://lawgo.in/lawgo/products/user/1/search/"+sText[0];
   String returnResult = getProductList(url);
   this.textSearch = sText[0];
   return returnResult;
   
  }

  public String getProductList(String url)
  {
   
   Product tempProduct = new Product();
   String matchFound = "N";
   //productResults is an arraylist with all product details for the search criteria
   //productResults.clear();
   
   
   try {
    
    
    JSONObject json = jParser.getJSONFromUrl(url);
    
    productList = json.getJSONArray("ProductList");
  
    //parse date for dateList 
    for(int i=0;i<productList.length();i++)
    {
     tempProduct = new Product();
     
     JSONObject obj=productList.getJSONObject(i);
     
     tempProduct.setProductCode(obj.getString("ProductCode"));
     tempProduct.setProductName(obj.getString("ProductName"));
     tempProduct.setProductGrammage(obj.getString("ProductGrammage"));
     tempProduct.setProductBarcode(obj.getString("ProductBarcode"));
     tempProduct.setProductDivision(obj.getString("ProductCatCode"));
     tempProduct.setProductDepartment(obj.getString("ProductSubCode"));
     tempProduct.setProductMRP(obj.getString("ProductMRP"));
     tempProduct.setProductBBPrice(obj.getString("ProductBBPrice"));
     
     //check if this product is already there in productResults, if yes, then don't add it again. 
     matchFound = "N";
     
     for (int j=0; j < productResults.size();j++)
     {
      
      if (productResults.get(j).getProductCode().equals(tempProduct.getProductCode()))
      {
       matchFound = "Y";    
      }
     }
     
     if (matchFound == "N")
     {
       productResults.add(tempProduct);
     }
     
    }
    
    return ("OK");
    
   } catch (Exception e) {
    e.printStackTrace();
    return ("Exception Caught");
   }
  }
  
  @Override
  protected void onPostExecute(String result) {

   super.onPostExecute(result);
   
   if(result.equalsIgnoreCase("Exception Caught"))
   {
    Toast.makeText(getActivity(), "Unable to connect to server,please try later", Toast.LENGTH_LONG).show();

    pd.dismiss();
   }
   else
   {
  
    
    //calling this method to filter the search results from productResults and move them to 
    //filteredProductResults
    filterProductArray(textSearch);
    searchResults.setAdapter(new SearchResultsAdapter(getActivity(),filteredProductResults));
    pd.dismiss();
   }
  }

 }
}

class SearchResultsAdapter extends BaseAdapter
{
  private LayoutInflater layoutInflater;
  
  private ArrayList<Product> productDetails=new ArrayList<Product>();
  int count;
  Typeface type;
  Context context;
  
  //constructor method
 public SearchResultsAdapter(Context context, ArrayList<Product> product_details) {
 
  layoutInflater = LayoutInflater.from(context);
 
   this.productDetails=product_details;
   this.count= product_details.size();
   this.context = context;
   type= Typeface.createFromAsset(context.getAssets(),"fonts/book.TTF");
   
 }

 @Override
 public int getCount() {
  return count;
 }

 @Override
 public Object getItem(int arg0) {
  return productDetails.get(arg0);
 }

 @Override
 public long getItemId(int arg0) {
  return arg0;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) 
 {
  
   ViewHolder holder;
      Product tempProduct = productDetails.get(position);
      
   if (convertView == null) 
      {
       convertView = layoutInflater.inflate(R.layout.listtwo_searchresults, null);
          holder = new ViewHolder();
          holder.product_name = (TextView) convertView.findViewById(R.id.product_name);
          holder.product_mrp = (TextView) convertView.findViewById(R.id.product_mrp);
          holder.product_mrpvalue = (TextView) convertView.findViewById(R.id.product_mrpvalue);
          holder.product_bb = (TextView) convertView.findViewById(R.id.product_bb);
          holder.product_bbvalue = (TextView) convertView.findViewById(R.id.product_bbvalue);
          holder.addToCart = (Button) convertView.findViewById(R.id.add_cart);

    convertView.setTag(holder);
      } 
   else 
      {
             holder = (ViewHolder) convertView.getTag();
      }
  
       
   holder.product_name.setText(tempProduct.getProductName());
   holder.product_name.setTypeface(type);
      
   holder.product_mrp.setTypeface(type);
   
   holder.product_mrpvalue.setText(tempProduct.getProductMRP());
   holder.product_mrpvalue.setTypeface(type);
   
   holder.product_bb.setTypeface(type);
   
   holder.product_bbvalue.setText(tempProduct.getProductBBPrice());
   holder.product_bbvalue.setTypeface(type);
   
      return convertView;
 }
 
  static class ViewHolder 
  {         
         TextView product_name;
         TextView product_mrp;
         TextView product_mrpvalue;
         TextView product_bb;
         TextView product_bbvalue;
         TextView product_savings;
         TextView product_savingsvalue;
         TextView qty;
         TextView product_value;
         Button addToCart;
         
         
  }
  
  
  
}

 


Download this zip file and place the contents in the /libs folder of your project.

Download zbar zip file


First Android App | Step 9 | Speech Recognizer in Android

In this step, we are going to see how to integrate the Google's speech recognizer to capture voice and get the text conversion back to your activity.

In this case, the speech recognizer has been used on the Search tab as can be see below. There is a button next to searchView where we are integrating the speech recognizer.


Below is the YouTube video to explain how the integration was done.



Source Code

Changes in Search.java

1. Add setOnClickListener for audio button.

        buttonAudio.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View v) 
            {
             Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                // Specify free form input
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                intent.putExtra(RecognizerIntent.EXTRA_PROMPT,"Please start speaking");
                intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.ENGLISH);
                startActivityForResult(intent, 2); 
            }
        });

2. Add code in onActivityResult to retrieve results and populate the searchView.

@Override  
    public void onActivityResult(int requestCode, int resultCode, Intent data)  
    {  
        //this requestCode is for handling the barcode activity.       
        if(requestCode==1)  
        {  
         String barcode=data.getStringExtra("BARCODE");
         if (barcode.equals("NULL"))
         {
          //that means barcode could not be identified or user pressed the back button
          //do nothing 
         }
         else
         {
          search.setQuery(barcode, true);
          search.setIconifiedByDefault(false);
         }
        }
        
        //this requestCode is for speechRecognizer. Only this part of the code needs to be added for
        //the implementation of voice to text functionality. 
 
        if (requestCode == 2) {
            ArrayList results;
            results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            //Toast.makeText(this, results.get(0), Toast.LENGTH_SHORT).show();
            
            //if the name has an ' then the SQL is failing. Hence replacing them. 
            String text = results.get(0).replace("'","");
            search.setQuery(text, true);
         search.setIconifiedByDefault(false);
        }
          
    }

First Android App | Step 10 | Implementing Button within Listview

In the search results, there is an option for the user to add to Cart. This has been implemented by using a Button or imageButton.

The complete view on how this can be implemented is available below.



Source Code

In Search.java, we have added a onClickListener for the button and a custom onClickListener to handle the click. 

inside getView of SearchResultsAdapter, we set up the custom listener

holder.addToCart.setOnClickListener(new MyPersonalClickListener("button_addtocart",tempProduct,context));

and then inside the SearchResultsAdapter class, add the following code to handle onClick

//this is a customized clicklistener
   public class MyPersonalClickListener implements OnClickListener
      {

       String button_name;
       Product prod_name;
       int tempQty;
       int tempValue;
       SQLiteDatabase sqLite;
       Context context;
       
       //constructor method
       public MyPersonalClickListener(String button_name, Product prod_name, Context context) 
       {
            this.prod_name = prod_name;
            this.button_name = button_name;
            this.context = context;
       }

       @Override
       public void onClick(View v)
       {
          
        // in this section, we are going to add items to cart
        //if the item is already there in cart, then increase the quantity by 1, else add the item to cart
        //if the quantity of the item has reached 10, then do nothing ---this is just a specific logic where
        //i did not want any item with quantity more than 10, but if you choose not to, then just comment out
        //the code. 
     sqLite=context.openOrCreateDatabase("basketbuddy", context.MODE_PRIVATE, null);
     
     Cursor cc = sqLite.rawQuery("SELECT PRODUCT_QTY, PRODUCT_VALUE FROM CART WHERE PRODUCT_CODE ="+Integer.parseInt(prod_name.getProductCode()), null);
     
     if (cc.getCount()== 0)
     {
           //product not already there in cart..add to cart
      sqLite.execSQL("INSERT INTO CART (PRODUCT_CODE, PRODUCT_NAME, PRODUCT_BARCODE, PRODUCT_GRAMMAGE"+
            ", PRODUCT_MRP, PRODUCT_BBPRICE, PRODUCT_DIVISION, PRODUCT_DEPARTMENT,PRODUCT_QTY,PRODUCT_VALUE) VALUES("+
           prod_name.getProductCode()+",'"+ prod_name.getProductName()+ "','" +
            prod_name.getProductBarcode()+"','"+ prod_name.getProductGrammage()+"',"+
           Integer.parseInt(prod_name.getProductMRP())+","+ Integer.parseInt(prod_name.getProductBBPrice())+","+
            Integer.parseInt(prod_name.getProductDivision())+","+Integer.parseInt(prod_name.getProductDepartment())+
            ",1,"+ Integer.parseInt(prod_name.getProductBBPrice())+")");
           
         Toast.makeText(context,"Item "+prod_name.getProductName()+" added to Cart", Toast.LENGTH_LONG).show();
     }
     else
     {
      
      //product already there in cart
      if(cc.moveToFirst())
     {
      do{
       tempQty=cc.getInt(0);
       tempValue = cc.getInt(1);
      }while(cc.moveToNext());
     }
      
      if (tempQty < 10)
      {
       sqLite.execSQL("UPDATE CART SET PRODUCT_QTY = "+ (tempQty+1)+",PRODUCT_VALUE = "+ 
     (Integer.parseInt(prod_name.getProductBBPrice())+tempValue)+" WHERE PRODUCT_CODE ="+
     prod_name.getProductCode());
       
       Toast.makeText(context,"Item "+prod_name.getProductName()+" added to Cart", Toast.LENGTH_LONG).show();
      }
     }

     sqLite.close();
              
       }

    }

First Android App | Step 11 | Shopping Cart | Delete within ListView and Refresh ListView

In this session, we will see how to create a Cart for our E-Commerce Application.

The user will be able to view items in his cart, change quantity, delete item and then proceed to checkout.

The final output will look like this.



The complete video of the tutorial is available in the following YouTube video.




Source Code is as follows:

fragment_mycart.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:id="@+id/mychecklist"
   android:orientation="vertical" 
   android:background="#E6E6E6">

    <RelativeLayout 
   android:layout_width="fill_parent"
   android:layout_height="45dp" >
        
        <TextView
            android:id="@+id/item_text"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Items"
            android:textColor="#474747"
            android:textSize="14dp"
            android:layout_marginTop="4dp"
            android:layout_marginLeft="10dp"/>
            
       <TextView
            android:id="@+id/item_count"
            android:layout_toRightOf="@+id/item_text"
            android:layout_alignParentTop="true"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="(2)"
            android:textColor="#474747"
            android:textSize="14dp"
            android:layout_marginTop="4dp"
            android:layout_marginLeft="5dp"/>
        
       <TextView
            android:id="@+id/shipping_text"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/item_text"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Shipping:"
            android:textColor="#474747"
            android:textSize="14dp"
            android:layout_marginTop="2dp"
            android:layout_marginLeft="10dp"/>
       
       <TextView
            android:id="@+id/shipping_amount"
            android:layout_toRightOf="@+id/shipping_text"
            android:layout_alignTop="@+id/shipping_text"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Rs.50"
            android:textColor="#474747"
            android:textSize="14dp"
            android:layout_marginLeft="5dp"/>
       
             
        <TextView
            android:id="@+id/total_amount"
            android:layout_toLeftOf="@+id/checkout"
            android:layout_alignParentTop="true"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Rs. 5700"
            android:textColor="#000000"
            android:textSize="20dp"
            android:layout_marginTop="12dp"
            android:layout_marginRight="10dp"/>

        <Button
            android:id="@+id/checkout"
            android:layout_width="wrap_content"
            android:layout_height="25dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="4dp"
            android:layout_marginTop="4dp"
            android:background="#A2A2A2"
            android:padding="2dp"
            android:text="Checkout >>"
            android:textSize="16dp" />
         
           
      <TextView
            android:id="@+id/cart_empty"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="CART IS EMPTY"
            android:textColor="#474747"
            android:textSize="20dp"
            android:layout_marginTop="12dp"
            android:layout_marginLeft="50dp"/>    
         
         
         
    </RelativeLayout>
    
    
    
    <View
        android:id="@+id/view1"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="5dp"
        android:background="#BABABA" />
    
    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:layout_margin="5dp">
        
    </ListView>
   
</LinearLayout>



listone_custom.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageButton
        android:id="@+id/delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:background="#FFFFFF"
        android:scaleX="1"
        android:scaleY="1"
        android:src="@android:drawable/ic_delete" />

    <TextView
        android:id="@+id/product_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="10dp"
        android:layout_toLeftOf="@+id/delete"
        android:text="Kelloggs Corn Flakes Honey and Almond 400gm"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/product_mrp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/product_name"
        android:layout_below="@+id/product_name"
        android:layout_marginTop="15dp"
        android:text="MRP: "
        android:textSize="14dp" 
        android:textColor="#757575"/>

    <TextView
        android:id="@+id/product_mrpvalue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/product_mrp"
        android:layout_marginLeft="0dp"
        android:layout_toRightOf="@+id/product_mrp"
        android:text="Rs.20000"
        android:textSize="14dp" 
        android:textColor="#757575"/>

    <TextView
        android:id="@+id/product_bb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/product_name"
        android:layout_below="@+id/product_mrp"
        android:layout_marginTop="5dp"
        android:text="BB:  "
        android:textSize="14dp" 
        android:textColor="#757575"/>

    <TextView
        android:id="@+id/product_bbvalue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/product_mrpvalue"
        android:layout_below="@+id/product_mrpvalue"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="5dp"
        android:text="Rs.15000"
        android:textSize="14dp" 
        android:textColor="#757575"/>

    <View
        android:id="@+id/view1"
        android:layout_width="1dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/product_savings"
        android:layout_below="@+id/product_name"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_toRightOf="@+id/product_mrpvalue"
        android:background="#E6E6E6" />
 
    <TextView
        android:id="@+id/qty_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/view1"
        android:layout_below="@+id/product_name"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="10dp"
        android:text="Qty"
        android:textSize="14dp" 
        android:textColor="#757575"/>
    
    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="50dp"
        android:layout_height="40dp"
        android:layout_below="@+id/qty_text"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="0dp"
        android:layout_toRightOf="@+id/view1"/>
    
    <View
        android:id="@+id/view2"
        android:layout_width="1dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/product_savings"
        android:layout_below="@+id/product_name"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_toRightOf="@+id/spinner1"
        android:background="#E6E6E6" />
    
    
    <TextView
        android:id="@+id/product_value"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/product_name"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_toRightOf="@+id/view2"
        android:text="Rs 5000"
        android:textColor="#191919"
        android:textSize="20dp" />
 
    <View
        android:id="@+id/view3"
        android:layout_width="wrap_content"
        android:layout_height="1dp"
        android:layout_alignRight="@+id/product_savingsvalue"
        android:layout_below="@+id/product_value"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"
        android:layout_toRightOf="@+id/view2"
        android:background="#E6E6E6" />
    
    <TextView
        android:id="@+id/product_savings"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/view3"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"
        android:layout_toRightOf="@+id/view2"
        android:text="Savings: "
        android:textColor="#5E5E5E"
        android:textSize="14dp" />
 
    <TextView
        android:id="@+id/product_savingsvalue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/view3"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="5dp"
        android:layout_toRightOf="@+id/product_savings"
        android:text="Rs 12000 "
        android:textColor="#5E5E5E"
        android:textSize="14dp" />
        
<View
        android:id="@+id/view4"
        android:layout_width="match_parent"
        android:layout_height="10dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/view1"
        android:layout_marginTop="5dp"
        android:background="#E6E6E6" />

</RelativeLayout>

qty_spinner.item.xml

<?xml version="1.0" encoding="utf-8"?>

<TextView  
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:textSize="16dip"
    android:gravity="center"  
    android:textColor="#000000"         
    android:padding="0dp"
    />

MyCart.java

package com.zing.basket;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.zing.basket.util.Product;


@SuppressLint("ShowToast")
public class MyCart extends Fragment {
 
 ArrayList<Product&lgt; cart_list = new ArrayList<Product&lgt;();
 SQLiteDatabase sqLite;
 int count=0;
 int totalCartItemCount =0;
 int totalCartValue = 0;
 View myFragmentView;
 final String[] qtyValues = {"1","2","3","4","5","6","7","8","9","10"};
 
 
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
 }
 
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
 Bundle savedInstanceState) {
   myFragmentView = inflater.inflate(R.layout.fragment_mycart, container, false);
   
   getCartData();
   totalCartItemCount = cart_list.size();
   totalCartValue =0;
   for (int temp1=0; temp1 < cart_list.size(); temp1++)
   {
    totalCartValue = totalCartValue + Integer.parseInt(cart_list.get(temp1).getProductValue());
   }
   HomeScreen activity = (HomeScreen) getActivity();
   
   Typeface type= Typeface.createFromAsset(activity.getAssets(),"fonts/book.TTF");
   
   TextView itemText = (TextView) myFragmentView.findViewById(R.id.item_text);
   TextView itemCount = (TextView) myFragmentView.findViewById(R.id.item_count);
   TextView shippingText = (TextView) myFragmentView.findViewById(R.id.shipping_text);
   TextView shippingAmount = (TextView) myFragmentView.findViewById(R.id.shipping_amount);
   TextView totalAmount = (TextView) myFragmentView.findViewById(R.id.total_amount);
   Button checkout = (Button) myFragmentView.findViewById(R.id.checkout);
   ListView lv1=(ListView) myFragmentView.findViewById(R.id.listView1);
   TextView cartEmpty = (TextView) myFragmentView.findViewById(R.id.cart_empty);
   
   if (totalCartItemCount == 0)
   {
    itemText.setVisibility(myFragmentView.INVISIBLE);
    itemCount.setVisibility(myFragmentView.INVISIBLE);
    shippingText.setVisibility(myFragmentView.INVISIBLE);
    shippingAmount.setVisibility(myFragmentView.INVISIBLE);
    totalAmount.setVisibility(myFragmentView.INVISIBLE);
    checkout.setVisibility(myFragmentView.INVISIBLE);
    lv1.setVisibility(myFragmentView.INVISIBLE);
    cartEmpty.setVisibility(myFragmentView.VISIBLE);
   }
   
   else
   {
    itemText.setVisibility(myFragmentView.VISIBLE);
    itemCount.setVisibility(myFragmentView.VISIBLE);
    shippingText.setVisibility(myFragmentView.VISIBLE);
    shippingAmount.setVisibility(myFragmentView.VISIBLE);
    totalAmount.setVisibility(myFragmentView.VISIBLE);
    checkout.setVisibility(myFragmentView.VISIBLE);
    lv1.setVisibility(myFragmentView.VISIBLE);
    cartEmpty.setVisibility(myFragmentView.INVISIBLE);
    
   }
   
   
   itemCount.setText("("+ totalCartItemCount + ")");
   if (totalCartValue &lgt; 500)
   {
    shippingAmount.setText("Free");
    totalAmount.setText("Rs "+ totalCartValue);
   }
   else
   {
    shippingAmount.setText("Rs 50");
    totalAmount.setText("Rs "+ (totalCartValue+50));
   }
   
   
   itemText.setTypeface(type);
   itemCount.setTypeface(type);
   shippingText.setTypeface(type);
   shippingAmount.setTypeface(type);
   totalAmount.setTypeface(type);
   checkout.setTypeface(type);
   
   
   lv1.setAdapter(new custom_list_one(this.getActivity(),cart_list));
   
   return myFragmentView;
  }
 
 class custom_list_one extends BaseAdapter
 {
   private LayoutInflater layoutInflater;
   ViewHolder holder;
   private ArrayList<Product&lgt; cartList=new ArrayList<Product&lgt;();
   int cartCounter;
   Typeface type;
   Context context;
   
  public custom_list_one(Context context, ArrayList<Product&lgt; cart_list) {
   layoutInflater = LayoutInflater.from(context);
    this.cartList=cart_list;
    this.cartCounter= cartList.size();
    this.context = context;
    type= Typeface.createFromAsset(context.getAssets(),"fonts/book.TTF");
  }

  @Override
  public int getCount() {

   return cartCounter;
  }

  @Override
  public Object getItem(int arg0) {

   return cartList.get(arg0);
  }

  @Override
  public long getItemId(int arg0) {

   return arg0;
  }

  @Override
  public View getView(final int position, View convertView, ViewGroup parent) 
  {
   Product tempProduct = cart_list.get(position);

   
    if (convertView == null) 
       {
        convertView = layoutInflater.inflate(R.layout.listone_custom, null);
           holder = new ViewHolder();
           holder.name = (TextView) convertView.findViewById(R.id.product_name);
           holder.product_mrp = (TextView) convertView.findViewById(R.id.product_mrp);
           holder.product_mrpvalue = (TextView) convertView.findViewById(R.id.product_mrpvalue);
           holder.qty = (Spinner) convertView.findViewById(R.id.spinner1);
           holder.cancel = (ImageButton) convertView.findViewById(R.id.delete);
           holder.product_value = (TextView) convertView.findViewById(R.id.product_value);
           holder.qty_text =(TextView) convertView.findViewById(R.id.qty_text);
           holder.product_bb = (TextView) convertView.findViewById(R.id.product_bb);
           holder.product_bbvalue = (TextView) convertView.findViewById(R.id.product_bbvalue);
           holder.product_savings = (TextView) convertView.findViewById(R.id.product_savings);
           holder.product_savingsvalue = (TextView) convertView.findViewById(R.id.product_savingsvalue);
           
           convertView.setTag(holder);
       } 
       else 
       {
              holder = (ViewHolder) convertView.getTag();
       }
   
       
    holder.name.setText(tempProduct.getProductName());
    holder.name.setTypeface(type);
    
    holder.product_mrp.setTypeface(type);
    
    
    holder.product_mrpvalue.setText("Rs "+tempProduct.getProductMRP());
    holder.product_mrpvalue.setTypeface(type);
    
    
    ArrayAdapter<String&lgt; aa=new ArrayAdapter<String&lgt;(context,R.layout.qty_spinner_item,qtyValues);
    aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    
    holder.qty.setAdapter(aa);
   
    holder.qty.setSelection(Integer.parseInt(tempProduct.getProductQty())-1);
    
    holder.product_bb.setTypeface(type);
    
    holder.product_bbvalue.setText("Rs "+tempProduct.getProductBBPrice());
    holder.product_bbvalue.setTypeface(type);
    
    holder.product_savings.setTypeface(type);
    
    holder.product_savingsvalue.setText("Rs "+(Integer.parseInt(tempProduct.getProductMRP())-Integer.parseInt(tempProduct.getProductBBPrice()))*Integer.parseInt(tempProduct.getProductQty())+"");
    holder.product_savingsvalue.setTypeface(type);
    
    holder.qty_text.setTypeface(type);
    
    holder.product_value.setText("Rs "+Integer.parseInt(tempProduct.getProductBBPrice())*Integer.parseInt(tempProduct.getProductQty())+"");
    holder.product_value.setTypeface(type);
    

    
    holder.cancel.setOnClickListener(new MyPersonalClickListener("button_delete",tempProduct)); 
          
    holder.qty.setOnItemSelectedListener(new OnItemSelectedListener(){
    
    @Override
    public void onItemSelected(AdapterView<?&lgt; parent, View view,int selectionIndex, long id) 
    {
      //if user has changed the quantity, then save it in the DB. refresh cart_list
     
      if ((parent.getSelectedItemPosition()+1) != Integer.parseInt(cart_list.get(position).getProductQty()))
      {
       
       sqLite=context.openOrCreateDatabase("basketbuddy", context.MODE_PRIVATE, null);
       sqLite.execSQL("UPDATE CART SET PRODUCT_QTY ='"+ (parent.getSelectedItemPosition()+1)+"' WHERE PRODUCT_CODE ='"+cart_list.get(position).getProductCode()+"'");
       sqLite.execSQL("UPDATE CART SET PRODUCT_VALUE='" + (parent.getSelectedItemPosition()+1) * Integer.parseInt(cart_list.get(position).getProductBBPrice())  +"' WHERE PRODUCT_CODE ='"+cart_list.get(position).getProductCode()+"'");
       sqLite.close();
       getCartData();
       
       notifyDataSetChanged();
       
       //refresh data outside the listview - Cart Total, Total Items, Shipping Cost etc
       View parentView = (View) view.getParent().getParent().getParent().getParent();
       
       TextView txtTotalAmount = (TextView) parentView.findViewById(R.id.total_amount);
       TextView txtTotalItems = (TextView) parentView.findViewById(R.id.item_count);
       TextView txtShippingAmt = (TextView) parentView.findViewById(R.id.shipping_amount);
       
       totalCartItemCount = cart_list.size();
       totalCartValue =0;
       
       for (int temp1=0; temp1 < cart_list.size(); temp1++)
       {
        totalCartValue = totalCartValue + Integer.parseInt(cart_list.get(temp1).getProductValue());
       }
       
       txtTotalItems.setText("("+ totalCartItemCount + ")");
       
         if (totalCartValue &lgt; 500)
         {
          txtShippingAmt.setText("Free");
          txtTotalAmount.setText("Rs "+ totalCartValue);
         }
         else
         {
          txtShippingAmt.setText("Rs 50");
          txtTotalAmount.setText("Rs "+ (totalCartValue+50));
         }
      }
    }

    @Override
    public void onNothingSelected(AdapterView<?&lgt; arg0) 
    {

    }
    });
    
       return convertView;
  }
   class ViewHolder 
   {         
          TextView name;
          TextView product_mrp;
          TextView product_mrpvalue;
          TextView product_bb;
          TextView product_bbvalue;
          TextView qty_text;
          TextView product_savings;
          TextView product_savingsvalue;
          TextView product_value;
          ImageButton cancel;
          Spinner qty;
          
   }
   
 }
 
 public class MyPersonalClickListener implements OnClickListener
    {

      String button_name;
      Product prod_name;
      int tempQty;
      int tempValue;
      
      public MyPersonalClickListener(String button_name, Product prod_name) 
      {
           this.prod_name = prod_name;
           this.button_name = button_name;
      }

      @Override
      public void onClick(View v)
      {
  
       if (button_name == "button_delete")
          {
        sqLite=getActivity().openOrCreateDatabase("basketbuddy", getActivity().MODE_PRIVATE, null);
        sqLite.execSQL("DELETE FROM CART WHERE PRODUCT_CODE ="+Integer.parseInt(prod_name.getProductCode()));
          sqLite.close();
          Toast.makeText(getActivity(),"Item "+prod_name.getProductName()+" deleted from Cart", Toast.LENGTH_LONG).show();
          
          getCartData();
          
          View lView = (View) v.getParent().getParent();
          
          ((ListView) lView).setAdapter(new custom_list_one(getActivity(),cart_list));
       
          
          TextView txtTotalAmount = (TextView) myFragmentView.findViewById(R.id.total_amount);
          TextView txtTotalItems = (TextView) myFragmentView.findViewById(R.id.item_count);
          TextView txtShippingAmt = (TextView) myFragmentView.findViewById(R.id.shipping_amount);   
          TextView itemText = (TextView) myFragmentView.findViewById(R.id.item_text);
          TextView shippingText = (TextView) myFragmentView.findViewById(R.id.shipping_text);
          Button checkout = (Button) myFragmentView.findViewById(R.id.checkout);
          ListView lv1=(ListView) myFragmentView.findViewById(R.id.listView1);
          TextView cartEmpty = (TextView) myFragmentView.findViewById(R.id.cart_empty);
          
          totalCartItemCount = cart_list.size();
     totalCartValue =0;
     for (int temp1=0; temp1 < cart_list.size(); temp1++)
     {
      totalCartValue = totalCartValue + Integer.parseInt(cart_list.get(temp1).getProductValue());
     }
    
     txtTotalItems.setText("("+ totalCartItemCount + ")");
    
     if (totalCartValue &lgt; 500)
     {
      txtShippingAmt.setText("Free");
      txtTotalAmount.setText("Rs "+ totalCartValue);
     }
     else
     {
      txtShippingAmt.setText("Rs 50");
      txtTotalAmount.setText("Rs "+ (totalCartValue+50));
     }
     
     
     if (totalCartItemCount == 0)
     {
      itemText.setVisibility(myFragmentView.INVISIBLE);
      txtTotalItems.setVisibility(myFragmentView.INVISIBLE);
      shippingText.setVisibility(myFragmentView.INVISIBLE);
      txtShippingAmt.setVisibility(myFragmentView.INVISIBLE);
      txtTotalAmount.setVisibility(myFragmentView.INVISIBLE);
      checkout.setVisibility(myFragmentView.INVISIBLE);
      lv1.setVisibility(myFragmentView.INVISIBLE);
      cartEmpty.setVisibility(myFragmentView.VISIBLE);
     }
     
     else
     {
      itemText.setVisibility(myFragmentView.VISIBLE);
      txtTotalItems.setVisibility(myFragmentView.VISIBLE);
      shippingText.setVisibility(myFragmentView.VISIBLE);
      txtShippingAmt.setVisibility(myFragmentView.VISIBLE);
      txtTotalAmount.setVisibility(myFragmentView.VISIBLE);
      checkout.setVisibility(myFragmentView.VISIBLE);
      lv1.setVisibility(myFragmentView.VISIBLE);
      cartEmpty.setVisibility(myFragmentView.INVISIBLE);
      
     } 

          }
       
      }

   }
 
 public void getCartData() {
  

  HomeScreen activity = (HomeScreen) getActivity();
  Product tempCartItem = new Product();
  
  cart_list.clear();
  sqLite=activity.openOrCreateDatabase("basketbuddy", activity.MODE_PRIVATE, null);
  Cursor c=sqLite.rawQuery("SELECT  * FROM CART",null);
  count=0;
  if(c.moveToFirst())
  {
   do{
    
    tempCartItem = new Product();
    tempCartItem.setProductCode(c.getString(c.getColumnIndex("PRODUCT_CODE")));
    tempCartItem.setProductName(c.getString(c.getColumnIndex("PRODUCT_NAME")));
    tempCartItem.setProductBarcode(c.getString(c.getColumnIndex("PRODUCT_BARCODE")));
    tempCartItem.setProductGrammage(c.getString(c.getColumnIndex("PRODUCT_GRAMMAGE")));
    tempCartItem.setProductDivision(c.getString(c.getColumnIndex("PRODUCT_DIVISION")));
    tempCartItem.setProductDepartment(c.getString(c.getColumnIndex("PRODUCT_DEPARTMENT")));
    tempCartItem.setProductBBPrice(c.getString(c.getColumnIndex("PRODUCT_BBPRICE")));
    tempCartItem.setProductMRP(c.getString(c.getColumnIndex("PRODUCT_MRP")));
    tempCartItem.setProductQty(c.getString(c.getColumnIndex("PRODUCT_QTY")));
    tempCartItem.setProductValue(c.getString(c.getColumnIndex("PRODUCT_VALUE")));
    cart_list.add(tempCartItem);
    count++;
   }while(c.moveToNext());
   
  }
  sqLite.close();
  
 }
}


Comments

Popular posts from this blog

web2apk

http://web2apk.com/create.aspx Create App   Intro   About   Changes   MalWare ?   Contact   Privacy Useful Links Bluetooth Mini Keyboards Android Mini PC Reset Android URL App Title Icon or

Android Bar Chart Using MpAndroidChart Library Tutorial

https://www.numetriclabz.com/android-bar-chart-using-mpandroidchart-library-tutorial/ Android Bar Chart Using MpAndroidChart Library Tutorial Objective In this tutorial we learn how to implement Bar Chart using MpAndroidChart Library in your Android App. Download Source Code       Step 1 Contents ·        1  Introduction ·        2  Creating Bar chart o    2.1  Create a new Project o    2.2  Adding library in Project o    2.3  Create Layout o    2.4  To Plot Bar Chart §   2.4.1  Initialize the graph id §   2.4.2  Creating a Dataset §   2.4.3  Defining X-axis labels §   2.4.4  Set the data §   2.4.5  Add the description to the chart §   2.4.6  Run your App § ...

how to retrieve image from sqlite database in android and display in listview

 Android platform provides several ways to store data in our application. 1. SQLite database 2. SharedPreferences etc For our post, we will only work with SQLite database. First and foremost, we need to understand what an SQLite database is? SQLite database  is an open source SQL database that stores data to a text file on a device. It executes SQL Commands to perform a set of functions, that is, create, read, update and delete operations. On my previous post, I showed how to  store data in SQLite database from edit text, retrieve and populate it in a listview . For this post, I will show the SQLite CRUD operations with images from gallery and text from EditText. We need to understand this; images are stored in SQLite database as BLOB data type. A BLOB is a large binary object that can hold a variable amount of data.  Note, we can only store images in the database as BLOB data type. We need to convert our image path to a bitmap th...