lundi 17 septembre 2018

How to get the Single String value from the Multiple checkbox which are only checked

I'm using spinner to get selected value and upload it on firebase database. since from drop down only one value can be selected, so there is another Editbox where individual can write if they have elligible for other option too and it is totally a lengthy process.

So I'm looking to implement multiple check box in Grid view such that when user check the given check box, then I could retrive only the value that are checked in one single string and later upload in firebase.

Here is the code I found

public class MainActivity_test extends Activity {



    CheckBox chk1,chk2,chk3,chk4,chk5,chk6,chk7,chk8,chk9;
    Button btn;

    TextView txt;
    ArrayList<String> list ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);


        list = new ArrayList<String>();


        chk1=(CheckBox)findViewById(R.id.checkBox1);  
        chk2=(CheckBox)findViewById(R.id.checkBox2);  
        chk3=(CheckBox)findViewById(R.id.checkBox3);  
        chk4=(CheckBox)findViewById(R.id.checkBox4);  
        chk5=(CheckBox)findViewById(R.id.checkBox5);  
        chk6=(CheckBox)findViewById(R.id.checkBox6);  
        chk7=(CheckBox)findViewById(R.id.checkBox7);  
        chk8=(CheckBox)findViewById(R.id.checkBox8);  
        chk9=(CheckBox)findViewById(R.id.checkBox9);  
        txt = (TextView)findViewById(R.id.textView1);
        btn =(Button)findViewById(R.id.button1);  
        addListenerOnButton();
    }

    public void addListenerOnButton() {


        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                for (String str : list) {
     txt.setText(txt.getText().toString() + " , " + str);
}


            }

        });

    }



    public void onCheckboxClicked(View view) {

        boolean checked = ((CheckBox) view).isChecked();

        switch(view.getId()) {
        case R.id.checkBox1:
             list.add(chk1.getTag().toString());


        break;
        case R.id.checkBox2:
            list.add(chk2.getTag().toString());

        break;

        case R.id.checkBox3:
            list.add(chk3.getTag().toString());

            break;
        case R.id.checkBox4:
            list.add(chk4.getTag().toString());

            break;
        case R.id.checkBox5:
            list.add(chk5.getTag().toString());

            break;
        case R.id.checkBox6:
            list.add(chk6.getTag().toString());

            break;
        case R.id.checkBox7:
            list.add(chk7.getTag().toString());

            break;
        case R.id.checkBox8:
            list.add(chk8.getTag().toString());

            break;
        case R.id.checkBox9:
            list.add(chk9.getTag().toString());

            break;

        }
        }


}

But How do I get the value of only Selected item in as One single String output.

Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire