将数据从一个 activity 传递到另一个 activity 并显示在 Listview 上
Pass Data from one activity to other activity and display on Listview
我有 2 个活动第一个是数据 activity 有一些 Spinners
我想从中获取数据 spinner
并将它们添加到历史记录 activity并显示到 ListView
。我不想在 button
单击时打开第二个 activity。
贝娄是我到目前为止所做的。
我试图找到类似的问题,但 none 很有帮助。
数据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);
}
历史布局:
<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>
历史布局:
<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
如果您不需要持久化数据,您可以将数据存储在模型 (POJO) class 中,然后在打开历史记录时从 class 中获取数据 activity.
如果您需要持久化数据,可以将其存储在数据库中或使用共享首选项。
您可以将数据存储在数据库中,然后如前所述检索它们,或者您可以直接通过 activity 开始的 Intent 将数据从一个 activity 传递到另一个 activity .
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra(variable_name, content);
然后从第二个 activity
中检索它
String whatEver = getIntent().getExtras().getString(variable_name);
我有 2 个活动第一个是数据 activity 有一些 Spinners
我想从中获取数据 spinner
并将它们添加到历史记录 activity并显示到 ListView
。我不想在 button
单击时打开第二个 activity。
贝娄是我到目前为止所做的。
我试图找到类似的问题,但 none 很有帮助。
数据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);
}
历史布局:
<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>
历史布局:
<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
如果您不需要持久化数据,您可以将数据存储在模型 (POJO) class 中,然后在打开历史记录时从 class 中获取数据 activity. 如果您需要持久化数据,可以将其存储在数据库中或使用共享首选项。
您可以将数据存储在数据库中,然后如前所述检索它们,或者您可以直接通过 activity 开始的 Intent 将数据从一个 activity 传递到另一个 activity .
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra(variable_name, content);
然后从第二个 activity
中检索它 String whatEver = getIntent().getExtras().getString(variable_name);