Hi I´m just starting to learn how to use Android Studio. And I want to try to send the values of the choices the user makes from one activity to the other.
So when the user chooses their "mackor" = sandwiches, I want it to show up in a textview (kvittoView) where it shows all the "mackor" they have chosen (clicked on).
Right now with the code I have made so far I can only display 1 "mackor" at a time.
This all happens between activity 2 and 3.
Activity 2:
package com.example.studentcafe;
import android.content.Intent;
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 androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class SecondActivity extends AppCompatActivity {
String[] mackor_names = {
"Tonfisk Macka 30:-",
"Skagen Macka 35:-",
"Kyckling Macka 35:-",
"Curryröra Macka 30:-",
"Ost o Kalkon Macka 25:-",
"Köttbulle Macka 25:-",
"Falafel Macka 20:-"
};
ListView lv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
ArrayAdapter<String> mackor_names_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mackor_names);
lv1 = (ListView) findViewById(R.id.list_options);
lv1.setAdapter(mackor_names_adapter);
lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String item_name = mackor_names_adapter.getItem(i);
Toast.makeText(SecondActivity.this, "Du har valt " item_name, Toast.LENGTH_SHORT).show();
String TempListViewClickedValue = mackor_names[i];
Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
intent.putExtra("ListViewClickedValue", TempListViewClickedValue);
startActivity(intent);
}
});
}
public void tillbaka(View view) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
public void gavidare(View view) {
Intent intent = new Intent(this, ThirdActivity.class);
startActivity(intent);
}
}
Activity 3
package com.example.studentcafe;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.widget.TextView;
import java.util.ArrayList;
public class ThirdActivity extends AppCompatActivity {
TextView editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
editText = (TextView) findViewById(R.id.kvittoView);
String TempHolder = getIntent().getStringExtra("ListViewClickedValue");
editText.setText(TempHolder);
}
public void tillbaka2(View view) {
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
public void bestall(View view) {
Intent intent = new Intent(this, FourthActivity.class);
startActivity(intent);
}
}
CodePudding user response:
You can pass data like this(this is from SecondActivity):
Intent intent = new Intent(this, ThirdActivity.class);
intent.putExtra("EXTRA_SESSION_ID", sessionId);
startActivity(intent);
and than in ThirdActivity in onCreate method:
String sessionId = getIntent().getStringExtra("EXTRA_SESSION_ID");
Also, best practise is to have key "EXTRA_SESSION_ID" stored in public static variable so you have only one object
CodePudding user response:
1.Change this to arraylist
String[] mackor_names = {
"Tonfisk Macka 30:-",
"Skagen Macka 35:-",
"Kyckling Macka 35:-",
"Curryröra Macka 30:-",
"Ost o Kalkon Macka 25:-",
"Köttbulle Macka 25:-",
"Falafel Macka 20:-"
};
ArrayList<String> list = new ArrayList<>();
list.add("Tonfisk Macka 30:-");
list.add("Skagen Macka 35:-"); ......
2.and then use
intent.putStringArrayListExtra("test",list);
3.and to get values
Intent intent=getIntent();
ArrayList<String> test = intent.getStringArrayListExtra("test");
CodePudding user response:
//Create the bundle
Bundle bundle = new Bundle();
//Add your data from getFactualResults method to bundle
bundle.putString("VENUE_NAME", venueName);
//Add the bundle to the intent
i.putExtras(bundle);
Insead of array you can use bundle