我使用SQLite数据库来保存我应用中不同活动的数据。在第一个活动中,我使用add
方法在表中创建一行,下一个活动使用update
方法更新创建的列。我之前的问题在某种程度上类似,可以在这里找到:
Previous Question
以前我的问题是没有使用setId
。现在我的问题是第三项活动。调用update
方法后,列保持 Null 。我尝试将id从NewProjectActivity传递给MainActivity,然后传递给IntensityActivity,但我不知道为什么列不会更新。以下是代码:
SQLite Helper:
public class SQLiteHelper extends SQLiteOpenHelper implements ProjectDAO {
public SQLiteHelper(Context context) {
super(context, "my_db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL("CREATE TABLE tbl_project_info (id INTEGER PRIMARY KEY," +
"name TEXT," +
"company_name TEXT," +
"address TEXT," +
"length1 TEXT," +
"length2 TEXT," +
"length3 TEXT," +
"length4 TEXT," +
"length5 TEXT," +
"diameter1 Text," +
"diameter2 Text," +
"diameter3 Text," +
"diameter4 Text," +
"diameter5 Text," +
"surface Text," +
"soilResistance Text," +
"intensity Text," +
"allowedIntensity Text," +
"intensityResult Text)");
} catch (SQLiteException e) {
Log.e("SQLITE", "onCreate: " + e.toString());
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
@Override
public long addProject(Project project) {
SQLiteDatabase db = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", project.getName());
contentValues.put("company_name", project.getCompany_name());
contentValues.put("address", project.getAddress());
contentValues.put("length1", project.getLength1());
contentValues.put("length2", project.getLength2());
contentValues.put("length3", project.getLength3());
contentValues.put("length4", project.getLength4());
contentValues.put("length5", project.getLength5());
contentValues.put("diameter1", project.getDiameter1());
contentValues.put("diameter2", project.getDiameter2());
contentValues.put("diameter3", project.getDiameter3());
contentValues.put("diameter4", project.getDiameter4());
contentValues.put("diameter5", project.getDiameter5());
contentValues.put("surface", project.getSurface());
contentValues.put("soilResistance", project.getSoilResistance());
contentValues.put("intensity", project.getIntensity());
contentValues.put("allowedIntensity", project.getAllowedIntensity());
contentValues.put("intensityResult", project.getIonS());
long result = db.insert("tbl_project_info", null, contentValues);
db.close();
return result;
}
@Override
public int getProjectsCount() {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM tbl_project_info", null);
int count = cursor.getCount();
cursor.close();
db.close();
return count;
}
@Override
public boolean updateProject(Project project) {
SQLiteDatabase db = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("length1", project.getLength1());
contentValues.put("length2", project.getLength2());
contentValues.put("length3", project.getLength3());
contentValues.put("length4", project.getLength4());
contentValues.put("length5", project.getLength5());
contentValues.put("diameter1", project.getDiameter1());
contentValues.put("diameter2", project.getDiameter2());
contentValues.put("diameter3", project.getDiameter3());
contentValues.put("diameter4", project.getDiameter4());
contentValues.put("diameter5", project.getDiameter5());
contentValues.put("surface", project.getSurface());
contentValues.put("soilResistance", project.getSoilResistance());
contentValues.put("intensity", project.getIntensity());
contentValues.put("allowedIntensity", project.getAllowedIntensity());
contentValues.put("intensityResult", project.getIonS());
db.update("tbl_project_info", contentValues, "id = ?", new String[]{String.valueOf(project.getId())});
db.close();
return true;
}
@Override
public List<Project> getAllProjects() {
List<Project> projects = new ArrayList<>();
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM tbl_project_info", null);
if (cursor.moveToFirst()) {
do {
Project project = new Project();
project.setName(cursor.getString(0));
project.setCompany_name(cursor.getString(1));
project.setAddress(cursor.getString(2));
projects.add(project);
} while (cursor.moveToNext());
}
return projects;
}
}
NewProjectActivity:
public class NewProjectActivity extends AppCompatActivity {
private ProjectDAO projectDAO;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_project);
projectDAO = DBInjector.provideProjectDao(this);
setupViews();
}
private void setupViews() {
final EditText projectNameET = findViewById(R.id.et_newProject_projectName);
final EditText companyNameET = findViewById(R.id.et_newProject_companyName);
final EditText addressET = findViewById(R.id.et_newProject_address);
Button saveInfoBTN = findViewById(R.id.btn_newProject_saveInfo);
saveInfoBTN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
long projectID = -1;
if (projectNameET.length() > 0) {
if (companyNameET.length() > 0) {
if (addressET.length() > 0) {
Project project = new Project();
project.setName(projectNameET.getText().toString());
project.setCompany_name(companyNameET.getText().toString());
project.setAddress(addressET.getText().toString());
projectID = projectDAO.addProject(project);
if (projectID > 0){
Toast.makeText(NewProjectActivity.this, "Success", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(NewProjectActivity.this, "Failed", Toast.LENGTH_SHORT).show();
}
}
}else {
companyNameET.setError("company name not entered");
}
}else{
projectNameET.setError("project name not entered");
}
PersonalInfoSharedPrefManager manager = new PersonalInfoSharedPrefManager(NewProjectActivity.this);
manager.setID(projectID);
Intent intent = new Intent(NewProjectActivity.this,MainActivity.class);
intent.putExtra("IE_PROJECTID",projectID);
startActivity(intent);
}
});
}
}
MainActivity:
public class MainActivity extends AppCompatActivity {
private ProjectDAO projectDAO;
private long mProjectID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
projectDAO = DBInjector.provideProjectDao(this);
mProjectID = getIntent().getLongExtra("IE_PROJECTID",0);
final EditText lengthET1 = findViewById(R.id.et_main_length1);
final EditText lengthET2 = findViewById(R.id.et_main_length2);
final EditText lengthET3 = findViewById(R.id.et_main_length3);
final EditText lengthET4 = findViewById(R.id.et_main_length4);
final EditText lengthET5 = findViewById(R.id.et_main_length5);
final EditText diameterET1 = findViewById(R.id.et_main_diameter1);
final EditText diameterET2 = findViewById(R.id.et_main_diameter2);
final EditText diameterET3 = findViewById(R.id.et_main_diameter3);
final EditText diameterET4 = findViewById(R.id.et_main_diameter4);
final EditText diameterET5 = findViewById(R.id.et_main_diameter5);
Button calculateButton = findViewById(R.id.btn_main_calculate);
calculateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
float Le1 = 0;
if (lengthET1.length() > 0) {
String L1 = lengthET1.getText().toString();
Le1 = Float.parseFloat(L1);
}
float Di1 = 0;
if (diameterET1.length() > 0) {
String D1 = diameterET1.getText().toString();
Di1 = Float.parseFloat(D1);
}
float Le2 = 0;
if (lengthET2.length() > 0) {
String L2 = lengthET2.getText().toString();
Le2 = Float.parseFloat(L2);
}
float Di2 = 0;
if (diameterET2.length() > 0) {
String D2 = diameterET2.getText().toString();
Di2 = Float.parseFloat(D2);
}
float Le3 = 0;
if (lengthET3.length() > 0) {
String L3 = lengthET3.getText().toString();
Le3 = Float.parseFloat(L3);
}
float Di3 = 0;
if (diameterET3.length() > 0) {
String D3 = diameterET3.getText().toString();
Di3 = Float.parseFloat(D3);
}
float Le4 = 0;
if (lengthET4.length() > 0) {
String L4 = lengthET4.getText().toString();
Le4 = Float.parseFloat(L4);
}
float Di4 = 0;
if (diameterET4.length() > 0) {
String D4 = diameterET4.getText().toString();
Di4 = Float.parseFloat(D4);
}
float Le5 = 0;
if (lengthET5.length() > 0) {
String L5 = lengthET5.getText().toString();
Le5 = Float.parseFloat(L5);
}
float Di5 = 0;
if (diameterET5.length() > 0) {
String D5 = diameterET5.getText().toString();
Di5 = Float.parseFloat(D5);
}
final float Surface1 = (float) (Le1 * Di1 * Math.PI);
final float Surface2 = (float) (Le2 * Di2 * Math.PI);
final float Surface3 = (float) (Le3 * Di3 * Math.PI);
final float Surface4 = (float) (Le4 * Di4 * Math.PI);
final float Surface5 = (float) (Le5 * Di5 * Math.PI);
final float Surface = Surface1 + Surface2 + Surface3 + Surface4 + Surface5;
long projectID = -1;
Intent intent = new Intent(MainActivity.this, IntensityActivity.class);
if (Surface == 0) {
Toast.makeText(MainActivity.this, "No numbers are entered", Toast.LENGTH_SHORT).show();
} else {
intent.putExtra("Result", Surface);
intent.putExtra("IE_PROJECTID",projectID);
startActivity(intent);
}
PersonalInfoSharedPrefManager manager = new PersonalInfoSharedPrefManager(MainActivity.this);
manager.setSuface(Surface);
Project project = new Project();
project.setId(mProjectID);
project.setLength1(lengthET1.getText().toString());
project.setDiameter1(diameterET1.getText().toString());
project.setLength2(lengthET2.getText().toString());
project.setDiameter2(diameterET2.getText().toString());
project.setLength3(lengthET3.getText().toString());
project.setDiameter3(diameterET3.getText().toString());
project.setLength4(lengthET4.getText().toString());
project.setDiameter4(diameterET4.getText().toString());
project.setLength5(lengthET5.getText().toString());
project.setDiameter5(diameterET5.getText().toString());
project.setSurface(String.valueOf(Surface));
projectDAO.updateProject(project);
}
});
}
}
IntensityActivity:
public class IntensityActivity extends AppCompatActivity {
private ProjectDAO projectDAO;
private long mProjectID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intensity);
projectDAO = DBInjector.provideProjectDao(this);
mProjectID = getIntent().getLongExtra("IE_PROJECTID",0);
final float Result = getIntent().getFloatExtra("Result",0);
final Button resistanceBTN1 = findViewById(R.id.btn_intensity_R1);
final Button resistanceBTN2 = findViewById(R.id.btn_intensity_R2);
final Button resistanceBTN3 = findViewById(R.id.btn_intensity_R3);
final Button resistanceBTN4 = findViewById(R.id.btn_intensity_R4);
final Button resistanceBTN5 = findViewById(R.id.btn_intensity_R5);
final View RL = findViewById(R.id.rl_intensity_allowedIntensity);
final TextView allowedResistance = findViewById(R.id.tv_intensity_allowedIntensityNumber);
final TextView surfaceNumber = findViewById(R.id.tv_intensity_surfaceNumber);
final EditText intensityNumber = findViewById(R.id.et_intensity_intensityNumber);
Button calculateButton = findViewById(R.id.btn_intensity_calculate);
final Button goToVoltageButton = findViewById(R.id.btn_intensity_goToVoltage);
final TextView formulaResultNumber = findViewById(R.id.tv_intensity_resultNumber);
String Sur = Float.toString(Result);
surfaceNumber.setText(Sur);
final double R1 = Result*0.250;
final double R2 = Result*0.125;
final double R3 = Result*0.050;
final double R4 = Result*0.025;
final double R5 = Result*0.010;
final String Re1 = Double.toString(R1);
final String Re2 = Double.toString(R2);
final String Re3 = Double.toString(R3);
final String Re4 = Double.toString(R4);
final String Re5 = Double.toString(R5);
resistanceBTN1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RL.setVisibility(View.VISIBLE);
allowedResistance.setText(Re1);
}
});
resistanceBTN2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RL.setVisibility(View.VISIBLE);
allowedResistance.setText(Re2);
}
});
resistanceBTN3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RL.setVisibility(View.VISIBLE);
allowedResistance.setText(Re3);
}
});
resistanceBTN4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RL.setVisibility(View.VISIBLE);
allowedResistance.setText(Re4);
}
});
resistanceBTN5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RL.setVisibility(View.VISIBLE);
allowedResistance.setText(Re5);
}
});
calculateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String I = intensityNumber.getText().toString();
float In = Float.parseFloat(I);
float R = In/Result;
String Res = String.valueOf(R);
formulaResultNumber.setText(Res);
goToVoltageButton.setVisibility(View.VISIBLE);
PersonalInfoSharedPrefManager manager = new PersonalInfoSharedPrefManager(IntensityActivity.this);
manager.setIntensity(In);
}
});
goToVoltageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(IntensityActivity.this,SimpleVoltage.class);
startActivity(intent);
Project project = new Project();
project.setId(mProjectID);
project.setAllowedIntensity(String.valueOf(allowedResistance));
project.setIntensity(intensityNumber.getText().toString());
projectDAO.updateProject(project);
}
});
}
}
答案 0 :(得分:1)
从MainActivity
到IntensityActivity
,我发现你总是在项目ID中传递-1
final float Surface = Surface1 + Surface2 + Surface3 + Surface4 + Surface5;
long projectID = -1;
Intent intent = new Intent(MainActivity.this, IntensityActivity.class);
if (Surface == 0) {
Toast.makeText(MainActivity.this, "No numbers are entered", Toast.LENGTH_SHORT).show();
} else {
intent.putExtra("Result", Surface);
intent.putExtra("IE_PROJECTID",projectID);
startActivity(intent);
}
相反,您可以从Intent
传递此活动所获得的相同ID只需在MainActivity
中更新您的代码,如下所示,然后传递mProjectID
。
final float Surface = Surface1 + Surface2 + Surface3 + Surface4 + Surface5;
long projectID = -1;
Intent intent = new Intent(MainActivity.this, IntensityActivity.class);
if (Surface == 0) {
Toast.makeText(MainActivity.this, "No numbers are entered", Toast.LENGTH_SHORT).show();
} else {
intent.putExtra("Result", Surface);
intent.putExtra("IE_PROJECTID",mProjectID);
startActivity(intent);
}
希望这会有所帮助。
答案 1 :(得分:0)
在您的更新方法中尝试这种方式,
db.update("tbl_project_info", contentValues, "id="+String.valueOf(project.getId()), null);
您还需要将以下行更改为MainActivity.java
long projectID = -1;
到
long projectID = mProjectID;