- gridView.setOnItemLongClickListener calls showDialogUpdate.
- showDialogUpdate uses setContentView to R.layout.update_activity
- Inside update_activity layout I have 3 EditText (edtName, edtUsername and edtPassword) which I want to be populated when the dialog is shown
Thanks for the help.
public class FoodList extends AppCompatActivity {
final String versionName = BuildConfig.VERSION_NAME;
GridView gridView;
ArrayList<Food> list;
FoodListAdapter adapter = null;
ActivityResultLauncher<String> mTakePhotoList;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_list);
mTakePhotoList = registerForActivityResult(new ActivityResultContracts.GetContent(), new ActivityResultCallback<Uri>() {
@Override
public void onActivityResult(Uri result) {
imageViewFood.setImageURI(result);
}
});
gridView = findViewById(R.id.gridView);
list = new ArrayList<>();
adapter = new FoodListAdapter(this, R.layout.items, list);
gridView.setAdapter(adapter);
try {
// get all data from sqlite
Cursor cursor = MainActivity.sqLiteHelper.getData("SELECT * FROM DATA");
list.clear();
while (cursor.moveToNext()) {
int id = cursor.getInt(0);
String name = cursor.getString(1);
String username = cursor.getString(2);
String password = cursor.getString(3);
byte[] image = cursor.getBlob(4);
list.add(new Food(name, username, password, image));
}
adapter.notifyDataSetChanged();
MainActivity.sqLiteHelper.close();
}
catch (Exception error) {
Log.e("Update error", error.getMessage());
}
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String clickedText1 = list.get(position).getName();
String clickedText2 = list.get(position).getUsername();
String clickedText3 = list.get(position).getPassword();
androidx.appcompat.app.AlertDialog.Builder builder = new androidx.appcompat.app.AlertDialog.Builder(FoodList.this, R.style.CustomAlertDialog);
builder.setCancelable(true);
builder.setTitle(clickedText1);
builder.setMessage("Username: " + clickedText2 + "\n" + "Password: " + clickedText3);
builder.setIcon(R.mipmap.my_pass_small);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
});
gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
CharSequence[] items = {"Update", "Delete"};
AlertDialog.Builder dialog = new AlertDialog.Builder(FoodList.this, R.style.CustomAlertDialog);
dialog.setIcon(R.mipmap.my_pass_small);
dialog.setTitle("Choose an Action");
dialog.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Update")) {
// update
Cursor c = MainActivity.sqLiteHelper.getData("SELECT id FROM DATA");
ArrayList<Integer> arrID = new ArrayList<>();
while (c.moveToNext()){
arrID.add(c.getInt(0));
}
// show dialog update at here
showDialogUpdate(FoodList.this, arrID.get(position));
} else {
// delete
Cursor c = MainActivity.sqLiteHelper.getData("SELECT id FROM DATA");
ArrayList<Integer> arrID = new ArrayList<>();
while (c.moveToNext()){
arrID.add(c.getInt(0));
}
showDialogDelete(arrID.get(position));
}
}
});
dialog.show();
return true;
}
});
MainActivity.sqLiteHelper.close();
}
ImageView imageViewFood;
private void showDialogUpdate(Activity activity, final int position){
final Dialog dialog = new Dialog(activity);
dialog.setContentView(R.layout.update_activity);
dialog.setTitle("");
imageViewFood = dialog.findViewById(R.id.imageViewFood);
final EditText edtName = dialog.findViewById(R.id.edtName);
final EditText edtUsername = dialog.findViewById(R.id.edtUsername);
final EditText edtPassword = dialog.findViewById(R.id.edtPassword);
Button btnUpdate = dialog.findViewById(R.id.btnUpdate);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
MainActivity.sqLiteHelper.close();
imageViewFood.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// request photo library
ActivityCompat.requestPermissions(
FoodList.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
888
);
}
});
btnUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
MainActivity.sqLiteHelper.updateData(
edtName.getText().toString().trim(),
edtUsername.getText().toString().trim(),
edtPassword.getText().toString().trim(),
MainActivity.imageViewToByte(imageViewFood),
position
);
dialog.dismiss();
Toast.makeText(getApplicationContext(), "Updated successfully!",Toast.LENGTH_SHORT).show();
}
catch (Exception error) {
Log.e("Update error", error.getMessage());
}
updateFoodList();
}
});
}
private void showDialogDelete(final int idFood){
final AlertDialog.Builder dialogDelete = new AlertDialog.Builder(FoodList.this, R.style.CustomAlertDialog);
dialogDelete.setIcon(R.mipmap.my_pass_small);
dialogDelete.setTitle("Warning!");
dialogDelete.setMessage("Are you sure you want to delete this account?");
dialogDelete.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
try {
MainActivity.sqLiteHelper.deleteData(idFood);
Toast.makeText(getApplicationContext(), "Deleted successfully!",Toast.LENGTH_SHORT).show();
} catch (Exception e){
Log.e("Error", e.getMessage());
}
updateFoodList();
MainActivity.sqLiteHelper.close();
}
});
dialogDelete.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialogDelete.show();
}
private void updateFoodList(){
try {
// get all data from sqlite
Cursor cursor = MainActivity.sqLiteHelper.getData("SELECT * FROM DATA");
list.clear();
while (cursor.moveToNext()) {
cursor.getInt(0);
String name = cursor.getString(1);
String username = cursor.getString(2);
String password = cursor.getString(3);
byte[] image = cursor.getBlob(4);
list.add(new Food(name, username, password, image));
}
adapter.notifyDataSetChanged();
cursor.close();
MainActivity.sqLiteHelper.close();
}
catch (Exception error){
Log.e("Error", error.getMessage());
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if(requestCode == 888){
if(grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
mTakePhotoList.launch("image/*");
}
else {
Toast.makeText(getApplicationContext(), "You have no permission to access the file location!", Toast.LENGTH_SHORT).show();
}
return;
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}