The Time
The Time

Reputation: 737

Android: The method getId() in the type View is not applicable for the arguments (int)

I am trying to access the cb in the if statement but I am getting cb cant be resolved

I have tried declare Checkbox cb as class variable but I am getting The method getId() in the type View is not applicable for the arguments (int).

I tried to declare it as method local variable like final CheckBox cb; but I am getting two errors: The first one The final local variable cb may already have been assigned at this line cb = new CheckBox(this); and the second one The method getId() in the type View is not applicable for the arguments (int)

how can I fix that?

private void createCheckboxList(final ArrayList<Integer> items) {
    //final CheckBox cb;

    final LinearLayout ll = (LinearLayout) findViewById(R.id.lila);
    for (int i = 0; i < items.size(); i++) {
        CheckBox cb = new CheckBox(this);
        cb.setText(String.valueOf(items.get(i)));
        cb.setId(i);
        ll.addView(cb);

    }
    Button btn = new Button(this);
    btn.setLayoutParams(new LinearLayout.LayoutParams(500, 150));
    btn.setText("submit");
    ll.addView(btn);

    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            for (int i : items) {
                if (cb.getId(i).isChecked()) {

                }
            }

        }
    });

}

Upvotes: 0

Views: 1145

Answers (1)

Paul Rast
Paul Rast

Reputation: 53

  1. The reference cb wont exist outside the 'for' loop. Since you know its id you can create a new checkbox reference and use findviewById(); to get refer to the same checkbox

  2. cb.getId() returns and integer not a checkbox reference

    final LinearLayout ll = (LinearLayout) findViewById(R.id.lila);
    for (int i = 0; i < items.size(); i++) {
    final CheckBox cb = new CheckBox(this);
    cb.setText(String.valueOf(items.get(i)));
    cb.setId(i);
    ll.addView(cb);
    
    }
    Button btn = new Button(this);
    btn.setLayoutParams(new LinearLayout.LayoutParams(500, 150));
    btn.setText("submit");
    ll.addView(btn);
    
    btn.setOnClickListener(new View.OnClickListener() {
    
    @Override
    public void onClick(View v) {
        for (int i = 0; i < items.size()) {   \\
             Checkbox ch=(Checkbox) findViewById(i);  \\
            if (ch.isChecked()) {
    
            }
        }
    
    }
    });
    
    }
    

Upvotes: 1

Related Questions