java - Android-java 条件检查错误 - 需要纠正一些代码逻辑

标签 java android if-statement while-loop conditional-statements

这是我的示例详细信息收集页面的代码,一旦客户输入了他的详细信息,它将被存储,直到再次重新安装该应用程序,代码中没有错误并且执行正确,但客户能够无需填写表格即可进入下一个 Activity

我尝试了一些条件检查,但所有这些都失败了,需要代码逻辑方面的一些帮助

    package asr.customer;

import java.io.File;
import java.util.Locale;

import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.content.ContentValues;
import android.text.Editable;
import android.database.sqlite.SQLiteDatabase;
import android.view.*;
import android.widget.*;

public class ASRCustomerAppActivity extends Activity implements Button.OnClickListener{
/** Called when the activity is first created. */
    Button btn;
    ContentValues values= new ContentValues();
    SQLiteDatabase db;
    String first,last,state,city,g;
    int day,month,year;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn=(Button)findViewById(R.id.save);
        btn.setOnClickListener(this);
        File dbtest =new File("/data/data/asr.customer/databases/customerdetail1.db");
        if(dbtest.exists())
        {
          // what to do if it does exist
            Intent intent=new Intent();
            intent.setClass(this,mapActivity.class);
            startActivity(intent);
            finish();
        }
        else
        {

           db=openOrCreateDatabase("customerdetail1.db",SQLiteDatabase.CREATE_IF_NECESSARY,null);

            db.setVersion(1);
            db.setLocale(Locale.getDefault());
            db.setLockingEnabled(true);
            final String table_create="CREATE TABLE customer("+"firstname TEXT,"+"lastname TEXT,"+"day INTEGER,"+"gender TEXT,"+"month INTEGER,"+"year INTEGER,"+"city TEXT,"+"state TEXT);";
            db.execSQL(table_create);
            EditText i2=(EditText)findViewById(R.id.first);
            first=i2.getText().toString();
            EditText i3=(EditText) findViewById(R.id.last1);
            last=i3.getText().toString();

            DatePicker d=(DatePicker) findViewById(R.id.date);
            day=d.getDayOfMonth();
            month= d.getMonth()+1;
            year=d.getYear();
            RadioGroup rg= (RadioGroup) findViewById(R.id.radiogroup);
            RadioButton r1= (RadioButton) findViewById(R.id.radioButton1);
            RadioButton r2= (RadioButton) findViewById(R.id.radioButton2);
            if(r1.isChecked()==true)
            {
            g=r1.getText().toString();
            }
            else
            {
            g= r2.getText().toString();
            }

            EditText i4=(EditText) findViewById(R.id.city);
            city=i4.getText().toString();
            EditText i5=(EditText) findViewById(R.id.state);
            state=i5.getText().toString();





            }
        }
    public void onClick(View v) {
        // what to do if it doesn't exist

        do{
            //  code
            Toast.makeText(ASRCustomerAppActivity.this, "Please Fill The Blank Spaces", Toast.LENGTH_LONG).show();

        }while((first.equalsIgnoreCase("") || last.equalsIgnoreCase("") || city.equalsIgnoreCase("") || state.equalsIgnoreCase("")));


            values.put("firstname",first);
            values.put("lastname",last);
            values.put("day",day);
            values.put("month",month);
            values.put("year",year);
            values.put("gender",g);
            values.put("city",city);
            values.put("state",state);

            db.insert("customer", null, values);

            Intent intent=new Intent();
            intent.setClass(this,mapActivity.class);
            startActivity(intent);
            finish();
        }



    }

最佳答案

而不是这个

while((first.equalsIgnoreCase("") || last.equalsIgnoreCase("") || city.equalsIgnoreCase("") || state.equalsIgnoreCase("")));

试试这个

while((first.length==0|| last.length==0 || city.length==0|| state.length==0));

并将其放入您的 onClick

 first=i2.getText().toString();
 last=i3.getText().toString();
city=i4.getText().toString();
state=i5.getText().toString();

关于java - Android-java 条件检查错误 - 需要纠正一些代码逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9683100/

相关文章:

python - 从 for 循环中的 if else 语句的最后一行提取信息 Python

java - if 语句条件没有通过但应该是!? (java)

PHP If else 语句一条数据库记录为空会显示一张默认图片

java - 未找到后端。请确保您的 CLASSPATH 上有后端模块。我该如何解决这个错误

android - 直接从互联网在 ImageView 中显示图像

android - "I/ActivityManager: Displayed...activity...+850ms"由什么组成?

java - 如何从另一个类通知RecyclerView的DataSetChanged?

java - javax.xml.bind.JAXBException 的 ClassNotFoundException 与 Spring Boot 切换到 Java 9 时

java - 在上下文中获取 NullPointerException

java - gradle - 项目作为依赖项