Browse last data entered in PHP json

0

I am working on a project with sound arduino the theme that every few minutes a new datum is added to the bd, but I want to show in an android view the last data entered and update when a new datum is added the issue is that I have raised a half idea but I do not find it shows me nothing.
(the code is a fragment that returns all the data in a RecyclerView that works)

public void Datosonido() {
    String Url = "http://localhost:82/list.php";
    AsyncHttpClient client = new AsyncHttpClient();
    client.post(Url, new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            if (statusCode == 200) {
                String resp = new String(responseBody);
                cargar(resp);
                Log.e("INFO", resp);
            }
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

        }
    });

}
public void cargar(String respuesta){
    LinearLayoutManager lm = new LinearLayoutManager(getActivity());
    lm.setOrientation(LinearLayoutManager.VERTICAL);
    rc.setLayoutManager(lm);

    try {
        List<Sonido> lista=new ArrayList<>();
        JSONArray json = new JSONArray(respuesta);
        for (int i = 0; i < json.length(); i++) {
            Sonido s = new Sonido();
            s.id = json.getJSONObject(i).getInt("id");
            s.nombre = json.getJSONObject(i).getString("name");
            s.valor = json.getJSONObject(i).getDouble("valor1");
            s.fecha = json.getJSONObject(i).getString("fecha");
            lista.add(s);
            Log.e("AAAAAAAAAAAAA",s.nombre);
        }
        Adaptador a = new Adaptador(lista,this,R.layout.item_sound);
        rc.setAdapter(a);
    }catch (Exception e){
        e.printStackTrace();
    }
}

Current Registrations

[{"id":"1","name":"A","valor1":"1","fecha":"2018-12-11"},{"id":"2","name":"Sonido","valor1":"709","fecha":"2018-12-11"},{"id":"3","name":"Sonido","valor1":"710","fecha":"2018-12-11"}]

with this you should show me the last record with all your data in a fragment A contribution with an idea to solve this problem would be very helpful. try in the json for part to do a for instead of i ++ to put json.lenght () - 1 and i-- but it did not work for me

    
asked by Renan Matias Maturana Diaz 11.12.2018 в 23:49
source

1 answer

1

You will have to modify or create a new php. To show all the data in a RecyclerView:

$query = "SELECT * FROM tu_tabla ORDER BY id";

To show only the last data entered:

$query = "SELECT * FROM tu_tabla ORDER BY id DESC LIMIT 1";

If you want to connect to Mysql every 5 minutes, use a handler:

List<Sonido> lista=new ArrayList<>();
final Handler handler = new Handler();
handler.postDelayed( new Runnable() {
        @Override
        public void run() {
            lista.clear();
            Datosonido();
            handler.postDelayed( this, 1000*60*5 ); 
        }
    }, 0 );
    
answered by 12.12.2018 в 03:28