简体   繁体   中英

Pass Data from one activity to other activity and display on Listview

I Have 2 activities 1st one is Data activity there are some Spinners I would like to take data from that spinner and add them to the History activity and display to ListView . I don't want to open the second activity on button click. Bellow is what I have done till now.

I tried to find the similar questions but none was helpful.

Data Activity :

 public class Data extends AppCompatActivity {



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_data);


    final Spinner handSpiner = (Spinner) findViewById(R.id.spinner_Arm_used);

    final ArrayAdapter<String> handAdapter = new ArrayAdapter<String>(Data.this,
            android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.hand));
    handAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    handSpiner.setAdapter(handAdapter);

    final Spinner weightSpiner = (Spinner) findViewById(R.id.spinner_weight);

    final ArrayAdapter<String> weightAdapter = new ArrayAdapter<String>(Data.this,
            android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.weight));
    weightAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    weightSpiner.setAdapter(weightAdapter);



    final Spinner heightSpiner = (Spinner) findViewById(R.id.spinner_height);

    final ArrayAdapter<String> heightAdapter = new ArrayAdapter<String>(Data.this,
            android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.height));
    heightAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    heightSpiner.setAdapter(heightAdapter);


    final Spinner bloodTypeSpiner = (Spinner) findViewById(R.id.spinner_blood_type);

    final ArrayAdapter<String> bloodTypeAdapter = new ArrayAdapter<String>(Data.this,
            android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.bloodType));
    bloodTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    bloodTypeSpiner.setAdapter(bloodTypeAdapter);


    final Spinner smallPressureSpiner = (Spinner) findViewById(R.id.spinner_small_pressure);

    final ArrayAdapter<String> smallPressureAdapter = new ArrayAdapter<String>(Data.this,
            android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.smallPressure));
    smallPressureAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    smallPressureSpiner.setAdapter(smallPressureAdapter);


    final Spinner bigPressureSpiner = (Spinner) findViewById(R.id.spinner_big_pressure);

    final ArrayAdapter<String> bigPressureAdapter = new ArrayAdapter<String>(Data.this,
            android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.bigPressure));
    bigPressureAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    bigPressureSpiner.setAdapter(bigPressureAdapter);
}

History Layout:

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout_editor_absoluteX="42dp"
    tools:layout_editor_absoluteY="0dp">

    <TextView
        android:id="@+id/textView14"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="21dp"
        android:layout_marginTop="91dp"
        android:text="Χέρι Αιμοδοσίας" />

    <TextView
        android:id="@+id/textView15"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/textView14"
        android:layout_below="@+id/textView14"
        android:layout_marginTop="26dp"
        android:text="Αρτηριακή Πίεση" />


    <TextView
        android:id="@+id/textView17"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/textView15"
        android:layout_below="@+id/textView15"
        android:layout_marginTop="120dp"
        android:text="Βάρος" />

    <TextView
        android:id="@+id/textView18"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/textView17"
        android:layout_below="@+id/textView17"
        android:layout_marginTop="16dp"
        android:text="Ύψος" />

    <TextView
        android:id="@+id/textView19"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/textView18"
        android:layout_below="@+id/textView18"
        android:layout_marginTop="34dp"
        android:text="Ομάδα Αίματος" />

    <Spinner
        android:id="@+id/spinner_Arm_used"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView15"
        android:layout_marginStart="11dp"
        android:layout_toEndOf="@+id/textView15" />

    <Spinner
        android:id="@+id/spinner_weight"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/textView17"
        android:layout_marginStart="14dp"
        android:layout_toEndOf="@+id/textView17" />

    <Spinner
        android:id="@+id/spinner_height"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/spinner_weight"
        android:layout_alignTop="@+id/textView18" />

    <Spinner
        android:id="@+id/spinner_blood_type"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/textView19"
        android:layout_toEndOf="@+id/textView14" />

    <Spinner
        android:id="@+id/spinner_small_pressure"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/spinner_big_pressure"
        android:layout_marginStart="16dp"
        android:layout_toEndOf="@+id/spinner_big_pressure" />

    <Spinner
        android:id="@+id/spinner_big_pressure"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/textView15"
        android:layout_centerHorizontal="true" />

    <Button
        android:id="@+id/btnaddHistory"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="11dp"
        android:layout_toEndOf="@+id/spinner_small_pressure"
        android:text="Προσθήκη"

        />


</RelativeLayout>

History Layout:

   <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ListView
        android:id="@+id/lvHistory"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />
</RelativeLayout

You can store the data in a model (POJO) class if you don't need your data to be persistent and then fetch data from the class while opening History activity. If you need your data to be persistent, you can store it in a database or can use shared preference.

You can either store the data in a database and then retrieve them as mentioned or you can either directly pass the data from one activity to the other through the Intent starting the activity like so.

Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra(variable_name, content);

and then retrieve it from second activity

 String whatEver = getIntent().getExtras().getString(variable_name);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM