Home > Software engineering >  How to pass in a parameter into a function?
How to pass in a parameter into a function?

Time:03-20

I'm trying to add the addcard name parameter in my addCard function under case R.id.btn_billHistory: How can I pass the name parameter in it? I'm not sure how to solve it. This line is giving me an error.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch (id){
        case R.id.btn_billHistory:
            //Toast.makeText(getApplicationContext(),"Bill History",Toast.LENGTH_LONG).show();
            Intent i = new Intent(MainActivity.this,BillActivity.class);
            startActivity(i);
            finish();
            **addCard();**
            return true;
        case R.id.btn_about:
            openCloseDialog();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

private void **addCard(String name)** {
    final View view = getLayoutInflater().inflate(R.layout.activity_bill, null);

    TextView nameView = view.findViewById(R.id.edtName);
    Button delete = view.findViewById(R.id.btn_delete);

    nameView.setText(name);

    delete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            layout.removeView(view);
        }
    });
    layout.addView(view);
}

CodePudding user response:

Well you need to pass the addCard()functions before start activity

   public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch (id){
        case R.id.btn_billHistory:
            //Toast.makeText(getApplicationContext(),"Bill History",Toast.LENGTH_LONG).show();
            addCard("Check Bill History");
            Intent i = new Intent(MainActivity.this,BillActivity.class);
            startActivity(i);
            finish();
            
            return true;
        case R.id.btn_about:
            openCloseDialog();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

    private void **addCard(String name)** {
        final View view = getLayoutInflater().inflate(R.layout.activity_bill, null);
    
        TextView nameView = view.findViewById(R.id.edtName);
        Button delete = view.findViewById(R.id.btn_delete);
    
        nameView.setText(name);
    
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                layout.removeView(view);
            }
        });
        layout.addView(view);
    }

I hope this work for you!!

CodePudding user response:

You need to pass a string to the addCard call in onOptionsItemSelected.

Just skimming your code snippet, I would guess it's "Bill History", but the string you pass is up to you.

For example:

case R.id.btn_billHistory:
    Intent i = new Intent(MainActivity.this,BillActivity.class);
    startActivity(i);
    finish();
    addCard("Bill History");
    return true;

However, just noticing your finish call, and it doesn't make sense to update any views in current activity if you're just going to start another activity. So you might want to rethink that bit.

  • Related