-2

Imagine I have checkbox with criteria in main activity and table in second activity. I want to link table to checkbox so for example when price and mileage is checked, only price and mileage columns are displayed. But when nothing is checked table is not appearing.I would really appreciate if someone could explain how to do it or give me a link to tutorial.

MainActivity.java:

package todo.beginner.com.carchooser2;
    
        import android.content.Intent;
        import android.os.Bundle;
        import android.support.v7.app.AppCompatActivity;
        import android.view.View;
        import android.widget.Button;
        import android.widget.CheckBox;
        import android.widget.Toast;
    
        import static todo.beginner.com.carchooser2.R.id.checkBoxPrice;
        import static todo.beginner.com.carchooser2.R.id.checkBoxGas;
        import static todo.beginner.com.carchooser2.R.id.checkBoxYear;
        import static todo.beginner.com.carchooser2.R.id.checkBoxMileage;
        import static todo.beginner.com.carchooser2.R.id.checkBoxCapacity;
    
        public class MainActivity extends AppCompatActivity {
            private CheckBox check1, check2, check3, check4, check5;
            private static Button button_next;
    
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                addListenerToCeckBox();
                OnClickButtonListener();
            }
    
            public void OnClickButtonListener() {
                button_next = (Button)findViewById(R.id.button);
                button_next.setOnClickListener(
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                                startActivity(intent);
    
                            }
                        }
                );
    new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                    intent.putExtra("Price", check1.isChecked());
                    startActivity(intent);
    
                }
            };
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                    intent.putExtra("Year", check2.isChecked());
                    startActivity(intent);
    
                }
            };
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                    intent.putExtra("Capacity", check3.isChecked());
                    startActivity(intent);
    
                }
            };
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                    intent.putExtra("Gas", check4.isChecked());
                    startActivity(intent);
    
                }
            };
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                    intent.putExtra("Mileage", check5.isChecked());
                    startActivity(intent);
    
                }
            };
    
    
            }
    
            public void addListenerToCeckBox() {
                check1 = (CheckBox)findViewById(checkBoxCena);
                check1.setOnClickListener(
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (((CheckBox)v).isChecked()){
                                    Toast.makeText(MainActivity.this,
                                            "Price is chosen", Toast.LENGTH_LONG).show();
                                }
                            }
                        }
                );
                check2 = (CheckBox)findViewById(checkBoxGads);
                check2.setOnClickListener(
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (((CheckBox)v).isChecked()){
                                    Toast.makeText(MainActivity.this,
                                            "Year is chosen", Toast.LENGTH_LONG).show();
                                }
                            }
                        }
                );
                check3 = (CheckBox)findViewById(checkBoxTilpums);
                check3.setOnClickListener(
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (((CheckBox)v).isChecked()){
                                    Toast.makeText(MainActivity.this,
                                            "Engine capacity is chosen", Toast.LENGTH_LONG).show();
                                }
                            }
                        }
                );
                check4 = (CheckBox)findViewById(checkBoxDegviela);
                check4.setOnClickListener(
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (((CheckBox)v).isChecked()){
                                    Toast.makeText(MainActivity.this,
                                            "Gas consumption is chosen", Toast.LENGTH_LONG).show();
                                }
                            }
                        }
                );
                check5 = (CheckBox)findViewById(checkBoxNobraukums);
                check5.setOnClickListener(
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (((CheckBox)v).isChecked()){
                                    Toast.makeText(MainActivity.this,
                                            "Mileage is chosen", Toast.LENGTH_LONG).show();
                                }
                            }
                        }
                );
    
            }
    
    
    
        }

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="todo.beginner.com.carchooser2.MainActivity">

    <CheckBox
        android:text="Price"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/checkBoxPrice"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="67dp" />

    <CheckBox
        android:text="Year"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/checkBoxPrice"
        android:layout_alignRight="@+id/checkBoxPrice"
        android:layout_alignEnd="@+id/checkBoxPrice"
        android:layout_marginTop="33dp"
        android:id="@+id/checkBoxYear" />

    <CheckBox
        android:text="Capacity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="37dp"
        android:id="@+id/checkBoxCapacity"
        android:layout_below="@+id/checkBoxYear"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <CheckBox
        android:text="Gas"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/checkBoxCapacity"
        android:layout_alignRight="@+id/checkBoxCapacity"
        android:layout_alignEnd="@+id/checkBoxCapacity"
        android:layout_marginTop="30dp"
        android:id="@+id/checkBoxGas" />

    <CheckBox
        android:text="Mileage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/checkBoxGas"
        android:layout_alignRight="@+id/checkBoxGas"
        android:layout_alignEnd="@+id/checkBoxGas"
        android:layout_marginTop="33dp"
        android:id="@+id/checkBoxMileage" />

    <Button
        android:text="Continue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="31dp"
        android:id="@+id/button" />

    <TextView
        android:text="Choose criteria!"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

SecondActivity.java:

package todo.beginner.com.carchooser2;
    
        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
    
        public class SecondActivity extends AppCompatActivity {
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_second);
         boolean hasPrice = getIntent().getBooleanExtra("Price", true);
            boolean hasYear = getIntent().getBooleanExtra("Year", true);
            boolean hasCapacity = getIntent().getBooleanExtra("Capacity", true);
            boolean hasGas = getIntent().getBooleanExtra("Gas", true);
            boolean hasMileage = getIntent().getBooleanExtra("Mileage", true);
    
            TextView Price = (TextView) findViewById(R.id.Price); // you will need to create this id in your layout
            Price.setVisibility(hasPrice ? View.VISIBLE : View.GONE);
    
            TextView Year = (TextView) findViewById(R.id.Year); // you will need to create this id in your layout
            Year.setVisibility(hasYear ? View.VISIBLE : View.GONE);
    
            TextView Capacity = (TextView) findViewById(R.id.Capacity); // you will need to create this id in your layout
            Capacity.setVisibility(hasCapacity ? View.VISIBLE : View.GONE);
    
            TextView Gas = (TextView) findViewById(R.id.Gas); // you will need to create this id in your layout
            Gas.setVisibility(hasGas ? View.VISIBLE : View.GONE);
    
            TextView Mileage = (TextView) findViewById(R.id.Mileage); // you will need to create this id in your layout
            Mileage.setVisibility(hasMileage ? View.VISIBLE : View.GONE);
            }
        }

activity_second.xml:

<?xml version="1.0" encoding="utf-8"?>
        <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="10dp">
    
            <TableRow
                android:background="#607D8B"
                android:padding="5dp">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:id="@+id/Name"
                    android:text="Car Name" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:id="@+id/Price"
                    android:text="Price" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:id="@+id/Year"
                    android:text="Year" />
            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:id="@+id/Gas"
                    android:text="Gas" />
            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:id="@+id/Mileage"
                    android:text="Mileage" />
            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:id="@+id/Capacity"
                    android:text="Capacity" />
            </TableRow>
    
            <TableRow
                android:background="#ECEFF1"
                android:padding="5dp">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Audi" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="5000" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="2001" />
            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="7" />
            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="280000" />
            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="2.5" />
            </TableRow>
        </TableLayout>
gunr2171
  • 16,104
  • 25
  • 61
  • 88

1 Answers1

1

In your OnClickButtonListener, you are declaring a lot of new View.OnClickListener()s, but you are just creating the implementations then not doing anything with them. Those should all be removed. Your method should look like this:

        public void OnClickButtonListener() {
            button_next = (Button)findViewById(R.id.button);
            button_next.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                            intent.putExtra("Price", check1.isChecked());
                            intent.putExtra("Year", check2.isChecked());
                            intent.putExtra("Capacity", check3.isChecked());
                            intent.putExtra("Gas", check4.isChecked());
                            intent.putExtra("Mileage", check5.isChecked());
                            startActivity(intent);

                        }
                    }
            );
        }

See how it parallels with the code in the onCreate method in the SecondActivity?

After this change, you should see about what you expect to see.


Your first step would put be to create an Intent that has the values of the checkboxes, and pass that to the second activity:

    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
    intent.putExtra("name", check1.isChecked());
    // same for other checkboxes
    startActivity(intent);

Next, in your SecondActivity access those values:

    boolean hasName = getIntent().getBooleanExtra("name", true);

When you are creating your TableRow, set the visibility based on the values:

    TextView carName = (TextView) findViewById(R.id.car_name); // you will need to create this id in your layout
    carName.setVisibility(hasName ? View.VISIBLE : View.GONE);

Remember, if nothing is checked then you won't see anything, so you should probably start MainActivity with all the checkboxes checked to begin with.

kris larson
  • 30,387
  • 5
  • 62
  • 74
  • boolean hasName = getIntent().getBooleanExtra("name"); when I am writing this in SecondActivity it shows error ''getBooleanExtra (string, boolean) in intent cannot be applied to (String)". Am I doing it correctly putting it in OnCreate? – TotalAndroidBeginner Dec 07 '16 at 18:29
  • That's because I wrote it wrong. Should be `getBooleanExtra("name", true)` (assuming you want the default to be true). Updated answer. – kris larson Dec 07 '16 at 21:25
  • Thanks about that! I updated the code and added activity_main.xml code. Could you please check if I did everything as I was supposed to or messed up big time? Thanks in advance! – TotalAndroidBeginner Dec 08 '16 at 16:56
  • Updated my answer to suggest some changes. – kris larson Dec 08 '16 at 17:11