简体   繁体   English

如何从多个复选框中获取“单个字符串”值,仅选中该复选框

[英]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. 我正在使用微调器来获取选定的值,并将其上传到Firebase数据库。 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. 由于从下拉列表中只能选择一个值,因此还有另一个Editbox,如果个人也可以使用其他选项,则可以在其中编辑,这是一个漫长的过程。

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. 因此,我希望在Grid视图中实现多个复选框,以便当用户选中给定的复选框时,我只能检索在单个字符串中选中的值,然后再在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. 提前致谢。

You should do something like this 你应该做这样的事情

public class MainActivity_test extends Activity implements CompoundButton.OnCheckedChangeListener{

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

    addCheckboxListner();
}

private void addCheckboxListner(){
    chk1.setOnCheckedChangeListener(this);
    chk2.setOnCheckedChangeListener(this);
    chk3.setOnCheckedChangeListener(this);
    chk4.setOnCheckedChangeListener(this);
    chk5.setOnCheckedChangeListener(this);
    chk6.setOnCheckedChangeListener(this);
    chk7.setOnCheckedChangeListener(this);
    chk8.setOnCheckedChangeListener(this);
    chk9.setOnCheckedChangeListener(this);
}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    onCheckboxClicked(buttonView);
}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM