Advertisement
Guest User

HomeActivity

a guest
Nov 25th, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.51 KB | None | 0 0
  1. package com.example.gui_shopping_list;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import android.app.Activity;
  6. import android.os.Bundle;
  7. import android.view.Menu;
  8. import android.view.MenuItem;
  9. import android.widget.*;
  10. import android.widget.AdapterView.OnItemClickListener;
  11. import android.view.View;
  12. import android.content.Intent;
  13.  
  14. public class HomeActivity extends Activity  implements View.OnClickListener
  15. {
  16.     public  final static String PAR_KEY = "com.example.gui_shopping_list.par";
  17.     private Button viewList;
  18.     private ListView listView1;
  19.     public ArrayList<Product> arrayList;
  20.     @Override
  21.     protected void onCreate(Bundle savedInstanceState)
  22.     {
  23.         super.onCreate(savedInstanceState);
  24.         setContentView(R.layout.home);
  25.        
  26.         Bundle intentExtras = getIntent().getExtras();
  27.         if (intentExtras == null)
  28.         {
  29.             return;
  30.         }
  31.         String name = intentExtras.getString("name");
  32.         String budget = intentExtras.getString("budget");
  33.         //String email = intentExtras.getString("email");
  34.        
  35.         TextView nameText = (TextView) findViewById(R.id.nameText);
  36.         nameText.setText("Feel free to add items to the basket, " + name + "!");
  37.         TextView budgetText = (TextView) findViewById(R.id.budgetText);
  38.         budgetText.setText("Budget: €" + budget);
  39.         //TextView emailText = (TextView) findViewById(R.id.emailText);
  40.         //emailText.setText(email);
  41.        
  42.         viewList=(Button)findViewById(R.id.viewList);
  43.         viewList.setOnClickListener(this);
  44.        
  45.         final Product data[] = new Product[]
  46.         {
  47.             new Product("Bread","Brennans", "1.99"),
  48.             new Product("Milk","Avonmore", "1.49"),
  49.             new Product("Cheese","Dubliner", "2.99"),
  50.             new Product("Eggs","Free Range", "1.79"),
  51.             new Product("Ham","Dennys", "1.29")
  52.         };
  53.            
  54.         CustomAdapter adapter = new CustomAdapter(this,
  55.                 R.layout.listview_item_row, data);
  56.        
  57.         listView1 = (ListView)findViewById(R.id.listView1);
  58.        
  59.         listView1.setAdapter(adapter);
  60.    
  61.         listView1.setOnItemClickListener(new OnItemClickListener()
  62.         {
  63.             public void onItemClick(AdapterView<?> parent, View view, int position,
  64.                     long id)
  65.             {
  66.                 Toast.makeText(getApplicationContext(),"Item added to list", Toast.LENGTH_LONG).show();
  67.                
  68.                 arrayList.add(data[position]);
  69.                
  70.                 //Intent listIntent = new Intent(HomeActivity.this, ListActivity.class);
  71.                 //listIntent.putExtra(data.get(position), data.toString());
  72.                 Intent mIntent = new Intent(HomeActivity.this,ListActivity.class);
  73.                 Bundle mBundle = new Bundle();
  74.                 mBundle.putParcelableArrayList(PAR_KEY, arrayList);
  75.                 mIntent.putExtras(mBundle);
  76.             }
  77.          });
  78.        
  79.     }
  80.  
  81.     @Override
  82.     public boolean onCreateOptionsMenu(Menu menu)
  83.     {
  84.         // Inflate the menu; this adds items to the action bar if it is present.
  85.         getMenuInflater().inflate(R.menu.main, menu);
  86.         return true;
  87.     }
  88.  
  89.     @Override
  90.     public boolean onOptionsItemSelected(MenuItem item)
  91.     {
  92.         // Handle action bar item clicks here. The action bar will
  93.         // automatically handle clicks on the Home/Up button, so long
  94.         // as you specify a parent activity in AndroidManifest.xml.
  95.         int id = item.getItemId();
  96.         if (id == R.id.action_settings)
  97.         {
  98.             return true;
  99.         }
  100.         return super.onOptionsItemSelected(item);
  101.     }
  102.  
  103.     @Override
  104.     public void onClick(View v)
  105.     {
  106.         // TODO Auto-generated method stub
  107.         Intent intent = new Intent(HomeActivity.this, ListActivity.class);
  108.         startActivity(intent);
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement