简体   繁体   中英

How can I use the same Activity for different content in Android?

Consider I am using the linear layout for my app

 <TextView
    android:text="product 1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:textColor="@android:color/white"
    android:textSize="54sp"
    android:background="#009611" />

<TextView
    android:text="product 2"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:textColor="@android:color/white"
    android:textSize="54sp"
    android:background="#123431" />

<TextView
    android:text="product 3"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:textColor="@android:color/white"
    android:textSize="54sp"
    android:background="#009688" />
</LinearLayout>

My output is:

akash preet 样本输出

when I select product1 it should open product1_des Activity
when I select product2 it should open product2_des Activity
when I select product3 it should open product3_des Activity
and goes on..

And I have 1000 product should I create 1000 Activity? How can I use the product_des Activity (ie single Activity) to support all my 1000 product with different description that is related to that product? Please help me.

And I have 1000 product should I create 1000 Activity? How can I use the product_des Activity (ie single Activity) to support all my 1000 product with different description that is related to that product? Please help me.

NO!!!

You can create an Activity called for example ProductDetail but only one, and then everywhere you want to show a product detail you can use putExtras()

Intent intent = new Intent(this, ProductDetail.class);
intent.putExtra("productName", ProductName);
startActivity(intent);

And in ProductDetail you can get the name of the product with a Bundle

Bundle b = new Bundle();
b = getIntent().getExtras();
String name = b.getString("productName");

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