在android和mysql中从listview删除记录

kq0g1dla  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(242)

我正试图解决这个问题,代码似乎工作,但我不知道为什么它不删除数据库中的记录,所以我会张贴我的java代码,第一个将包括在android中的listview。第一个任务是下载并填充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;

?>
5lhxktic

5lhxktic1#

好了,伙计们,我找到了答案,我刚刚添加了deleterecord()客户端。getresponsemessage();
现在它工作了。。。。我希望它能帮助别人!

相关问题