我正在尝试解决这个问题,代码似乎有效,但我不知道为什么它不删除数据库中的记录,所以我将发布我的java代码,第一个代码将包含listView安卓。 第一个 AsyncTask 是下载并填充 listView,第二个是消除记录
public class ptClienteScheda extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pt_cliente_scheda);
String idC= getIntent().getStringExtra("idC");
/*PROVA PER ID
Toast.makeText(getBaseContext(),"ID cliente" + idC,Toast.LENGTH_SHORT).show();
*/
URL paginaURL = null;
//invio il dato username che mi servirà per la query
try
{
paginaURL = new URL("http://10.0.2.2/gymHome/schedaPtCliente.php?idC=" + idC);
HttpURLConnection client = (HttpURLConnection) paginaURL.openConnection();
}
catch (MalformedURLException e)
{
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//stato+=connessioneHttp.ScaricaTesto();
new ScaricaTestoTask().execute("http://10.0.2.2/gymHome/schedaPtCliente.php?idC="+ idC);
}
protected void popolaListView(String righeCLiente)
{
//Toast.makeText(this, righeCLiente, Toast.LENGTH_LONG);
//popolo la listView
String[] nameproducts = righeCLiente.split("\\+");
// definisco un ArrayList
final ArrayList<String> listp = new ArrayList<String>();
for (int i = 0; i < nameproducts.length; ++i)
{
listp.add(nameproducts[i]);
}
// recupero la lista dal layout
final ListView mylist = (ListView) findViewById(R.id.lvptc);
// creo e istruisco l'adattatore
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listp);
// inietto i dati
mylist.setAdapter(adapter);
mylist.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> adattatore, final View componente, int pos, long id){
// recupero il titolo memorizzato nella riga tramite l'ArrayAdapter
String titoloriga = (String) adattatore.getItemAtPosition(pos);
String[]titr=titoloriga.split(" ");
//Toast.makeText(getBaseContext(),"Ho cliccato sull'elemento con titolo" + titoloriga,Toast.LENGTH_SHORT).show();
String idC=titr[1];
TextView tw= findViewById(R.id.twID);
tw.setText(idC);
AlertDialog.Builder adb = new AlertDialog.Builder(
ptClienteScheda.this);
adb.setTitle("Elimina elemento:");
adb.setMessage("Sei sicuro di voler eliminare " +listp.get(pos)+ "?");
final int posizioneDaRimuovere = pos;
adb.setNegativeButton("Annulla", null);
adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which)
{
new eliminaEsercizioTask().execute();
adapter.remove(adapter.getItem(posizioneDaRimuovere));
}
});
adb.show();
}
});
}
public void btAggiungi(View view)
{
Intent intent = new Intent(ptClienteScheda.this, aggiungiEsercizio.class);
//passo il dato all'activity
startActivity(intent);
}
public class ScaricaTestoTask extends AsyncTask<String, Void, String>
{
protected String doInBackground(String... urls)
{
return connessioneHttp.ScaricaTesto(urls[0]);
}
@Override
protected void onPostExecute(String result)
{
String righeCliente;
righeCliente=connessioneHttp.parseRigheSchedaPt(result);
popolaListView(righeCliente);
String gigi=connessioneHttp.parseclienteRighePT(result);
TextView tw=findViewById(R.id.twper2);
tw.setText("Sei nella scheda del cliente : "+ gigi);
}
}
public class eliminaEsercizioTask extends AsyncTask<String, Void, String>
{
protected String doInBackground(String... urls)
{
TextView tw2=findViewById(R.id.twID);
String idc= (String) tw2.getText();
return connessioneHttp.deleteRecord(idc);
}
@Override
protected void onPostExecute(String result)
{
TextView risultato=findViewById(R.id.twres);
String res=result;
risultato.setText(res);
}
}
}
这里有另一个类deleteRecord,它连接到已经测试过的PHP
public static String deleteRecord(String id)
{
String result = "";
URL paginaURL = null;
try
{
paginaURL = new URL("http://10.0.2.2/gymHome/eliminaRighe.php?idEsercizio=" + id);
HttpURLConnection client = (HttpURLConnection) paginaURL.openConnection();
result+=" eliminato "+id;
return result;
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
result+="NON ELIMINATO";
return result;
}
问题是记录没有被删除
同时发布 PHP(已经测试并且 PHP 可以工作)
<?php
$user = 'root';
$password = '';
$db = 'gymhome';
$host = 'localhost';
$port=3306;
$idEsercizio=$_GET['idEsercizio'];
try
{
$conn = new PDO("mysql:host=$host;dbname=$db", $user, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// sql to delete a record
$sql = "DELETE FROM righescheda WHERE righescheda.id='$idEsercizio' ";
// use exec() because no results are returned
$conn->exec($sql);
echo "Record deleted successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
最佳答案
好的,我找到了答案,我刚刚添加到了deleteRecord() client.getResponseMessage();
现在它可以工作了......我希望它能对其他人有所帮助!
关于java - Android和MYSQL中从listView中删除记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50814190/