简体   繁体   中英

I cannot navigate to multiple activities from a single Card View Activity. I can navigate to Only 1

I am using cardview for the first time. I have created 6 cards and made them listen to user click, but only the first cardview works and navigates to the next activity, none of the other work. Even I have tried all the Click Methods.

I have tried to use OnClickListner and onClick through XML. In Java it dont give any error but I cant navigate, in XML when I write the onClick method it simply gives error "cannot find....." something like that.

     <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".landing_screen"
    android:background="@drawable/bg"
    android:orientation="vertical"
    android:padding="5dp"
    android:gravity="center"
    >
    <TextView
       android:layout_marginBottom="0dp"
        android:textSize="20sp"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Please Choose Who you are"/>
    <LinearLayout

        android:clipToPadding="false"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <android.support.v7.widget.CardView
        app:cardBackgroundColor="#"
        android:onClick="passenger"
        android:foreground="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:layout_width="140dp"
        android:layout_height="140dp"
        android:layout_margin="5dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:gravity="center">

            <ImageView
                android:padding="5dp"
                android:src="@drawable/passenger"
                android:layout_width="56dp"
                android:layout_height="64dp"
                android:background="@drawable/circlebgpurple"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:layout_marginTop="2dp"
                android:text="Passenger"
                android:textColor="#000"

                />
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/line"
                android:layout_margin="2dp"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="I am a Passenger"
                android:textColor="#000"
                android:padding="5dp"
                />

        </LinearLayout>
    </android.support.v7.widget.CardView>
        <android.support.v7.widget.CardView
            app:cardBackgroundColor="#"
            android:id="@+id/relativecard"
            android:foreground="?android:attr/selectableItemBackground"
            android:clickable="true"
            android:layout_width="140dp"
            android:layout_height="140dp"
            android:layout_margin="5dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:gravity="center">

                <ImageView
                    android:padding="5dp"
                    android:src="@drawable/boy"
                    android:layout_width="56dp"
                    android:layout_height="64dp"
                    android:background="@drawable/circlebggreen"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textStyle="bold"
                    android:layout_marginTop="2dp"
                    android:text="Relative"
                    android:textColor="#000"

                    />
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/line"
                    android:layout_margin="2dp"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="I am a Relative"
                    android:textColor="#000"
                    android:padding="5dp"
                    />
            </LinearLayout>
        </android.support.v7.widget.CardView>
    </LinearLayout>
    <LinearLayout
        android:clipToPadding="false"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <android.support.v7.widget.CardView
            app:cardBackgroundColor="#"
            android:id="@+id/drivercard"
            android:foreground="?android:attr/selectableItemBackground"
            android:clickable="true"
            android:layout_width="140dp"
            android:layout_height="140dp"
            android:layout_margin="5dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:gravity="center">

                <ImageView
                    android:padding="8dp"
                    android:src="@drawable/driver"
                    android:layout_width="56dp"
                    android:layout_height="64dp"
                    android:background="@drawable/circlebgyellow"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textStyle="bold"
                    android:layout_marginTop="2dp"
                    android:text="Driver"
                    android:textColor="#000"

                    />
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/line"
                    android:layout_margin="2dp"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="I am a Driver"
                    android:textColor="#000"
                    android:padding="5dp"
                    />

            </LinearLayout>
        </android.support.v7.widget.CardView>
        <android.support.v7.widget.CardView
            app:cardBackgroundColor="#"
            android:id="@+id/tkchecker"
            android:foreground="?android:attr/selectableItemBackground"
            android:clickable="true"
            android:layout_width="140dp"
            android:layout_height="140dp"
            android:layout_margin="5dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:gravity="center">

                <ImageView
                    android:padding="5dp"
                    android:src="@drawable/ic_ticket_checker"
                    android:layout_width="56dp"
                    android:layout_height="64dp"
                    android:background="@drawable/circlebgpink"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textStyle="bold"
                    android:layout_marginTop="2dp"
                    android:text="Ticket Checker"
                    android:textColor="#000"

                    />
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/line"
                    android:layout_margin="2dp"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="I'm a TicketChecker"
                    android:textColor="#000"
                    android:padding="5dp"
                    />

            </LinearLayout>
        </android.support.v7.widget.CardView>



    </LinearLayout>
    <LinearLayout
        android:clipToPadding="false"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <android.support.v7.widget.CardView
            app:cardBackgroundColor="#"
            android:id="@+id/subadmin"
            android:foreground="?android:attr/selectableItemBackground"
            android:clickable="true"
            android:layout_width="140dp"
            android:layout_height="140dp"
            android:layout_margin="5dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:gravity="center">

                <ImageView
                    android:padding="5dp"
                    android:src="@drawable/subadmin"
                    android:layout_width="56dp"
                    android:layout_height="64dp"
                    android:background="@drawable/circlebgyellow"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textStyle="bold"
                    android:layout_marginTop="2dp"
                    android:text="Sub-Admin"
                    android:textColor="#000"

                    />
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/line"
                    android:layout_margin="2dp"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="I am a Sub-Admin"
                    android:textColor="#000"
                    android:padding="5dp"
                    />

            </LinearLayout>
        </android.support.v7.widget.CardView>
        <android.support.v7.widget.CardView
            app:cardBackgroundColor="#"
            android:id="@+id/admin"
            android:foreground="?android:attr/selectableItemBackground"
            android:clickable="true"
            android:layout_width="140dp"
            android:layout_height="140dp"
            android:layout_margin="5dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:gravity="center">

                <ImageView
                    android:layout_width="56dp"
                    android:layout_height="64dp"
                    android:background="@drawable/circlebggreen"
                    android:padding="5dp"
                    android:src="@drawable/manager" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textStyle="bold"
                    android:layout_marginTop="5dp"
                    android:text="Admin"
                    android:textColor="#000"

                    />
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/line"
                    android:layout_margin="2dp"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="I am the Admin"
                    android:textColor="#000"
                    android:padding="2dp"
                    />

            </LinearLayout>
        </android.support.v7.widget.CardView>



    </LinearLayout>

</LinearLayout>


[![enter image description here][1]][1]

I simply want to navigate to the corrosponding activity of each CardView when Clicked.

Java Code:

public class relative extends AppCompatActivity {

    CardView relativecard;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sign_up);
        relativecard = (CardView)findViewById(R.id.relativecard);


        relativecard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent relative = new Intent(getApplicationContext(), signup.class);
                startActivity(relative);
            }
        });

    }

}

Probably you should define the onClick attribute in XML for all cards and then in your activity you should define the method whose name you have give in XML and then you should write an if-else or switch statement on the basis of the id of the card that is clicked and then whatever view is clicked, you need to match and perform action. Something like this:

public void myMethod(View v){
     switch(v.getId()){
          case R.id.cardOne:
               //do something here when card one is clicked
          break;
          case R.id.cardTwo:
              //do something here when card two is clicked.
          break;
          .
          . and so on.
 }

In your XML file, you need to put myMethod in the onClick attribute of each card, so that whenever a card is clicked, it will invoke myMethod() in the corresponding activity

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