【发布时间】: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