-2

So, i've been trying to develop an app where u order sandwiches by yourself. So far I've managed everything with the listview, what type of sandwiches you can order etc.

Then I move onto the next activity, which is supposed to display the sandwiches the user picked, but unfortunately nothing pops up.

If you have an alternative code for the second activity im open for it.

Code1 = Where you order what sandwich:

package com.example.boo.cafestudent;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class Order extends AppCompatActivity{




  ArrayList<String> klickademackor = new ArrayList<String>();
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.orderactivity);

    String[] mackor ={"Ostsmörgås","Skinksmörgås","Ost och Skinksmörgås", "Salamismörgås","Räksmörgås",
"Rensmörgås", "Älgsmörgås", "Köttbullesmörgås"};



ListAdapter johansadapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, mackor);

    //hämtar ListView
 ListView mackorListView = (ListView) findViewById(R.id.ListViewMackor);

//Hämtar data från array stringen

   // final String[] array_mackor =    getResources().getStringArray(R.array.arraymackor);

    //skapandet av en array adapter

    //Själva adaptern till ListView
    mackorListView.setAdapter(johansadapter);


    //kopplar onClicklistener till Listvie
    mackorListView.setOnItemClickListener(
            new AdapterView.OnItemClickListener(){
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id){
                    String mackor = String.valueOf(parent.getItemAtPosition(position));
                    Toast.makeText(Order.this, mackor + " har lagts till", Toast.LENGTH_LONG).show();
                    klickademackor.add(mackor);
                }
            });

};



public void onClick (View View){
    Intent i = new Intent (Order. this, Confirmation.class);


    startActivity(i);
}}

Code2 = Supposed to show the results using a textview which ID is textview2.

package com.example.boo.cafestudent;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class Confirmation extends AppCompatActivity {

ArrayList<String> klickademackor = new ArrayList<String>();

private TextView textviewMackor;


@SuppressLint("WrongViewCast")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.confirmationactivity);

    textviewMackor = (TextView) findViewById(R.id.textView2);

    for (String index : klickademackor) {
        textviewMackor.append(index + "\n");
    }

}

public void confirmation (View view){
    Toast.makeText(this,"Din beställning är bekräftad",    Toast.LENGTH_SHORT).show();
}


}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Tom Scott
  • 49
  • 7
  • Possible duplicate of [How to pass ArrayList from one activity to another?](https://stackoverflow.com/questions/21250339/how-to-pass-arraylistcustomeobject-from-one-activity-to-another) – Murat Karagöz Dec 06 '18 at 13:25
  • But im not using any .put Extra methods, and im not getting any errors in my code? – Tom Scott Dec 06 '18 at 13:40
  • That's why it does not work in the first place. You are never sending the List to the second Activity. – Murat Karagöz Dec 06 '18 at 13:45

2 Answers2

2

In code 1 you have created an array list as follows :

ArrayList<String> klickademackor = new ArrayList<String>();

And you have add the items that are selected to this ArrayList inside the setOnItemClickListener method. Now if you want to get this array list in another activity then you need to pass this ArrayList along with the intent here :

public void onClick (View View){
Intent i = new Intent (Order. this, Confirmation.class);
i.putStringArrayListExtra("myArrayList",klickademackor);

startActivity(i);
}}

This will pass your ArrayList along with the Intent object i which you can receive from another activity using :

ArrayList arrayList<Integer> = getIntent().getStringArrayListExtra("myArrayList");

So write this code inside the 2nd activity and use this ArrayList to display in text view. I hope this helps.

arunken
  • 415
  • 3
  • 15
-1

I guess that you are trying to show a Toast using the wrong context, in this case the context from the Order activity.

In your click listener try to get the context by using the View instance:

Toast.makeText(view.getContext();, mackor + " har lagts till", Toast.LENGTH_LONG).show();
                    klickademackor.add(mackor);
Juan Ramos
  • 557
  • 4
  • 14