Android开发:如何将Spinner选值添加到TextView及杂货APP代码需求
Hey there! Since you're new to Android development, I'll break this down step by step with clear, commented code that's easy to follow. Let's build this grocery order feature together!
First, let's create the layout (your app's user interface) in activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <!-- Spinner for selecting grocery items with price --> <Spinner android:id="@+id/spinner_items" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="16dp" android:prompt="@string/select_item" /> <!-- Spinner for selecting quantity --> <Spinner android:id="@+id/spinner_quantity" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="16dp" android:prompt="@string/select_quantity" /> <!-- Button to add selected item to TextView --> <Button android:id="@+id/btn_add_item" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/add_item" /> <!-- Scrollable TextView to display ordered items --> <TextView android:id="@+id/tv_order_summary" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="16dp" android:inputType="textMultiLine" android:scrollbars="vertical" android:textSize="16sp" /> </LinearLayout>
Next, here's the Java code for MainActivity.java that handles all the functionality:
import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.Spinner; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private Spinner spinnerItems, spinnerQuantity; private Button btnAddItem; private TextView tvOrderSummary; // Predefined list of grocery items with their prices private String[] groceryItems = {"Chips -5rs-", "Biscuits -20rs-", "Noodles -15rs-", "Sugar -40rs-", "Salt -10rs-"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Initialize all UI elements initViews(); // Set up the grocery items spinner setupItemsSpinner(); // Set up quantity spinner (generates 1-20 packet options) setupQuantitySpinner(); // Handle add item button clicks setupAddButtonListener(); } private void initViews() { spinnerItems = findViewById(R.id.spinner_items); spinnerQuantity = findViewById(R.id.spinner_quantity); btnAddItem = findViewById(R.id.btn_add_item); tvOrderSummary = findViewById(R.id.tv_order_summary); } private void setupItemsSpinner() { // Bind grocery items array to the spinner with a default dropdown style ArrayAdapter<String> itemsAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, groceryItems); spinnerItems.setAdapter(itemsAdapter); } private void setupQuantitySpinner() { ArrayList<String> quantityList = new ArrayList<>(); // Generate quantity options with proper pluralization for (int i = 1; i <= 20; i++) { quantityList.add(i + " packet" + (i > 1 ? "s" : "")); } // Bind quantity list to the spinner ArrayAdapter<String> quantityAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, quantityList); spinnerQuantity.setAdapter(quantityAdapter); } private void setupAddButtonListener() { btnAddItem.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Get selected values from both spinners String selectedItem = spinnerItems.getSelectedItem().toString(); String selectedQuantity = spinnerQuantity.getSelectedItem().toString(); // Format the entry to match your desired output String newEntry = "- " + selectedItem + " " + selectedQuantity + "\n"; // Append the new entry to the TextView (instead of overwriting) tvOrderSummary.append(newEntry); // Optional: Auto-scroll to the bottom of the TextView after adding an item tvOrderSummary.scrollTo(0, tvOrderSummary.getBottom()); } }); } }
A few quick tips to make this even better:
- I added pluralization for quantities (so it shows "1 packet" instead of "1 packets") to keep the output natural.
- The TextView is set to be scrollable, so users can view all their ordered items as the list grows.
- This code is Firebase-ready—once you're ready to integrate Firebase, you can add logic in the button click listener to save each order entry to Firestore or Realtime Database.
内容的提问来源于stack exchange,提问作者Vaibhav Gupta
相关产品推荐
相关产品推荐

