Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
MainActivity.java

Java
package com.example.ecomm;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
final LinearLayout layout = (LinearLayout)findViewById(R.id.linearMain);
final Button btn = (Button)findViewById(R.id.second);

final Controller ct = (Controller) getApplicationContext();
        
        
 ModelProducts products = null; 
 
         for(int i= 1; i<=7;i++){
        	 
        	  int Price = 15+ i;
        	  
        	  products = new ModelProducts("Product Item" +i, "Description"+i, Price);
        	 
        	  ct.setProducts(products);
         }
        
         
         int productsize = ct.getProductArraylistsize();
         
         LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
         
         
         for (int j=0;j< productsize;j++){
        	 
        	 
        	String pName = ct.getProducts(j).getProductName(); 
        	int pPrice = ct.getProducts(j).getProductPrice();
        	
        	
        	 LinearLayout la = new LinearLayout(this);
        	
        	 la.setOrientation(LinearLayout.HORIZONTAL);
        	    
        	 TextView tv = new TextView(this);
        	 
        	 tv.setText(" "+pName+" ");
        	 
        	 la.addView(tv);
        	    
             TextView tv1 = new TextView(this);
        	 
        	 tv1.setText("$"+pPrice+" ");
        	 
        	 la.addView(tv1);
        	 
        	  final Button btn1 = new Button(this);
        	  
        	               btn1.setId(j+1);
        	               btn1.setText("Add to Cart");
        	               
        	     
        	        btn1.setLayoutParams(params);
        	        
        	   final int index = j;
        	   
        	   btn1.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					// TODO Auto-generated method stub
					
					
					Log.i("TAG", "index:"+index);
					
					
					ModelProducts productsObject = ct.getProducts(index);
					
					
					if(!ct.getCart().CheckProductInCart(productsObject)){
					
					btn1.setText("Item Added");
					
					ct.getCart().setProducts(productsObject);
							
					Toast.makeText(getApplicationContext(), "New CartSize:" +ct.getCart().getCartsize(),Toast.LENGTH_LONG).show();		
							
					}else{
						
				    Toast.makeText(getApplicationContext(), "Products"+(index+1)+"Already Added",Toast.LENGTH_LONG ).show();
						
						 
						
						
						
					   }
							
				}	
					
		});
        	               
        	 la.addView(btn1);
        	 
        	 layout.addView(la);
        	   
        	   
          }
         
         btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				Intent in = new Intent(getBaseContext(),Screen2.class);
				 startActivity(in);
				
				
				
			}
		});
        
    }
}



main.xml

HTML
<linearlayout 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"
    tools:context=".MainActivity" 
    android:orientation="vertical"
    
    >
    
    <textview>
        android:layout_width="wrap_removed"
        android:layout_height="wrap_removed"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textColor="#898989"
        android:textStyle="bold"
        android:textSize="18sp"
        android:text="GreenBerry" />
    
    <linearlayout>
        android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:orientation="vertical"
	    android:id="@+id/linearMain"
	    >
	    
     </linearlayout>   
    <Button android:id="@+id/second"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Checkout"
	/>
Posted
Updated 31-Jul-15 22:23pm
v2
Comments
RohithNampally 31-Jul-15 17:49pm    
I am Getting error in FATAL EXCEPTION: main? Android

Please help me out with the Soultion
Richard MacCutchan 1-Aug-15 4:25am    
Where, what error? Please use your debugger to collect the information that will help identify where the error occurred, and what type of error it is.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900