如何解决这个错误"cannot resolve method 'addData(java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String)'"?
How to solve this error "cannot resolve method 'addData(java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String)'"?
我不知道为什么我不能添加 boolean insertData = dbHelper.addData(emailString, nameString,ageString, preferencesString,passwordString)
,因为 addData 在下面提供的代码行中一直显示错误。我想添加 if-else 语句是因为我希望它确保在插入数据过程中,用户必须填写所有详细信息才能创建帐户,但不能在给定的任何字段上留空让他们填写。还是我需要以其他方式填写?我仍然是 android 工作室的新手。请帮我。
private void getData() {
name = "" + Pname.getText().toString().trim();
age = "" + PAge.getText().toString().trim();
phone = "" + Pphone.getText().toString().trim();
preferenceselected = "" + Ppreferenceselected.getText().toString().trim();
String timeStamp = "" + System.currentTimeMillis();
long id = dbHelper.insertInfo(
"" + imageUri,
"" + name,
"" + age,
"" + phone,
"" + preferenceselected,
"" + timeStamp,
"" + timeStamp
);
String emailString = Pemail.getText().toString();
String nameString = Pname.getText().toString();
String ageString = PAge.getText().toString();
String preferencesString = Ppreferenceselected.getText().toString();
String passwordString = Ppassword.getText().toString();
boolean insertData = dbHelper.addData(emailString, nameString,ageString, preferencesString,passwordString);
if (insertData == true) {
Toast.makeText(MainActivity.this, "Account Created", Toast.LENGTH_LONG).show();
Intent goappsetting = new Intent(MainActivity.this,setting.class);
startActivity(goappsetting);
}else {
Toast.makeText(MainActivity.this, "Please do not leave any blanks or user exist", Toast.LENGTH_LONG).show();
}
Toast.makeText(this, "Account Created", Toast.LENGTH_SHORT).show();
}
这是我的MainActivity.Java
public class MainActivity extends AppCompatActivity {
private DatabaseHelper dbHelper;
private ImageView pImageView;
private EditText Pemail, Pname, PAge, Pphone, Ppassword;
private TextView loginacclink, Ppreferenceselected;
Button createacc, preferencebtn;
ActionBar actionBar;
String[] categories;
boolean[] checkeditems;
ArrayList<Integer> mUserItems = new ArrayList<>();
private static final int CAMERA_REQUEST_CODE = 100;
private static final int STORAGE_REQUEST_CODE = 101;
private static final int IMAGE_PICK_CAMERA_CODE = 102;
private static final int IMAGE_PICK_GALLERY_CODE = 103;
private String[] cameraPermissions;
private String[] storagePermissions;
private Uri imageUri;
private String name, age, phone;
private String preferenceselected;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// this is the status bar
actionBar = getSupportActionBar();
actionBar.setTitle("Create Account");
// this is for the check list
categories = getResources().getStringArray(R.array.user_categories);
checkeditems = new boolean[categories.length];
// this is for the items in the xml file
pImageView = findViewById(R.id.personImage);
Pemail = findViewById(R.id.email);
Pname = findViewById(R.id.name);
PAge = findViewById(R.id.age);
Pphone = findViewById(R.id.phone);
Ppassword = findViewById(R.id.password);
createacc = findViewById(R.id.createacc);
preferencebtn = findViewById(R.id.preferencebtn);
loginacclink = findViewById(R.id.loginacclink);
Ppreferenceselected = (TextView) findViewById(R.id.preferenceselected);
dbHelper = new DatabaseHelper(this);
cameraPermissions = new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
storagePermissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE};
pImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imagePickDialog();
}
});
preferencebtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick (View view){
AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
mBuilder.setTitle(R.string.dialog_title);
mBuilder.setMultiChoiceItems(categories, checkeditems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int position, boolean isChecked) {
if (isChecked) {
if (!mUserItems.contains(position)) {
mUserItems.add(position);
}
} else if (mUserItems.contains(position)) {
mUserItems.remove(position);
}
}
});
mBuilder.setCancelable(false);
mBuilder.setPositiveButton(R.string.ok_label, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
String item = "";
for (int i = 0; i < mUserItems.size(); i++) {
item = item + categories[mUserItems.get(i)];
if (i != mUserItems.size() - 1) {
item = item + ",";
}
}
Ppreferenceselected.setText(item);
}
});
mBuilder.setNegativeButton(R.string.dismiss_label, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
mBuilder.setNeutralButton(R.string.clear_all_label, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
for (int i = 0; i < checkeditems.length; i++) {
checkeditems[i] = false;
mUserItems.clear();
Ppreferenceselected.setText("");
}
}
});
AlertDialog mDialog = mBuilder.create();
mDialog.show();
}
});
loginacclink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, login.class);
startActivity(intent); }
});
createacc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// insert data into db
getData();
}
});
}
private void getData() {
name = "" + Pname.getText().toString().trim();
age = "" + PAge.getText().toString().trim();
phone = "" + Pphone.getText().toString().trim();
preferenceselected = "" + Ppreferenceselected.getText().toString().trim();
String timeStamp = "" + System.currentTimeMillis();
long id = dbHelper.insertInfo(
"" + imageUri,
"" + name,
"" + age,
"" + phone,
"" + preferenceselected,
"" + timeStamp,
"" + timeStamp
);
String emailString = Pemail.getText().toString();
String nameString = Pname.getText().toString();
String ageString = PAge.getText().toString();
String preferencesString = Ppreferenceselected.getText().toString();
String passwordString = Ppassword.getText().toString();
boolean insertData = dbHelper.addData(emailString, nameString,ageString, preferencesString,passwordString);
if (insertData == true) {
Toast.makeText(MainActivity.this, "Account Created", Toast.LENGTH_LONG).show();
Intent goappsetting = new Intent(MainActivity.this,setting.class);
startActivity(goappsetting);
}else {
Toast.makeText(MainActivity.this, "Please do not leave any blanks or user exist", Toast.LENGTH_LONG).show();
}
Toast.makeText(this, "Account Created", Toast.LENGTH_SHORT).show();
}
private void imagePickDialog() {
String[] options = {"Camera", "Gallery"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick Image From");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == 0) {
if (!checkCameraPermission()) {
requestCameraPermission();
}
else {
pickFromCamera();
}
}
else if (which == 1) {
if (!checkStoragePermission()) {
requestStoragePermission();
}
else {
pickFromGallery();
}
}
}
});
builder.create().show();
}
private boolean checkCameraPermission() {
boolean result = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
== (PackageManager.PERMISSION_GRANTED);
boolean result1 = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
== (PackageManager.PERMISSION_GRANTED);
return result && result1;
}
private void requestCameraPermission() {
ActivityCompat.requestPermissions(this, cameraPermissions, CAMERA_REQUEST_CODE);
}
private void pickFromGallery() {
Intent galleryIntent = new Intent(Intent.ACTION_PICK);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent, IMAGE_PICK_GALLERY_CODE);
}
private void pickFromCamera() {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "Image title");
values.put(MediaStore.Images.Media.DESCRIPTION, "Image description");
imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(cameraIntent, IMAGE_PICK_CAMERA_CODE);
}
private boolean checkStoragePermission() {
boolean result = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
== (PackageManager.PERMISSION_GRANTED);
return result;
}
private void requestStoragePermission() {
ActivityCompat.requestPermissions(this, storagePermissions, STORAGE_REQUEST_CODE);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case CAMERA_REQUEST_CODE: {
if (grantResults.length>0) {
boolean cameraAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
boolean storageAccepted = grantResults[1] == PackageManager.PERMISSION_GRANTED;
if (cameraAccepted && storageAccepted) {
pickFromCamera();
}
else {
Toast.makeText(this, "Camera permission required!", Toast.LENGTH_SHORT).show();
}
}
}
break;
case STORAGE_REQUEST_CODE:{
if (grantResults.length>0) {
boolean storageAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
if (storageAccepted) {
pickFromGallery();
}
else {
Toast.makeText(this, "Storage permission required!", Toast.LENGTH_SHORT).show();
}
}
}
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == IMAGE_PICK_GALLERY_CODE) {
CropImage.activity(data.getData())
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1, 1)
.start(this);
}
else if (requestCode == IMAGE_PICK_CAMERA_CODE) {
CropImage.activity(imageUri)
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1, 1)
.start(this);
}
else if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri resultUri = result.getUri();
imageUri = resultUri;
pImageView.setImageURI(resultUri);
}
else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
Toast.makeText(this, ""+error, Toast.LENGTH_SHORT).show();
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
这是我的DatabaseHelper.Java
public class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(@Nullable Context context) {
super(context, constants.DB_NAME, null, constants.DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(constants.CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + constants.TABLE_NAME);
onCreate(db);
}
public long insertInfo(String image, String name, String age, String phone, String preference, String addTimeStamp, String updateTimeStamp) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(constants.C_NAME, name);
values.put(constants.C_AGE, age);
values.put(constants.C_PHONE, phone);
values.put(constants.C_PREFERENCES, preference);
values.put(constants.C_IMAGE, image);
values.put(constants.C_Add_TIMESTAMP, addTimeStamp);
values.put(constants.C_UPDATED_TIMESTAMP, updateTimeStamp);
long id = db.insert(constants.TABLE_NAME, null, values);
db.close();
return id;
}
}
这是constance.Java
public class constants {
public static final String DB_NAME = "MMTA_DB";
public static final int DB_VERSION = 1;
public static final String TABLE_NAME = "USER_TABLE";
public static final String C_EMAIL = "EMAIL";
public static final String C_NAME = "NAME";
public static final String C_AGE = "AGE";
public static final String C_PHONE = "PHONE";
public static final String C_PASSWORD = "PASSWORD";
public static final String C_PREFERENCES = "PREFERENCES";
public static final String C_IMAGE = "IMAGE";
public static final String C_Add_TIMESTAMP = "TIMESTAMP";
public static final String C_UPDATED_TIMESTAMP = "UPDATED_TIMESTAMP";
public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " ("
+ C_EMAIL + " TEXT PRIMARY KEY ,"
+ C_NAME + " TEXT,"
+ C_AGE + " TEXT,"
+ C_PHONE + " TEXT,"
+ C_PASSWORD + " TEXT,"
+ C_PREFERENCES + " TEXT,"
+ C_IMAGE + " TEXT,"
+ C_Add_TIMESTAMP + " TEXT,"
+ C_UPDATED_TIMESTAMP + " TEXT"
+ ");";
}
DatabaseHelper
class 中缺少方法 addData(String,String,String,String,String)
。请在 class.
中添加此方法
更新 DatabaseHelper
class.
public class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(@Nullable Context context) {
super(context, constants.DB_NAME, null, constants.DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(constants.CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + constants.TABLE_NAME);
onCreate(db);
}
public long insertInfo(String image, String name, String age, String phone, String preference, String addTimeStamp, String updateTimeStamp) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(constants.C_NAME, name);
values.put(constants.C_AGE, age);
values.put(constants.C_PHONE, phone);
values.put(constants.C_PREFERENCES, preference);
values.put(constants.C_IMAGE, image);
values.put(constants.C_Add_TIMESTAMP, addTimeStamp);
values.put(constants.C_UPDATED_TIMESTAMP, updateTimeStamp);
long id = db.insert(constants.TABLE_NAME, null, values);
db.close();
return id;
}
//add the method
public boolean addData(String parameter1,String parameter2,String parameter3,String parameter4,String parameter5) {
// put here your logic
return false;
}
}
我不知道为什么我不能添加 boolean insertData = dbHelper.addData(emailString, nameString,ageString, preferencesString,passwordString)
,因为 addData 在下面提供的代码行中一直显示错误。我想添加 if-else 语句是因为我希望它确保在插入数据过程中,用户必须填写所有详细信息才能创建帐户,但不能在给定的任何字段上留空让他们填写。还是我需要以其他方式填写?我仍然是 android 工作室的新手。请帮我。
private void getData() {
name = "" + Pname.getText().toString().trim();
age = "" + PAge.getText().toString().trim();
phone = "" + Pphone.getText().toString().trim();
preferenceselected = "" + Ppreferenceselected.getText().toString().trim();
String timeStamp = "" + System.currentTimeMillis();
long id = dbHelper.insertInfo(
"" + imageUri,
"" + name,
"" + age,
"" + phone,
"" + preferenceselected,
"" + timeStamp,
"" + timeStamp
);
String emailString = Pemail.getText().toString();
String nameString = Pname.getText().toString();
String ageString = PAge.getText().toString();
String preferencesString = Ppreferenceselected.getText().toString();
String passwordString = Ppassword.getText().toString();
boolean insertData = dbHelper.addData(emailString, nameString,ageString, preferencesString,passwordString);
if (insertData == true) {
Toast.makeText(MainActivity.this, "Account Created", Toast.LENGTH_LONG).show();
Intent goappsetting = new Intent(MainActivity.this,setting.class);
startActivity(goappsetting);
}else {
Toast.makeText(MainActivity.this, "Please do not leave any blanks or user exist", Toast.LENGTH_LONG).show();
}
Toast.makeText(this, "Account Created", Toast.LENGTH_SHORT).show();
}
这是我的MainActivity.Java
public class MainActivity extends AppCompatActivity {
private DatabaseHelper dbHelper;
private ImageView pImageView;
private EditText Pemail, Pname, PAge, Pphone, Ppassword;
private TextView loginacclink, Ppreferenceselected;
Button createacc, preferencebtn;
ActionBar actionBar;
String[] categories;
boolean[] checkeditems;
ArrayList<Integer> mUserItems = new ArrayList<>();
private static final int CAMERA_REQUEST_CODE = 100;
private static final int STORAGE_REQUEST_CODE = 101;
private static final int IMAGE_PICK_CAMERA_CODE = 102;
private static final int IMAGE_PICK_GALLERY_CODE = 103;
private String[] cameraPermissions;
private String[] storagePermissions;
private Uri imageUri;
private String name, age, phone;
private String preferenceselected;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// this is the status bar
actionBar = getSupportActionBar();
actionBar.setTitle("Create Account");
// this is for the check list
categories = getResources().getStringArray(R.array.user_categories);
checkeditems = new boolean[categories.length];
// this is for the items in the xml file
pImageView = findViewById(R.id.personImage);
Pemail = findViewById(R.id.email);
Pname = findViewById(R.id.name);
PAge = findViewById(R.id.age);
Pphone = findViewById(R.id.phone);
Ppassword = findViewById(R.id.password);
createacc = findViewById(R.id.createacc);
preferencebtn = findViewById(R.id.preferencebtn);
loginacclink = findViewById(R.id.loginacclink);
Ppreferenceselected = (TextView) findViewById(R.id.preferenceselected);
dbHelper = new DatabaseHelper(this);
cameraPermissions = new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
storagePermissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE};
pImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imagePickDialog();
}
});
preferencebtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick (View view){
AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
mBuilder.setTitle(R.string.dialog_title);
mBuilder.setMultiChoiceItems(categories, checkeditems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int position, boolean isChecked) {
if (isChecked) {
if (!mUserItems.contains(position)) {
mUserItems.add(position);
}
} else if (mUserItems.contains(position)) {
mUserItems.remove(position);
}
}
});
mBuilder.setCancelable(false);
mBuilder.setPositiveButton(R.string.ok_label, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
String item = "";
for (int i = 0; i < mUserItems.size(); i++) {
item = item + categories[mUserItems.get(i)];
if (i != mUserItems.size() - 1) {
item = item + ",";
}
}
Ppreferenceselected.setText(item);
}
});
mBuilder.setNegativeButton(R.string.dismiss_label, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
mBuilder.setNeutralButton(R.string.clear_all_label, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
for (int i = 0; i < checkeditems.length; i++) {
checkeditems[i] = false;
mUserItems.clear();
Ppreferenceselected.setText("");
}
}
});
AlertDialog mDialog = mBuilder.create();
mDialog.show();
}
});
loginacclink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, login.class);
startActivity(intent); }
});
createacc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// insert data into db
getData();
}
});
}
private void getData() {
name = "" + Pname.getText().toString().trim();
age = "" + PAge.getText().toString().trim();
phone = "" + Pphone.getText().toString().trim();
preferenceselected = "" + Ppreferenceselected.getText().toString().trim();
String timeStamp = "" + System.currentTimeMillis();
long id = dbHelper.insertInfo(
"" + imageUri,
"" + name,
"" + age,
"" + phone,
"" + preferenceselected,
"" + timeStamp,
"" + timeStamp
);
String emailString = Pemail.getText().toString();
String nameString = Pname.getText().toString();
String ageString = PAge.getText().toString();
String preferencesString = Ppreferenceselected.getText().toString();
String passwordString = Ppassword.getText().toString();
boolean insertData = dbHelper.addData(emailString, nameString,ageString, preferencesString,passwordString);
if (insertData == true) {
Toast.makeText(MainActivity.this, "Account Created", Toast.LENGTH_LONG).show();
Intent goappsetting = new Intent(MainActivity.this,setting.class);
startActivity(goappsetting);
}else {
Toast.makeText(MainActivity.this, "Please do not leave any blanks or user exist", Toast.LENGTH_LONG).show();
}
Toast.makeText(this, "Account Created", Toast.LENGTH_SHORT).show();
}
private void imagePickDialog() {
String[] options = {"Camera", "Gallery"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick Image From");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == 0) {
if (!checkCameraPermission()) {
requestCameraPermission();
}
else {
pickFromCamera();
}
}
else if (which == 1) {
if (!checkStoragePermission()) {
requestStoragePermission();
}
else {
pickFromGallery();
}
}
}
});
builder.create().show();
}
private boolean checkCameraPermission() {
boolean result = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
== (PackageManager.PERMISSION_GRANTED);
boolean result1 = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
== (PackageManager.PERMISSION_GRANTED);
return result && result1;
}
private void requestCameraPermission() {
ActivityCompat.requestPermissions(this, cameraPermissions, CAMERA_REQUEST_CODE);
}
private void pickFromGallery() {
Intent galleryIntent = new Intent(Intent.ACTION_PICK);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent, IMAGE_PICK_GALLERY_CODE);
}
private void pickFromCamera() {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "Image title");
values.put(MediaStore.Images.Media.DESCRIPTION, "Image description");
imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(cameraIntent, IMAGE_PICK_CAMERA_CODE);
}
private boolean checkStoragePermission() {
boolean result = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
== (PackageManager.PERMISSION_GRANTED);
return result;
}
private void requestStoragePermission() {
ActivityCompat.requestPermissions(this, storagePermissions, STORAGE_REQUEST_CODE);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case CAMERA_REQUEST_CODE: {
if (grantResults.length>0) {
boolean cameraAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
boolean storageAccepted = grantResults[1] == PackageManager.PERMISSION_GRANTED;
if (cameraAccepted && storageAccepted) {
pickFromCamera();
}
else {
Toast.makeText(this, "Camera permission required!", Toast.LENGTH_SHORT).show();
}
}
}
break;
case STORAGE_REQUEST_CODE:{
if (grantResults.length>0) {
boolean storageAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
if (storageAccepted) {
pickFromGallery();
}
else {
Toast.makeText(this, "Storage permission required!", Toast.LENGTH_SHORT).show();
}
}
}
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == IMAGE_PICK_GALLERY_CODE) {
CropImage.activity(data.getData())
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1, 1)
.start(this);
}
else if (requestCode == IMAGE_PICK_CAMERA_CODE) {
CropImage.activity(imageUri)
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1, 1)
.start(this);
}
else if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri resultUri = result.getUri();
imageUri = resultUri;
pImageView.setImageURI(resultUri);
}
else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
Toast.makeText(this, ""+error, Toast.LENGTH_SHORT).show();
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
这是我的DatabaseHelper.Java
public class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(@Nullable Context context) {
super(context, constants.DB_NAME, null, constants.DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(constants.CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + constants.TABLE_NAME);
onCreate(db);
}
public long insertInfo(String image, String name, String age, String phone, String preference, String addTimeStamp, String updateTimeStamp) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(constants.C_NAME, name);
values.put(constants.C_AGE, age);
values.put(constants.C_PHONE, phone);
values.put(constants.C_PREFERENCES, preference);
values.put(constants.C_IMAGE, image);
values.put(constants.C_Add_TIMESTAMP, addTimeStamp);
values.put(constants.C_UPDATED_TIMESTAMP, updateTimeStamp);
long id = db.insert(constants.TABLE_NAME, null, values);
db.close();
return id;
}
}
这是constance.Java
public class constants {
public static final String DB_NAME = "MMTA_DB";
public static final int DB_VERSION = 1;
public static final String TABLE_NAME = "USER_TABLE";
public static final String C_EMAIL = "EMAIL";
public static final String C_NAME = "NAME";
public static final String C_AGE = "AGE";
public static final String C_PHONE = "PHONE";
public static final String C_PASSWORD = "PASSWORD";
public static final String C_PREFERENCES = "PREFERENCES";
public static final String C_IMAGE = "IMAGE";
public static final String C_Add_TIMESTAMP = "TIMESTAMP";
public static final String C_UPDATED_TIMESTAMP = "UPDATED_TIMESTAMP";
public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " ("
+ C_EMAIL + " TEXT PRIMARY KEY ,"
+ C_NAME + " TEXT,"
+ C_AGE + " TEXT,"
+ C_PHONE + " TEXT,"
+ C_PASSWORD + " TEXT,"
+ C_PREFERENCES + " TEXT,"
+ C_IMAGE + " TEXT,"
+ C_Add_TIMESTAMP + " TEXT,"
+ C_UPDATED_TIMESTAMP + " TEXT"
+ ");";
}
DatabaseHelper
class 中缺少方法 addData(String,String,String,String,String)
。请在 class.
更新 DatabaseHelper
class.
public class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(@Nullable Context context) {
super(context, constants.DB_NAME, null, constants.DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(constants.CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + constants.TABLE_NAME);
onCreate(db);
}
public long insertInfo(String image, String name, String age, String phone, String preference, String addTimeStamp, String updateTimeStamp) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(constants.C_NAME, name);
values.put(constants.C_AGE, age);
values.put(constants.C_PHONE, phone);
values.put(constants.C_PREFERENCES, preference);
values.put(constants.C_IMAGE, image);
values.put(constants.C_Add_TIMESTAMP, addTimeStamp);
values.put(constants.C_UPDATED_TIMESTAMP, updateTimeStamp);
long id = db.insert(constants.TABLE_NAME, null, values);
db.close();
return id;
}
//add the method
public boolean addData(String parameter1,String parameter2,String parameter3,String parameter4,String parameter5) {
// put here your logic
return false;
}
}