【问题标题】:Android-java Contdition Check error - need some code logic to be correctedAndroid-java 条件检查错误 - 需要更正一些代码逻辑
【发布时间】:2012-03-29 19:10:18
【问题描述】:

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

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

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



    }

【问题讨论】:

  • 我在对该代码进行简短检查后立即回击。但我认为最好给你一些反馈为什么。如果你给别人更多的代码,比如这里的代码:请正确缩进。也可以使用空行来分隔一些(逻辑)块。这有助于发现错误并大大提高可读性。
  • @alextsc 好的朋友现在不这样做,但请尝试在此纠正我

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


【解决方案1】:

而不是这个

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

【讨论】:

  • 什么是 i2,i3,i4,i5 朋友??...我必须用那个替换什么
  • 而不是声明EditText i2;在 oncreate 中,在 oncreate 之前声明这些变量.. 以便它们在整个类中都可用... i2,i3.. 是 edittext 对象..
【解决方案2】:

您无法在 onCreate 方法中获取值,因为在窗口获得焦点之前不会绘制视图。

您必须在 onClick 方法中执行此操作,以使其像将其移至 buttonClick 方法一样工作:

city=i4.getText().toString();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多