Wie beim Insert-AlertDialog, nutzen wir hier fast die gleiche Syntax.
update.php
<?php
include 'dbconn.php';
header('Content-Type: application/json');
$response = array();
$id = (isset($_POST['id'], $_POST['manufacturer'], $_POST['type'])) ? $_POST['id'] : die(json_encode(array('success' => -1), JSON_PRETTY_PRINT));
$manufacturer = (isset($_POST['id'], $_POST['manufacturer'], $_POST['type'])) ? $_POST['manufacturer'] : die(json_encode(array('success' => -1), JSON_PRETTY_PRINT));
$type = (isset($_POST['id'], $_POST['manufacturer'], $_POST['type'])) ? $_POST['type'] : die(json_encode(array('success' => -1), JSON_PRETTY_PRINT));
$update = "UPDATE devices SET manufacturer = :manufacturer, type = :type WHERE _id = :id";
$updateQuery = $conn->db->prepare($update);
$updateQuery->bindParam(":manufacturer", $manufacturer, PDO:: PARAM_STR);
$updateQuery->bindParam(":type", $type, PDO:: PARAM_STR);
$updateQuery->bindParam(":id", $id, PDO:: PARAM_INT);
if ($updateQuery->execute() && $updateQuery->errorCode() == 0) {
echo json_encode(array('success' => 1), JSON_PRETTY_PRINT);
} else {
echo json_encode(array('success' => -1), JSON_PRETTY_PRINT);
}
$updateQuery->closeCursor();
$conn->db = null;
?>
Wir benötigen noch eine Klasse Update, welche für den Callback (Response) unseres ApiService gebraucht wird.
Update.java
public class Update {
private int success;
public int getSuccess() {
return success;
}
}
Zunächst erweitern wir wieder das Interface ApiService.
@FormUrlEncoded
@POST("/api/update.php")
Call<Update> updateDevice(
@Field("id")
String id,
@Field("manufacturer")
String manufacturer,
@Field("type")
String type
);
Das prompts.xml-Layout können wir gleich weiter nutzen. Und nutzen werden wir natürlich den Positiv-Button um die Methode showDialogUpdate aufzurufen.
private void showDialogUpdate(Context context, final String _id) {
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.prompts, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
alertDialogBuilder.setTitle("Device update");
alertDialogBuilder.setView(promptsView);
final EditText userInputManufacturer = (EditText) promptsView
.findViewById(R.id.editTextDialogManufacturer);
final EditText userInputType = (EditText) promptsView
.findViewById(R.id.editTextDialogType);
alertDialogBuilder
.setCancelable(false)
.setPositiveButton(getString(android.R.string.ok),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String manufacturer = userInputManufacturer.getText().toString();
String type = userInputType.getText().toString();
Call<Update> insertCall = apiService.updateDevice(_id, manufacturer, type);
insertCall.enqueue(new Callback<Update>() {
@Override
public void onResponse(Call<Update> call, Response<Update> response) {
switch (response.body().getSuccess()) {
case -1:
Toast.makeText(getApplicationContext(), "etwas ist schief gelaufen.", Toast.LENGTH_LONG).show();
break;
case 1:
Toast.makeText(getApplicationContext(), "Alle OK.", Toast.LENGTH_LONG).show();
break;
}
}
@Override
public void onFailure(Call<Update> call, Throwable t) {
Log.e("Fail", t.getLocalizedMessage());
}
});
}
})
.setNegativeButton(getString(android.R.string.cancel),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
Um den AlertDialog aufzurufen, werden wir die mit der ItemClickSupport-Klasse die OnItemLongClick-Methode nutzen. Diese setzten wir sinnvollerweiser am Ende der onCreate-Methode.
ItemClickSupport.addTo(mRecyclerView).setOnItemLongClickListener(new ItemClickSupport.OnItemLongClickListener() {
@Override
public boolean onItemLongClicked(RecyclerView recyclerView, int position, View v) {
showDialogUpdate(MainActivity.this, details.get(position).getId());
return true;
}
});