Get the value of the checkboxes after clicking the transmit button - android

I have checkbox list and I am trying to get the result after the user selected multiple chexkboxes and clicked the transmit button
how can I get the multiple checkboxes values after clicking the trasmit button?
I appreciate any help.
private void createRadioButton(final ArrayList<Integer> items) {
final LinearLayout ll = (LinearLayout) findViewById(R.id.lila);
for (int i = 0; i < items.size(); i++) {
CheckBox cb = new CheckBox(this);
cb.setText(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);
}
Edit 1:
private void createRadioButton(final ArrayList items) {
final LinearLayout ll = (LinearLayout) findViewById(R.id.lila);
for (int i = 0; i < items.size(); i++) {
CheckBox cb = new CheckBox(this);
cb.setText(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);
//here I am getting this error: OnClickListener cannot be resolved to a type
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
for(int i: items){
//here I am getting `cb cannot be resolved`
if(cb.getId(i).isChecked()){
}
}
}
});
}

Related

How can get dynamic button text from another dynamic button OnClickListener event

I want get second dynamic button text from another dynamic button OnClickListener event:
Here is define some dynamic buutons:
LinearLayout lv=(LinearLayout)findViewById(R.id.lv);
for (int k = 1; k <= str[0].length(); k++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(100, 100);
btnTopEn = new Button(this);
btnTopEn.setId(k);
final int id_ = btnTopEn.getId();
btnTopEn.setText(" ");
lv.addView(btnTopEn, params);
btnTopEn = ((Button) findViewById(id_));
final Button finalBtnT = btnTopEn;
btnTopEn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
finalBtnT.setText("");
}
});
}
Now I want text of second button from OnClickListener Event:
TableLayout layout = (TableLayout)findViewById(R.id.TableL);
String stt="RZCEADHPTAUJTSFR";
int l=0;
for (int f=0; f<=1; f++) {
TableRow tr = new TableRow(this);
for (int c=0; c<=7; c++) {
btnCEn = new Button (this);
String ss=(String.valueOf(stt.charAt(l)));
btnCEn.setText(ss);;
final Button finalBtnB = btnCEn;
btnCEn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int m=2;
btnTopEn = ((Button) findViewById(m));
final Button finalBtnT = btnTopEn;
if (finalBtnT.getText().equals("")) {
String stGetText=finalBtnB.getText().toString();
finalBtnT.setText(stGetText);
break;
}
}
}
});
TableRow.LayoutParams lp = new TableRow.LayoutParams(100,100);
tr.addView(btnCEn, lp);
}
layout.addView(tr);
}
I wrote some code in OnClickListener event but none happen!
What is the value of str in the first loop ?
Also you are setting the text to a space .
btnTopEn.setText(" ");
And while checking you check for empty :
if (finalBtnT.getText().equals("")){
}
Try changing to
if (finalBtnT.getText().toString().trim().equals("")){
}

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

I am trying to implement checkboxes list so after selecte the multiple checkboxes I want to store them in ArrayList and after clicking the button send them to the server. At the momenant I am getting the following error in the is statement
The method getId() in the type View is not applicable for the arguments (int)
as wellas at this line cb = new CheckBox(this);
The final local variable cb may already have been assigned
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(items.get(i).toString());
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()) {
}
}
}
});
}
Edit:
I have deleted the final CheckBox cb; and assign the object in the for loop CheckBox cb = new CheckBox(this); but I am getting in the if statement this error cb cannot be resolved How can I assign it to get ride of the error?
When I assign the Checkbox as class variable I am getting this error The method getId() in the type View is not applicable for the arguments (int) in the if statement.
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);
}
use this rather

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()) {
}
}
}
});
}
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
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()) {
}
}
}
});
}

Remove the Button from the LinearLayout

I am creating a CheckBox list and Button from ArrayList<Integer>, which I am getting as response from the server, programmatically. For updating purpose I am trying to delete the CheckBox list as well as the Button before I add the new one. With the current state the Button is not being deleted I noticed that when the ArrayList becomes empty the Button was not deleted.
How can I delete the Button in the case the table is empty since too there is some cases where the table becomes empty?
#Override
public void onAsyncTaskFinished(ArrayList<Integer> result) {
remove_elements();
createCheckboxList(result);
}
private void remove_elements() {
LinearLayout parent = (LinearLayout) findViewById(R.id.lila);
for (int i : items) {
CheckBox ch = (CheckBox) findViewById(i);
if (ch != null) {
parent.removeView(ch);
} else {
System.out.println("The checkbox is null");
}
}
Button btn = (Button) findViewById(1);
if (btn != null) {
parent.removeView(btn);
} else {
System.out.println("The button is null");
}
}
private void createCheckboxList(final ArrayList<Integer> items) {
this.items = items;
final ArrayList<Integer> selected = new ArrayList<Integer>();
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(items.get(i));
ll.addView(cb);
}
Button btn = new Button(this);
btn.setLayoutParams(new LinearLayout.LayoutParams(500, 150));
btn.setText("submit");
btn.setId(1);
ll.addView(btn);
btn.setOnClickListener(new View.OnClickListener() {}
}
Only create the button if items.size() > 0
if (items.size() > 0){
Button btn = new Button(this);
btn.setLayoutParams(new LinearLayout.LayoutParams(500, 150));
btn.setText("submit");
btn.setId(1);
ll.addView(btn);
btn.setOnClickListener(new View.OnClickListener() {}
}

Dynamically create button using data in array and setOnClicklistener

im trying to add the OnClickListener to the button that it generated by the value in array, but m fail to do so, any suggestion here?
long lprice = Long.parseLong(searchId.getText().toString());
List<String> buttonNameOne = dbc.getItemNameRbPrice(lprice);
List<String> buttonPriceOne = dbc.getPriceRbPrice(lprice);
List<String> buttonDateOne = dbc.getCurrentDateRbPrice(lprice);
for(int i = 0 ; i < buttonNameOne.size() ; i ++)
{
Button btn = new Button(this);
btn.setId(2000+i);
btn.setText(buttonNameOne.get(i) + i);
linearButton.addView(btn, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Log.v("Value of element "+i, buttonNameOne.get(i));
Log.v("Value of element "+i, buttonPriceOne.get(i));
Log.v("Value of element "+i, buttonDateOne.get(i));
}
What you want to do is add the btn.setOnClickListener
for(int i = 0; i < buttonnameOne.size(); i++)
{
Button btn = new Button(this);
btn.setId(2000+i);
btn.setText(buttonNameOne.get(i) + i));
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(view v) {
// TODO Whatever you want onclick to do
}
});
//Log stuff
linearButton.addView(btn, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}

Categories

Resources