2

I'm trying to print a string from ArrayList in my another Activity using ListView. I passed a key and values in my intent variable. Using this.

MainActivity

ArrayList<String> arrayList = new ArrayList<String>();
Intent intent = new Intent(getApplicationContext(), ViewCart.class);

mealOneIncrement++;
quantityOne.setText(Integer.toString(mealOneIncrement));
mealOneCost = mealOneIncrement * mealOnePrice;
price.setText(Double.toString(mealOneCost));

String mealOneSummayName = name.toString();
String mealOneSummaryDescription = description.toString();
String mealOneSummaryQuantity = String.valueOf(mealOneIncrement);
String mealOneSummaryCost = String.valueOf(mealOneCost);

arrayList.add(mealOneSummayName);
arrayList.add(mealOneSummaryDescription);
arrayList.add(mealOneSummaryQuantity);
arrayList.add(mealOneSummaryCost);

intent.putExtra("data", arrayList);

As you can see here I passed arrayList variable as value for able to passed this from another Activity.

SecondActivity

ListView listView = (ListView) findViewById(R.id.listView);

String[] mealSummary = {};
ArrayAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, mealSummary);
Bundle bundle = getIntent().getExtras();

String data = bundle.getString("data");
listView.setAdapter(adapter);

I'm little bit confuse in this part String data = bundle.getString("data"); I'm not sure if I'm passing a correct variable to my ListView so I can able to show the arrayList variable in my ListView. Am I doing it wrong? Any help would appreciated :)

Francisunoxx
  • 1,440
  • 3
  • 22
  • 45

3 Answers3

0

There are some problems with your code:

intent.putExtra("data", arrayList);

you are putting inside the bundle an arrayList of String, but then

String data = bundle.getString("data");

you are getting just a String. You need to use bundle.getStringArrayList("data").

ArrayList<String> data = bundle.getStringArrayList("data")

Also (not related to your problem), I recommend to use

Intent intent = new Intent(this, ViewCart.class);

instead of

Intent intent = new Intent(getApplicationContext(), ViewCart.class);
Matias Elorriaga
  • 8,880
  • 5
  • 40
  • 58
  • What's the difference of `this` and `getApplicationContext()`. Also I tried `ArrayList data = bundle.getStringArrayList("data")` but in my `listView.setAdapter(adapter);`. `data` variable is never use and it prints nothing on my listView. – Francisunoxx Aug 30 '16 at 18:08
  • @Francisunoxx http://stackoverflow.com/questions/7298731/when-to-call-activity-context-or-application-context. this refers to Activity Context since Activity extends Context https://developer.android.com/reference/android/app/Activity.html. You need a data set for the adapter – Raghunandan Aug 30 '16 at 18:10
  • you need to use data in the adapter, otherwise the adapter is empty. – Matias Elorriaga Aug 30 '16 at 18:11
  • @MatiasElorriaga I casted the `data` variable. `listView.setAdapter((ListAdapter) data);` but the file automatically close. – Francisunoxx Aug 30 '16 at 18:34
  • if you apply the code in my response, then data is an ArrayList.. you cannot cast it to ListAdapter.. you need to create an adapter USING that data. `mealSummary = (String[]) data.toArray();` `ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, mealSummary);` – Matias Elorriaga Aug 30 '16 at 18:36
0

Pass Arraylist as putStringArrayListExtra,

    Intent intent = new Intent(this, ViewCart.class);
    intent.putStringArrayListExtra("data", arrayList);
    startActivity(intent);

Get your ArrayList using getStringArrayListExtra,

    arrayList = getIntent().getStringArrayListExtra("data");
Mujammil Ahamed
  • 1,454
  • 4
  • 24
  • 50
0

You could modify this a bit.

In your secondActivity get the data like

ArrayList<String> data = getIntent().getSerializableExtra("data");

Now you got your data in an arrayList. If you wish to add this data to mealSummary then use,

mealSummary = (String[]) data.toArray();

Don't forget to call adapter.notifyDataSetChanged()

Ashik Vetrivelu
  • 1,021
  • 1
  • 9
  • 24