Passing data to another activity - java

So I'm trying to pass some data from one activity from another and I have some difficulties doing that.
This is the code:
private TextView createNewTextView (String text){
final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final TextView newTextView = new TextView(this);
ArrayList<String> players = new ArrayList<String>();
Intent zacniIgro = getIntent();
newTextView.setLayoutParams(lparams);
newTextView.setText(text);
players.add(text);
zacniIgro.putStringArrayListExtra("players", players);
return newTextView;
}
public void zacniIgro (View v){
Intent zacniIgro = new Intent (getApplicationContext(), Igra.class);
startActivity(zacniIgro);
}
How do I now get the data in the new activity? I tried this but it doesn't work
ArrayList<String> players = data.getStringArrayListExtra("players");
Any ideas how else I could do this?
Retrieving list:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_igra);
ArrayList<String> players = data.getStringArrayListExtra("players");
}
It red underlines 'data' so I'm pretty sure there's something wrong with 'data'?

The problem is that you're creating a new intent when you start your new activity. Try this :
ArrayList<String> players = new ArrayList<String>(); //declare it outside of the function
private TextView createNewTextView (String text){
final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final TextView newTextView = new TextView(this);
newTextView.setLayoutParams(lparams);
newTextView.setText(text);
players.add(text);
return newTextView;
}
public void zacniIgro (View v){
Intent zacniIgro = new Intent (getApplicationContext(), Igra.class);
zacniIgro.putStringArrayListExtra("players", players);
startActivity(zacniIgro);
}
On the other activity :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_igra);
Intent data = getIntent();
ArrayList<String> players = data.getStringArrayListExtra("players");
}

Related

How to get the arraylist out android studio

I am unable to get my ArrayList out when it's on a different page. I would like to get the ArrayList out Android Studio.
This is CartActivity class:
public class CartActivity extends AppCompatActivity {
private ArrayList<CartItem> cartList;
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
listView = (ListView) findViewById(R.id.listView);
CartAdapter adapter = null;
cartList = new ArrayList<>();
adapter = new CartAdapter(this, R.layout.adapter_cart_item, cartList);
listView.setAdapter(adapter);
}
}
This is ProductActivity class. It's where the data is being stored into the ArrayList:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product);
myDB = new DatabaseHelper(ProductActivity.this);
textViewPrice = (TextView) findViewById(R.id.textViewPrice);
textViewInfo = (TextView) findViewById(R.id.textViewInfo);
textViewName = (TextView) findViewById(R.id.textViewName);
ivProduct = (ImageView) findViewById(R.id.ivProduct);
btnAddToCart = (Button) findViewById(R.id.btnAddToCart);
spinnerQuantity = (Spinner) findViewById(R.id.spinnerQuantity);
Intent in = getIntent();
Bundle b = in.getExtras();
userID = b.getString("userID");
productID = b.getInt("productID");
Cursor results = myDB.checkProduct();
if (results.getCount() == 0) {
Toast.makeText(getApplicationContext(), "Error: no data found!",
Toast.LENGTH_SHORT).show();
return;
} else {
StringBuffer buffer = new StringBuffer();
while (results.moveToNext()) {
id = results.getInt(0);
name = results.getString(1);
price = results.getString(2);
info = results.getString(4);
quantity = Integer.parseInt(results.getString(5));
image = results.getBlob(6);
if (id == productID) {
textViewName.setText(name);
textViewInfo.setText(info);
textViewPrice.setText("S$" + price);
int[] quantityValues = new int[quantity + 1];
int counter = 0;
for (int i = 0; i < quantityValues.length; i++) {
quantityValues[i] = counter;
counter++;
quantityList.add(Integer.toString(quantityValues[i]));
}
Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
ivProduct.setImageBitmap(bitmap);
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, quantityList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerQuantity.setAdapter(adapter);
}
}
}
}
public void addCart(View v) {
cartList.add(new CartItem(name, price,spinnerQuantity.getSelectedItem().toString(), image, id));
Intent productListIntent = new Intent(this, CartActivity.class);
startActivity(productListIntent);
}
You should impliment Serializable in CartItem class and then pass from ProductActivity to CartActivity . Like this :
ProductActivity class.
Intent productListIntent= new Intent(this, CartActivity .class);
productListIntent.putExtra("cartlist", ArrayList<CartItem>mcartItem);
startActivity(productListIntent);
and in CartActivity class
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
ArrayList<CartItem> mycart= new ArrayList<CartItem>();
mycart= (ArrayList<CartItem>)getIntent().getSerializableExtra("cartlist");
listView = (ListView) findViewById(R.id.listView);
CartAdapter adapter = null;
adapter = new CartAdapter(this, R.layout.adapter_cart_item, mycart);
listView.setAdapter(adapter);
}
It's not the best practice, but you can make your arrayList
public static ArrayList list;
Then you can access it from ather activity using {class_name}.list
You have to pass your list of your data via intent(as Intent is a message passing Object) From ! activity to another activity. For that you have to make that class Parcable or Serializable . How to make a class Parcable in andriod studio read this answer. Then put thah class into the intent that you are passing to startActivity() method like
intent.putParcelableArrayListExtra("key",your_list);
or
intent.putExtra("key",your_class)// incase of single object
then get it to the onCreate() method of second activity like
getIntent().getParcelableArrayListExtra("your_key")
or
getIntent().getParcelableExtra()// incase of single object
Hope it will help your

Android studio pass ListView to another activity

I am trying to pass an array list to another activity but it seems that is not enough. I searched all day to pass the array list with "intent" but with no success. I wrote a code for learning purposes. How to pass the data and show the Arraylist in a second activity?
The action button is btn_save. If you want further details let me know.
The code is in
MainActivity (first activity):
public class MainActivity extends AppCompatActivity {
ArrayList<String> addArray = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editTextOne = (EditText) findViewById(R.id.editTextOne);
final EditText editTextTwo = (EditText) findViewById(R.id.editTextTwo);
Button btn_showText = (Button) findViewById(R.id.buttonShow);
final TextView textView = (TextView) findViewById(R.id.textResults);
Button btn_refresh = (Button) findViewById(R.id.btn_refresh);
Button btn_close = (Button) findViewById(R.id.btn_close);
Button btn_save = (Button) findViewById(R.id.btn_save);
final ListView showMe = (ListView) findViewById(R.id.list_items);
btn_showText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String text = editTextOne.getText().toString();
String textTwo = editTextTwo.getText().toString();
if (text.length() == 0 || textTwo.length() == 0){
textView.setText("You must enter Name & Surname");
}else {
textView.setText(Html.fromHtml(
"<font color=\"red\">"+ text + "</font> " +
"<font color=\"blue\"><b>" + textTwo + " </b></font>"));
}
}
});
btn_refresh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
startActivity(getIntent());
}
});
btn_close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
System.exit(0);
}
});
btn_save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Context context = getApplicationContext();
// CharSequence mytext = "Hahahaha";
// int duration = Toast.LENGTH_SHORT;
// Toast toast = Toast.makeText(context,mytext,duration);
// toast.show();
String text = editTextOne.getText().toString();
String textTwo = editTextTwo.getText().toString();
String getInput = text + textTwo;
if (addArray.contains(getInput)){
Toast.makeText(getBaseContext(), "Item already Added!", Toast.LENGTH_LONG).show();
}
else if (getInput == null || getInput.trim().equals("")){
Toast.makeText(getBaseContext(), "Input field is empty", Toast.LENGTH_LONG).show();
}
else{
addArray.add(getInput);
ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, addArray);
showMe.setAdapter(adapter);
Intent intent = new Intent(MainActivity.this, ListOfNames.class);
((EditText)findViewById(R.id.editTextOne)).setText(" ");
((EditText)findViewById(R.id.editTextTwo)).setText(" ");
}
}
});
}
public void onButtonClick(View v){
if (v.getId() == R.id.btn_list){
Intent i = new Intent(MainActivity.this, ListOfNames.class);
startActivity(i);
}
}
}
ListOfNames second activity: (almost empty)
public class ListOfNames extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_screen);
}
}
If you are trying to pass a arraylist of string then it will be easy. Just pass arraylist with intent like this:
ArrayList<String> list = new ArrayList<String>();
Intent intent = new Intent(ActivityOne.this, ActivityTwo.class);
intent.putStringArrayListExtra("key", list);
startActivity(intent);
And receive it in ActivityTwo like this:
ArrayList<String> list = getIntent().getStringArrayListExtra("key");
I found a temporary solution for this (The app is not broke). The only problem is the arraylist didn't increased. It contains and show only the last value.
Main Activity:
...
else{
// addArray.add(getInput);
// ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, addArray);
// showMe.setAdapter(adapter);
Intent intent = new Intent(MainActivity.this, ListOfNames.class);
intent.putExtra("data", getInput);
startActivity(intent);
// ((EditText)findViewById(R.id.editTextOne)).setText(" ");
// ((EditText)findViewById(R.id.editTextTwo)).setText(" ");
}
...
ListOfNames (Second Activity):
public class ListOfNames extends Activity {
ArrayList<String> addArrayT = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_screen);
Bundle bundle = getIntent().getExtras();
String data = bundle.getString("data");
Button btn_more = (Button) findViewById(R.id.btn_more);
ListView showMe = (ListView) findViewById(R.id.list_items);
addArrayT.add(data);
ArrayAdapter<String> adapterT = new ArrayAdapter<>(ListOfNames.this, android.R.layout.simple_list_item_1, addArrayT);
showMe.setAdapter(adapterT);
}
public void onClickMore(View v){
if (v.getId() == R.id.btn_more){
Intent i = new Intent(ListOfNames.this, MainActivity.class);
startActivity(i);
}
}
}

How to send an ArrayList<String> to Another Activity and display there?

How to send an ArrayList to Another Activity and display there ?
I Want to get data from another activity and send it to another activity and get there printed. I am only able to pass single string but not an array of strings
Code On Java File,
Main Activity:
EditText et1 , et2 , et3 , et4 ;
public final static String MESSAGE_KEY = "com.example.prabhav.myapplication.message";
ArrayList<String> ar , tr = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_on);
}
public void sm (View v)
{
et1= (EditText) findViewById(R.id.name);
String msg1 = et1.getText().toString();
et2= (EditText) findViewById(R.id.dob);
String msg2 = et2.getText().toString();
et3= (EditText) findViewById(R.id.emailinput);
String msg3 = et3.getText().toString();
et4= (EditText) findViewById(R.id.clgi);
String msg4 = et4.getText().toString();
ar.add(msg1);
ar.add(msg2);
ar.add(msg3);
ar.add(msg4);
tr=ar;
Intent intent = new Intent(this,SecAct.class);
intent.putExtra("myarray",tr);
startActivity(intent);
}
Another Activity:
Spinner s;
public final static String MESSAGE_KEY = "com.example.prabhav.myapplication.message";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent= getIntent();
ArrayList<String> ls = (ArrayList<String>) getIntent().getSerializableExtra("myarray");
ArrayAdapter<String> adptr=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ls);
s= (Spinner) findViewById(R.id.sp);
s.setAdapter(adptr);
// setContentView(R.layout.sec_lay);
}
Once try as follows
MainActivity
Intent i = new Intent(this,SecAct.class);
i.putStringArrayListExtra("myarray", tr);
startActivity(i);
2nd Activity
ArrayList<String> list=(ArrayList<String>)getIntent().getStringArrayListExtra("myarray");
//use the list as you wish
Hope this will helps you.
try sending the arrayList using putParcelableArrayList like this:
Intent intent = new Intent(this,SecAct.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("myArray", (ArrayList<? extends android.os.Parcelable>) tr);
intent.putExtras(bundle);
startActivity(intent);
And you can get the arrayList in the second activity like this:
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
tr= bundle.getParcelableArrayList("myArray");}
i hope this will help

pass two strings from one activity to another

I am working on a tic tac toe application for android. In the Two player section, I've created an activity which will ask the two players to enter their names. For that I've used two EditTexts. The problem is That my app force closes while starting the next activity. Here is my code:
//Activity 1:
EditText player1field,player2field;
Button startbutton;
Intent startbuttonintent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_options);
setupActionBar();
player1field = (EditText) findViewById(R.id.player1field);
player2field = (EditText) findViewById(R.id.player2field);
startbuttonintent = new Intent(this, Activity2.class);
startbutton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
String player1name = player1field.getText().toString();
String player2name = player2field.getText().toString();
startbuttonintent.putExtra("PLAYER1NAME",player1name);
startbuttonintent.putExtra("PLAYER2NAME",player2name);
startActivity(startbuttonintent);
}
});
}
this is activity 2
//Activity2
Intent startbuttonintent = getIntent();
TextView p1name,p2name;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_3m);
String player1name = startbuttonintent.getStringExtra("PLAYER1NAME");
String player2name = startbuttonintent.getStringExtra("PLAYER2NAME");
p1name = (TextView) findViewById(R.id.p1name);
p2name = (TextView) findViewById(R.id.p2name);
p1name.setText(player1name);
p2name.setText(player2name);
}
This code is not giving me any errors but my app force closes when I run it.
Please help me.
Thanks in advance.
Try this:
Activity 1:
EditText player1field,player2field;
Button startbutton;
Intent startbuttonintent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_options);
setupActionBar();
player1field = (EditText) findViewById(R.id.player1field);
player2field = (EditText) findViewById(R.id.player2field);
startbutton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
String player1name = player1field.getText().toString();
String player2name = player2field.getText().toString();
startbuttonintent = new Intent(this, Activity2.class);
startbuttonintent.putExtra("PLAYER1NAME",player1name);
startbuttonintent.putExtra("PLAYER2NAME",player2name);
startActivity(startbuttonintent);
}
});
}
You have call this method before onclick which is causing error.
And fetch the intent text like this:
//Activity2
TextView p1name,p2name;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_3m);
Bundle extras = getIntent().getExtras();
String player1name = extras.getString("PLAYER1NAME");
String player2name = extras.getString("PLAYER2NAME");
p1name = (TextView) findViewById(R.id.p1name);
p2name = (TextView) findViewById(R.id.p2name);
p1name.setText(player1name);
p2name.setText(player2name);
}
String intArray[] = {"one","two"};
Intent in = new Intent(this, B.class);
in.putExtra("my_array", intArray);
startActivity(in);
For Reading :
Bundle extras = getIntent().getExtras();
String[] arrayInB = extras.getStringArray("my_array");
Take yout getIntent method inside onCreate method
//Activity2
TextView p1name,p2name;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_3m);
Intent startbuttonintent = getIntent();
String player1name = startbuttonintent.getStringExtra("PLAYER1NAME");
String player2name = startbuttonintent.getStringExtra("PLAYER2NAME");
p1name = (TextView) findViewById(R.id.p1name);
p2name = (TextView) findViewById(R.id.p2name);
p1name.setText(player1name);
p2name.setText(player2name);
}
Move Intent startbuttonintent = getIntent(); to inside your onCreate() method
getIntent() is a method in Activity, so you can't call it in a static class level initialization. You must call it only after your Activity object is ready, like when in the onCreate() method
#Chinmay Dabke Please get the id of the button "startbutton"
by using
startbutton = (Button)findviewbyId(R.id.startbutton);
Try This....in Activity1
Bundle bundle = new Bundle();
bundle .putString("PLAYER1NAME",player1name);
bundle .putString("PLAYER2NAME",player2name);
intent1.putExtras(bundle);
in Activity2
Bundle b = getArguments();
String player1name = b.getString("PLAYER1NAME");
String player2name = b.getString("PLAYER2NAME");

Unknown Error - Bundling String Array

I keep getting an error for this application every time I press the menu button --> History to start the History.java class. I'm fairly certain it has to do with the Bundle method for sending the two arrays from the TipBookActivity.java class to the History.java class.
Below is the TipBookActivity code:
public class TipBookActivity extends Activity {
/** Called when the activity is first created. */
TextView textTip,textHour,textWage;
EditText editHour,editTip;
float wage;
int precision = 100;
String sTip,sHour;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textTip = (TextView) findViewById(R.id.tvTip);
textHour = (TextView) findViewById(R.id.tvHour);
textWage = (TextView) findViewById(R.id.tvWage);
editTip = (EditText) findViewById(R.id.etTip);
editHour = (EditText) findViewById(R.id.etHour);
Button bSubmit = (Button) findViewById(R.id.bSubmit);
final Bundle bTip = new Bundle();
final Bundle bHour = new Bundle();
final ArrayList<String> tipList = new ArrayList<String>();
final ArrayList<String> hourList = new ArrayList<String>();
bSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
textHour.setText(editHour.getText().toString());
textTip.setText(editTip.getText().toString());
wage = Float.parseFloat(textTip.getText().toString()) / Float.parseFloat(textHour.getText().toString());
String tip = String.format("$%.2f",wage);
textWage.setText(String.valueOf(tip) + " an hour");
textHour.setText(editHour.getText() + " Hour(s)");
textTip.setText("$" + editTip.getText());
bTip.putStringArray(sTip,new String[] {editTip.getText().toString()});
bHour.putStringArray(sHour,new String[] {editHour.getText().toString()});
tipList.addAll(Arrays.asList(sTip));
hourList.addAll(Arrays.asList(sHour));
Intent i = new Intent(TipBookActivity.this,History.class);
i.putExtras(bTip);
i.putExtras(bHour);
}
});
}
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater mMain = getMenuInflater();
mMain.inflate(R.menu.main_menu,menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.menuHistory:
startActivity(new Intent("com.smarticle.tipbook.HISTORY"));
return true;
case R.id.menuClear:
//set up next tutorials
Toast display = Toast.makeText(this, "Clear History feature coming soon.", Toast.LENGTH_SHORT);
display.show();
return true;
}
return false;
}
}
The History class code:
public class History extends Activity{
private ListView mainListViewTip;
private ListView mainListViewHour;
private ArrayAdapter<String>listAdapterTip;
private ArrayAdapter<String>listAdapterHour;
String sTip,sHour;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.history_main);
Bundle bTip = this.getIntent().getExtras();
Bundle bHour = this.getIntent().getExtras();
String[] array1 = bTip.getStringArray(sTip);
String[] array2 = bHour.getStringArray(sHour);
ListView mainListViewTip = (ListView) findViewById(R.id.mainListViewTip);
ListView mainListViewHour = (ListView) findViewById(R.id.mainListViewHour);
ArrayList<String> tipList = new ArrayList<String>();
ArrayList<String> hourList = new ArrayList<String>();
tipList.addAll(Arrays.asList(sTip));
hourList.addAll(Arrays.asList(sHour));
listAdapterTip = new ArrayAdapter<String>(this,R.layout.simplerow,tipList);
listAdapterHour = new ArrayAdapter<String>(this,R.layout.simplerow,hourList);
mainListViewTip.setAdapter(listAdapterTip);
mainListViewHour.setAdapter(listAdapterHour);
}
}
Any help on identifying the error cause would be greatly appreciated. The code works (in theory, I think), it just won't work in practice. The general idea is to input two numbers into EditText fields, save them as strings, display them as TextViews, set them as an ArrayList, then bundle and send them to the other class to display in a ListView.
You are not initializing sTip and sHour Strings in both Activities. so initializing sTip and Shour Strings with any constant value as in both Activities:
String sTip="sTip",sHour="sHour";
and from TipBookActivity you are not passing intent to startActivity so first declare Intent i globally then start your Activity as:
TextView textTip,textHour,textWage;
EditText editHour,editTip;
float wage;
int precision = 100;
String sTip,sHour;
Intent i; // declare here
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textTip = (TextView) findViewById(R.id.tvTip);
textHour = (TextView) findViewById(R.id.tvHour);
textWage = (TextView) findViewById(R.id.tvWage);
editTip = (EditText) findViewById(R.id.etTip);
editHour = (EditText) findViewById(R.id.etHour);
Button bSubmit = (Button) findViewById(R.id.bSubmit);
final Bundle bTip = new Bundle();
final Bundle bHour = new Bundle();
final ArrayList<String> tipList = new ArrayList<String>();
final ArrayList<String> hourList = new ArrayList<String>();
bSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
textHour.setText(editHour.getText().toString());
textTip.setText(editTip.getText().toString());
wage = Float.parseFloat(textTip.getText().toString()) / Float.parseFloat(textHour.getText().toString());
String tip = String.format("$%.2f",wage);
textWage.setText(String.valueOf(tip) + " an hour");
textHour.setText(editHour.getText() + " Hour(s)");
textTip.setText("$" + editTip.getText());
bTip.putStringArray(sTip,new String[] {editTip.getText().toString()});
bHour.putStringArray(sHour,new String[] {editHour.getText().toString()});
tipList.addAll(Arrays.asList(sTip));
hourList.addAll(Arrays.asList(sHour));
i = new Intent(TipBookActivity.this,History.class);
i.putExtras(bTip);
i.putExtras(bHour);
}
});
}
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.menuHistory:
startActivity(i)); // start Activity here by passing intent
return true;
Approach you are following is completely wrong, you can put one Bundle at a time to the intent, and you are putting two bundles, sTip, and sHours.
Second Bundle sHours will override the first one, and I think its main cause of the null pointer exception, instead you should put all the values(in your case two String Arrays) to a single bundle. and put that bundle to the Intent.
Do as Follows:
public class TipBookActivity extends Activity {
/** Called when the activity is first created. */
TextView textTip,textHour,textWage;
EditText editHour,editTip;
float wage;
int precision = 100;
String sTip="sTip";
String sHour="sHour";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textTip = (TextView) findViewById(R.id.tvTip);
textHour = (TextView) findViewById(R.id.tvHour);
textWage = (TextView) findViewById(R.id.tvWage);
editTip = (EditText) findViewById(R.id.etTip);
editHour = (EditText) findViewById(R.id.etHour);
Button bSubmit = (Button) findViewById(R.id.bSubmit);
final Bundle bundle= new Bundle();
final ArrayList<String> tipList = new ArrayList<String>();
final ArrayList<String> hourList = new ArrayList<String>();
bSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
textHour.setText(editHour.getText().toString());
textTip.setText(editTip.getText().toString());
wage = Float.parseFloat(textTip.getText().toString()) / Float.parseFloat(textHour.getText().toString());
String tip = String.format("$%.2f",wage);
textWage.setText(String.valueOf(tip) + " an hour");
textHour.setText(editHour.getText() + " Hour(s)");
textTip.setText("$" + editTip.getText());
bundle.putStringArray(sTip,new String[] {editTip.getText().toString()});
bundle.putStringArray(sHour,new String[] {editHour.getText().toString()});
tipList.addAll(Arrays.asList(sTip));
hourList.addAll(Arrays.asList(sHour));
Intent i = new Intent(TipBookActivity.this,History.class);
i.putExtras(bundle);
}
});
}
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater mMain = getMenuInflater();
mMain.inflate(R.menu.main_menu,menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.menuHistory:
startActivity(new Intent("com.smarticle.tipbook.HISTORY"));
return true;
case R.id.menuClear:
//set up next tutorials
Toast display = Toast.makeText(this, "Clear History feature coming soon.", Toast.LENGTH_SHORT);
display.show();
return true;
}
return false;
}
}
and in History Activity:
public class History extends Activity{
private ListView mainListViewTip;
private ListView mainListViewHour;
private ArrayAdapter<String>listAdapterTip;
private ArrayAdapter<String>listAdapterHour;
String sTip="sTip";
String sHour="sHour";
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.history_main);
Bundle bundle= this.getIntent().getExtras();
String[] array1 = bundle.getStringArray(sTip);
String[] array2 = bundle.getStringArray(sHour);
ListView mainListViewTip = (ListView) findViewById(R.id.mainListViewTip);
ListView mainListViewHour = (ListView) findViewById(R.id.mainListViewHour);
ArrayList<String> tipList = new ArrayList<String>();
ArrayList<String> hourList = new ArrayList<String>();
tipList.addAll(Arrays.asList(sTip));
hourList.addAll(Arrays.asList(sHour));
listAdapterTip = new ArrayAdapter<String>(this,R.layout.simplerow,tipList);
listAdapterHour = new ArrayAdapter<String>(this,R.layout.simplerow,hourList);
mainListViewTip.setAdapter(listAdapterTip);
mainListViewHour.setAdapter(listAdapterHour);
}
}

Categories