我正在尝试使用另一个Java类更新MainActivity中的SQLite。 我正在使用Intent,但我有这个错误。 当我搜索时,我意识到使用OnCreate我可以采取意图,但我有一个简单的Java类。
在我的MainActivity中,我以这种方式发送它,这似乎没有任何问题:
btnupdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calisan calisan = new Calisan();
if (Util.editTextControl(txtFirstNameUpdate_New)) {
if (Util.editTextControl(txtFirstNameUpdate_New)) {
calisan.setAd(txtFirstNameUpdate_New.getText().toString());
calisan.setSoyad(txtLastNameUpdate_New.getText().toString());
} else {
Util.showMessage(MainActivity.this, "Yeni Soyisim Alanı Boş Bırakılamaz.");
}
} else {
Util.showMessage(MainActivity.this, "Yeni İsim Alanı Boş Bırakılamaz.");
}
Intent intent = new Intent (MainActivity.this, DBContext.class );
intent.putExtra("ID", txtUpdatedID.getText().toString());
intent.putExtra ( "FirstNameUpdate", txtFirstNameUpdate_New.getText().toString());
intent.putExtra("LastNameUpdate", txtLastNameUpdate_New.getText().toString());
startActivity(intent);
boolean sonuc = DBContext.getInstance(MainActivity.this).updateEmployees(calisan.getId(), calisan.getAd(), calisan.getSoyad());
Util.showMessage(MainActivity.this, sonuc ? "Calisan Eklendi" : "Calısan Eklenemedi");
}
});
在第二个活动中:
public final class DBContext {
public boolean updateEmployees(long guncellenicekID, String guncellenecekAd, String guncellenicekSoyad){
boolean result = false;
//This is where I get error: cannot find symbol method
//getIntent()
Intent i = getIntent();
String updatedFirstName = i.getStringExtra("FirstNameUpdate");
String updatedLastName = i.getStringExtra("LastNameUpdate");
int ID = i.getIntExtra("ID",0);
SQLiteDatabase database = dbHelper.getWritableDatabase();
ContentValues cvUpdate = new ContentValues();
cvUpdate.put(DBHelper.COLUMN_FIRST_NAME, updatedFirstName);
cvUpdate.put(DBHelper.COLUMN_LAST_NAME, updatedLastName);
long id = database.update(DBHelper.TABLE_NAME, cvUpdate, DBHelper.COLUMN_ID + "=" + guncellenicekID, null);
if (id != -1){
result = true;
}
return result;
}
谢谢!
答案 0 :(得分:1)
这是因为您正在尝试使用DBContext获取intent(),而DBContext不是Activity,也不是activity的子类。请阅读getIntent()。
您需要让您的方法接受Intent作为参数,如下所示:
public final class DBContext {
...
public boolean updateEmployees(long guncellenicekID, String guncellenecekAd, String guncellenicekSoyad, Intent intent){
...
}
}
但不应使用上述代码。你不应该让你的DBContext
知道存在意图。您应该让DbContext执行其工作,即数据库操作,并忽略与其无关的所有内容。
您可以将方法更改为以下内容:
public final class DBContext {
...
// pass all the data via the parameters.
public boolean updateEmployees(long guncellenicekID, String guncellenecekAd, String guncellenicekSoyad, String updatedFirstName, String updatedLastName, int id){
...
}
}
虽然看起来很乱。
您还可以将所有参数设为pojo,然后将其作为updateEmployees
参数传递。首先,创建pojo:
// Pojo
public class Employee {
String guncellenicekID;
String guncellenecekAd;
String guncellenicekSoyad;
String updatedFirstName;
String updatedLastName;
int id;
// Constructor
// setter, you need to create it.
// getter, you need to create it.
}
的DbContext:
public final class DBContext {
...
// pass all the data via the parameters.
public boolean updateEmployees(Employee employee){
...
ContentValues cvUpdate = new ContentValues();
cvUpdate.put(DBHelper.COLUMN_FIRST_NAME, employee.getUpdatedFirstName());
cvUpdate.put(DBHelper.COLUMN_LAST_NAME, employee.getUpdatedLastName());
long id = database.update(DBHelper.TABLE_NAME, cvUpdate, DBHelper.COLUMN_ID + "=" + employee.getGuncellenicekID(), null);
...
}
}
然后你可以用这样的方法调用方法:
Employee employee = new Employee();
employee.setUpdatedFirstName("sampleFirstName");
employee.setUpdatedLastName("sampleLastName");
// set the other data
...
DbContext.getInstance().updateEmployees(employee);