Search code examples
javaandroidandroid-activityandroid-recyclerviewandroid-cardview

How to generate multiple cardview based on user input in android


I'm trying to design a page where address are stored in recycler view -> cardview.

When the user clicks the add address button from the Activity A the user is navigated to the add address page in Activity B. Here the user can input customer name, address line 1 and address line two.

And once save button is clicked in Activity B, a cardview should be created under the add address button in the Activity A.

This design is just like the amazon mobile app add address option.

Example: If a end-user has one address(one cardview will be present) and wants to add one more address(second address). Then another cardview will be created below the existing cardview with the new address.

Actual result as of now: The second address is being populated in the first cardview. Expected result: When user enters a new address a new cardview should be created below the existing one.

Code in the Activity A

public class ProfileManageAdressFragment extends AppCompatActivity {

    RecyclerView recyclerView;
    ProfileManageAddressRecyclerAdapter adapter;
    ArrayList<ProfileManageAddressGetterSetter> reviews;
    private Button addAddress;
    private String customer_name, address_one, address_two, city, state, pincode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_profile_manage_adress);
        addAddress = findViewById(R.id.addNewAddress);
        addAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), "Clicked", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(ProfileManageAdressFragment.this, AddNewAddress.class);
                startActivityForResult(intent, 1);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == 1) {
            if(resultCode == RESULT_OK) {
                customer_name = data.getStringExtra("customer_name");
                address_one = data.getStringExtra("address_one");
                address_two = data.getStringExtra("address_two");
                city = data.getStringExtra("city");
                state = data.getStringExtra("state");
                pincode = data.getStringExtra("pincode");

                reviews = new ArrayList<>();
                reviews.add(new ProfileManageAddressGetterSetter(customer_name, address_one, address_two, city, state, pincode));

                recyclerView = findViewById(R.id.addressRecyclerView);
                recyclerView.setHasFixedSize(true);
                recyclerView.setLayoutManager(new LinearLayoutManager(ProfileManageAdressFragment.this));
                adapter = new ProfileManageAddressRecyclerAdapter(ProfileManageAdressFragment.this, reviews);
                recyclerView.setAdapter(adapter);
            }
        }else if(resultCode == RESULT_CANCELED) {
            Toast.makeText(ProfileManageAdressFragment.this, "Cancelled", Toast.LENGTH_SHORT).show();
        }
    }
}

Code in Activity B

public class AddNewAddress extends AppCompatActivity {

    private EditText customer_name, address_one, address_two, city, state, pincode;
    private Button add_address;
    private String sCustomer_name, sAddress_one, sAddress_two, sCity, sState, sPincode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_new_address);

        customer_name = findViewById(R.id.customerName);
        address_one = findViewById(R.id.addressOne);
        address_two = findViewById(R.id.addressTwo);
        add_address = findViewById(R.id.addAddress);
        city = findViewById(R.id.city);
        state = findViewById(R.id.state);
        pincode = findViewById(R.id.pincode);

        final ProfileFragment profileFragment = new ProfileFragment();
        add_address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //setFragment(profileFragment);
                if(customer_name.getText().toString().equals("") || address_one.getText().toString().equals("") ||
                        address_two.getText().toString().equals("") || city.getText().toString().equals("") ||
                        state.getText().toString().equals("") || pincode.getText().toString().equals("")
                        ) {
                    Toast.makeText(AddNewAddress.this, "Please input all fields", Toast.LENGTH_LONG).show();
                }else {
                    sCustomer_name = customer_name.getText().toString();
                    sAddress_one = address_one.getText().toString();
                    sAddress_two = address_two.getText().toString();
                    sCity = city.getText().toString();
                    sState = state.getText().toString();
                    sPincode = pincode.getText().toString();

                    Intent intent = new Intent(AddNewAddress.this, ProfileManageAdressFragment.class);
                    intent.putExtra("customer_name", sCustomer_name);
                    intent.putExtra("address_one", sAddress_one);
                    intent.putExtra("address_two", sAddress_two);
                    intent.putExtra("city", sCity);
                    intent.putExtra("state", sState);
                    intent.putExtra("pincode", sPincode);
                    //startActivity(intent);
                    //startActivityForResult(intent, 1);
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }
        });
    }
}

Kindly let me know if additional information is required. Million thanks in advance for solutions! :)


Solution

  • There are many things you have to handle

    In ProfileManageAdressFragment

    • Naming Convention matter more extended AppCompatActivity and the class name is ProfileManageAdressFragment.
    • Button object created globally that not required, make it local obj.
    • same String customer_name and many more obj you're using inside onActivityResult so don't need to initialize globally make it local Obj.
    • In onActivityResult every time you reinitialize the recyclerView and adapter that not required. initialize recyclerView and adapter inside onCreate and when you get Data in onActivityResult add data to ArrayList and call adapter.notifyDataSetChange.
    • In onActivityResult using nested condition for request code and ResultCode make it on single condition Like. if (requestCode == 1 && resultCode == RESULT_OK){ code body }else {}

    In AddNewAddress

    • All globally initialize objects not required it takes memory only. so make it locally if you're not using outside of the method.
    • Instead checking value like this customer_name.getText().toString().equals("") , android provide one class TextUtil and TextUtil has one method isEmpty use that method.