我正在使用MySQL来显示数据,读取和更新。我试图使其显示在编辑文本中,但它只是读取ID,而不会在编辑文本中显示其他数据(例如,数字,信息和计时器),我试图修复代码,但我仍然不知道我在哪里出错做了。
数据库:db_asetnfc 表格:tb_asetnfc
表格行: -否(主键) -ID(不是主键,但仍然是唯一的。随机数) -信息 -计时器
PHP代码:
<?php
//Mendapatkan Nilai Dari Variable ID Pegawai yang ingin ditampilkan
$id = $_GET['id'];
//Importing database
require_once('koneksi.php');
$sql = "SELECT * FROM tb_asetnfc WHERE id=$id";
//Mendapatkan Hasil
$r = mysqli_query($con,$sql);
//Memasukkan Hasil Kedalam Array
$result = array();
$row = mysqli_fetch_array($r);
array_push($result,array(
"no"=>$row['no'],
"id"=>$row['id'],
"informasi"=>$row['informasi'],
"timer"=>$row['timer']
));
//Menampilkan dalam format JSON
echo json_encode(array('result'=>$result));
mysqli_close($con);
?>
我的Java代码
package com.asetnfc;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
public class DetailDatabaseWWW extends Activity implements OnClickListener{
private EditText editTextno;
private EditText editTextid;
private EditText editTextinformasi;
private EditText editTexttimer;
private Button buttonUpdate;
private Button buttonDelete;
private String id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detaildatabasewww);
Intent intent = getIntent();
id = intent.getStringExtra(konfigurasi.EMP_ID);
editTextno = (EditText) findViewById(R.id.editTextno);
editTextid = (EditText) findViewById(R.id.editTextid);
editTextinformasi = (EditText) findViewById(R.id.editTextinformasi);
editTexttimer = (EditText) findViewById(R.id.editTexttimer);
buttonUpdate = (Button) findViewById(R.id.buttonUpdate);
buttonDelete = (Button) findViewById(R.id.buttonDelete);
buttonUpdate.setOnClickListener(this);
buttonDelete.setOnClickListener(this);
editTextid.setText(id);
getEmployee();
}
private void getEmployee(){
class GetEmployee extends AsyncTask<Void,Void,String>{
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(DetailDatabaseWWW.this,"Fetching...","Wait...",false,false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
showEmployee(s);
}
@Override
protected String doInBackground(Void... params) {
RequestHandler rh = new RequestHandler();
String s = rh.sendGetRequestParam(konfigurasi.URL_GET_EMP,id);
return s;
}
}
GetEmployee ge = new GetEmployee();
ge.execute();
}
private void showEmployee(String json){
try {
JSONObject jsonObject = new JSONObject(json);
JSONArray result = jsonObject.getJSONArray(konfigurasi.TAG_JSON_ARRAY);
JSONObject c = result.getJSONObject(0);
String no = c.getString(konfigurasi.TAG_ID);
String informasi = c.getString(konfigurasi.TAG_INFORMASI);
String timer = c.getString(konfigurasi.TAG_TIMER);
editTextno.setText(no);
editTextinformasi.setText(informasi);
editTexttimer.setText(timer);
} catch (JSONException e) {
e.printStackTrace();
}
}
private void updateEmployee(){
final String no = editTextno.getText().toString().trim();
final String informasi = editTextinformasi.getText().toString().trim();
final String timer = editTexttimer.getText().toString().trim();
class UpdateEmployee extends AsyncTask<Void,Void,String>{
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(DetailDatabaseWWW.this,"Updating...","Wait...",false,false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
Toast.makeText(DetailDatabaseWWW.this,s,Toast.LENGTH_LONG).show();
}
@Override
protected String doInBackground(Void... params) {
HashMap<String,String> hashMap = new HashMap<>();
hashMap.put(konfigurasi.KEY_EMP_NO,no);
hashMap.put(konfigurasi.KEY_EMP_ID,id);
hashMap.put(konfigurasi.KEY_EMP_INFORMASI,informasi);
hashMap.put(konfigurasi.KEY_EMP_TIMER,timer);
RequestHandler rh = new RequestHandler();
String s = rh.sendPostRequest(konfigurasi.URL_UPDATE_EMP,hashMap);
return s;
}
}
UpdateEmployee ue = new UpdateEmployee();
ue.execute();
}
private void deleteEmployee(){
class DeleteEmployee extends AsyncTask<Void,Void,String> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(DetailDatabaseWWW.this, "Updating...", "Tunggu...", false, false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
Toast.makeText(DetailDatabaseWWW.this, s, Toast.LENGTH_LONG).show();
}
@Override
protected String doInBackground(Void... params) {
RequestHandler rh = new RequestHandler();
String s = rh.sendGetRequestParam(konfigurasi.URL_DELETE_EMP, id);
return s;
}
}
DeleteEmployee de = new DeleteEmployee();
de.execute();
}
private void confirmDeleteEmployee(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("Apakah Kamu Yakin Ingin Menghapus Pegawai ini?");
alertDialogBuilder.setPositiveButton("Ya",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
deleteEmployee();
startActivity(new Intent(DetailDatabaseWWW.this,DatabaseWWW.class));
}
});
alertDialogBuilder.setNegativeButton("Tidak",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
public void onClick(View v) {
if(v == buttonUpdate){
updateEmployee();
}
if(v == buttonDelete){
confirmDeleteEmployee();
}
}
}
我的xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent" android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:paddingBottom="16dp" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editTextno" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ID Tag" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editTextid" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Informasi" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editTextinformasi" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Timer" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editTexttimer" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Update"
android:id="@+id/buttonUpdate" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hapus"
android:id="@+id/buttonDelete" />
</LinearLayout>
当我运行代码时,它仅显示ID,而不显示其他任何行,如No,Informasi和timer。代码有什么问题?请有人帮我。