用逗号分隔所有具有非空值的 EditText
Separate all EditText with non Empty value by a comma
我是 java 和 sql 的初学者,我想整理一些我的 table 的信息。
我已将数据库连接到我的 java 项目。我想在我的 table 的一列中获取 EditText 的所有值。我想获取所有非空值并用逗号“,”分隔它们。问题是它会获取 EditText 的所有值,即使它们是空的。
我正在 table 中寻找这种结果:
专栏:曼奇
行:示例值 1、示例值 3、示例值 7
但实际上我有这个,
专栏:曼奇
行:示例值 1,,示例值 3,,,,,示例值 7
'''
private Controle3 controle3;
private void init() {
nb1 = (EditText) findViewById(R.id.nb1);
nb2 = (EditText) findViewById(R.id.nb2);
nb3 = (EditText) findViewById(R.id.nb3);
nb4 = (EditText) findViewById(R.id.nb4);
nb5 = (EditText) findViewById(R.id.nb5);
nb6 = (EditText) findViewById(R.id.nb6);
nb7 = (EditText) findViewById(R.id.nb7);
nb8 = (EditText) findViewById(R.id.nb8);
nb9 = (EditText) findViewById(R.id.nb9);
nb10 = (EditText) findViewById(R.id.nb10);
nb11 = (EditText) findViewById(R.id.nb11);
nb12 = (EditText) findViewById(R.id.nb12);
nb13 = (EditText) findViewById(R.id.nb13);
nb14 = (EditText) findViewById(R.id.nb14);
nb15 = (EditText) findViewById(R.id.nb15);
nb16 = (EditText) findViewById(R.id.nb16);
nb17 = (EditText) findViewById(R.id.nb17);
nb18 = (EditText) findViewById(R.id.nb18);
nb19 = (EditText) findViewById(R.id.nb19);
nb20 = (EditText) findViewById(R.id.nb20);
nb21 = (EditText) findViewById(R.id.nb21);
nb22 = (EditText) findViewById(R.id.nb22);
nb23 = (EditText) findViewById(R.id.nb23);
nb24 = (EditText) findViewById(R.id.nb24);
ecouteValidation();
this.controle3 = Controle3.getInstance(this);
recupProfil();
}
private void ecouteValidation(){
((Button) findViewById(R.id.btnValid)).setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
// Toast.makeText(FirstActivity.this, "test", Toast.LENGTH_SHORT).show();
//Log.d("message", "clic ok sur le bouton Valid ******************");
String immatriculation = null;
String name = null;
String manque = null;
try {
immatriculation = txtImmat.getText().toString();
name = txtname.getText().toString();
} catch (Exception e) {};
if(nb1.getText().toString().isEmpty() && nb2.getText().toString().isEmpty() && nb3.getText().toString().isEmpty() && nb4.getText().toString().isEmpty() && nb5.getText().toString().isEmpty() && nb6.getText().toString().isEmpty() && nb7.getText().toString().isEmpty() && nb8.getText().toString().isEmpty() && nb9.getText().toString().isEmpty() && nb10.getText().toString().isEmpty() && nb11.getText().toString().isEmpty() && nb12.getText().toString().isEmpty() && nb13.getText().toString().isEmpty() && nb14.getText().toString().isEmpty() && nb15.getText().toString().isEmpty() && nb16.getText().toString().isEmpty() && nb17.getText().toString().isEmpty() && nb18.getText().toString().isEmpty() && nb19.getText().toString().isEmpty() && nb20.getText().toString().isEmpty() && nb21.getText().toString().isEmpty() && nb22.getText().toString().isEmpty() && nb23.getText().toString().isEmpty() && nb24.getText().toString().isEmpty()){
manque = "COMPLET";
}else {
for(nb1.getText().toString().isEmpty()){
manque = nb1.getText().toString() + nb2.getText().toString() + " " + nb3.getText().toString() + " " + nb4.getText().toString() + " " + nb5.getText().toString() + " " + nb6.getText().toString() + " " + nb7.getText().toString() + " " + nb8.getText().toString() + " " + nb9.getText().toString() + " " + nb10.getText().toString() + " " + nb11.getText().toString() + " " + nb12.getText().toString() + " " + nb13.getText().toString() + " " + nb14.getText().toString() + " " + nb15.getText().toString() + " " + nb16.getText().toString() + " " + nb17.getText().toString() + " " + nb18.getText().toString() + " " + nb19.getText().toString() + " " + nb20.getText().toString() + " " + nb21.getText().toString() + " " + nb22.getText().toString() + " " + nb23.getText().toString() + " " + nb24.getText().toString();
}
}
if (name.isEmpty() || immatriculation.isEmpty()) {
Toast.makeText(MaterielVSLActivity.this, "Saisie incorrecte", Toast.LENGTH_SHORT).show();
} else {
afficheResult(immatriculation, name, manque);
Toast.makeText(MaterielVSLActivity.this, "OK", Toast.LENGTH_SHORT).show();
}
}
});
}
/**
* Affichage du message
*
*/
private void afficheResult(String immatriculation, String name, String manque){
this.controle3.creerProfil(immatriculation, name, manque, this);
}
}'''
您可以将所有 EditText 添加到一个数组中
EditText[] editTexts ={nb1,nb2,nb3...};
String content;
并循环数组检查项目内容是否为空将其添加到内容
for(EditText myEditText : editTexts){
if(!myEditText.getText().toString().isEmpty()){
content += myEditText.getText().toString() + ","
}
}
我是 java 和 sql 的初学者,我想整理一些我的 table 的信息。 我已将数据库连接到我的 java 项目。我想在我的 table 的一列中获取 EditText 的所有值。我想获取所有非空值并用逗号“,”分隔它们。问题是它会获取 EditText 的所有值,即使它们是空的。
我正在 table 中寻找这种结果:
专栏:曼奇
行:示例值 1、示例值 3、示例值 7
但实际上我有这个,
专栏:曼奇
行:示例值 1,,示例值 3,,,,,示例值 7
'''
private Controle3 controle3;
private void init() {
nb1 = (EditText) findViewById(R.id.nb1);
nb2 = (EditText) findViewById(R.id.nb2);
nb3 = (EditText) findViewById(R.id.nb3);
nb4 = (EditText) findViewById(R.id.nb4);
nb5 = (EditText) findViewById(R.id.nb5);
nb6 = (EditText) findViewById(R.id.nb6);
nb7 = (EditText) findViewById(R.id.nb7);
nb8 = (EditText) findViewById(R.id.nb8);
nb9 = (EditText) findViewById(R.id.nb9);
nb10 = (EditText) findViewById(R.id.nb10);
nb11 = (EditText) findViewById(R.id.nb11);
nb12 = (EditText) findViewById(R.id.nb12);
nb13 = (EditText) findViewById(R.id.nb13);
nb14 = (EditText) findViewById(R.id.nb14);
nb15 = (EditText) findViewById(R.id.nb15);
nb16 = (EditText) findViewById(R.id.nb16);
nb17 = (EditText) findViewById(R.id.nb17);
nb18 = (EditText) findViewById(R.id.nb18);
nb19 = (EditText) findViewById(R.id.nb19);
nb20 = (EditText) findViewById(R.id.nb20);
nb21 = (EditText) findViewById(R.id.nb21);
nb22 = (EditText) findViewById(R.id.nb22);
nb23 = (EditText) findViewById(R.id.nb23);
nb24 = (EditText) findViewById(R.id.nb24);
ecouteValidation();
this.controle3 = Controle3.getInstance(this);
recupProfil();
}
private void ecouteValidation(){
((Button) findViewById(R.id.btnValid)).setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
// Toast.makeText(FirstActivity.this, "test", Toast.LENGTH_SHORT).show();
//Log.d("message", "clic ok sur le bouton Valid ******************");
String immatriculation = null;
String name = null;
String manque = null;
try {
immatriculation = txtImmat.getText().toString();
name = txtname.getText().toString();
} catch (Exception e) {};
if(nb1.getText().toString().isEmpty() && nb2.getText().toString().isEmpty() && nb3.getText().toString().isEmpty() && nb4.getText().toString().isEmpty() && nb5.getText().toString().isEmpty() && nb6.getText().toString().isEmpty() && nb7.getText().toString().isEmpty() && nb8.getText().toString().isEmpty() && nb9.getText().toString().isEmpty() && nb10.getText().toString().isEmpty() && nb11.getText().toString().isEmpty() && nb12.getText().toString().isEmpty() && nb13.getText().toString().isEmpty() && nb14.getText().toString().isEmpty() && nb15.getText().toString().isEmpty() && nb16.getText().toString().isEmpty() && nb17.getText().toString().isEmpty() && nb18.getText().toString().isEmpty() && nb19.getText().toString().isEmpty() && nb20.getText().toString().isEmpty() && nb21.getText().toString().isEmpty() && nb22.getText().toString().isEmpty() && nb23.getText().toString().isEmpty() && nb24.getText().toString().isEmpty()){
manque = "COMPLET";
}else {
for(nb1.getText().toString().isEmpty()){
manque = nb1.getText().toString() + nb2.getText().toString() + " " + nb3.getText().toString() + " " + nb4.getText().toString() + " " + nb5.getText().toString() + " " + nb6.getText().toString() + " " + nb7.getText().toString() + " " + nb8.getText().toString() + " " + nb9.getText().toString() + " " + nb10.getText().toString() + " " + nb11.getText().toString() + " " + nb12.getText().toString() + " " + nb13.getText().toString() + " " + nb14.getText().toString() + " " + nb15.getText().toString() + " " + nb16.getText().toString() + " " + nb17.getText().toString() + " " + nb18.getText().toString() + " " + nb19.getText().toString() + " " + nb20.getText().toString() + " " + nb21.getText().toString() + " " + nb22.getText().toString() + " " + nb23.getText().toString() + " " + nb24.getText().toString();
}
}
if (name.isEmpty() || immatriculation.isEmpty()) {
Toast.makeText(MaterielVSLActivity.this, "Saisie incorrecte", Toast.LENGTH_SHORT).show();
} else {
afficheResult(immatriculation, name, manque);
Toast.makeText(MaterielVSLActivity.this, "OK", Toast.LENGTH_SHORT).show();
}
}
});
}
/**
* Affichage du message
*
*/
private void afficheResult(String immatriculation, String name, String manque){
this.controle3.creerProfil(immatriculation, name, manque, this);
}
}'''
您可以将所有 EditText 添加到一个数组中
EditText[] editTexts ={nb1,nb2,nb3...};
String content;
并循环数组检查项目内容是否为空将其添加到内容
for(EditText myEditText : editTexts){
if(!myEditText.getText().toString().isEmpty()){
content += myEditText.getText().toString() + ","
}
}