Incorporate adapterview to a listview filled from database

1

The fact is that right now I filled the listview correctly with the information I have in the database. But I want to make it custom by means of the adapters as I have done other times for custom lists.

The problem is that I do not know how to integrate the adapters to the code that I have included below. If you could help me I would really appreciate it.

public class registroNotas extends Activity {

    ListView lstCursos;
    String recuperado;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_registro_notas);

        //Recuperamos el código del logeo, que lo vamos a utilizar para mucha cosas
        Bundle recupera = getIntent().getExtras();
        if(recupera != null){
            recuperado = recupera.getString("cod");
        }

        Thread tr2 = new Thread (){
            @Override
            public void run(){
                final String resultado = enviarGET (recuperado);
                runOnUiThread (new Runnable(){
                    @Override
                    public void run() {
                        ////////////////////////////////////////
                        cargarListView (ArregloLista(resultado));
                        //ArregloLista ( resultado );
                        ////////////////////////////////////////
                    }
                });
            }
        };
        tr2.start ();
    }

    public String enviarGET(String id){

        URL url = null;
        String linea = "";
        int respuesta = 0;
        StringBuilder resul = null;

        try {
            //url = new URL ("");
            url = new URL ("");
            //Ahora enviamos el dato
            HttpURLConnection conection = (HttpURLConnection) url.openConnection ();
            //Guardamos la respuesta en el entero, porque sera un uno o un cero
            //Esto es lo que devuelve la BD una vez hacemos la consulta
            respuesta = conection.getResponseCode ();
            //Inicializamos resul
            resul = new StringBuilder ();

            if (respuesta == HttpURLConnection.HTTP_OK) {
                InputStream in = new BufferedInputStream (conection.getInputStream());
                BufferedReader reader = new BufferedReader (new InputStreamReader (in));

                while ((linea = reader.readLine ()) != null){
                    resul.append (linea);
                }
            }
        } catch (Exception e) {}
        return resul.toString ();
    }

    public void mostrar(String response) {
        try {
            JSONArray json = new JSONArray (response);
            for(int i=0;i<json.length ();i++){
                //Aquí llenamos un textView en el caso de tenerlo del siguiente modo:
                //nombre_TextView.setText("Alumno: "+json.getJSONObject(i).getString("nombreAlu"));
            }
        } catch (Exception e) {}
    }

    //Metodo que permite crear un arraylista para llena el listview
    public ArrayList<String> ArregloLista(String response){
        ArrayList<String> listado = new ArrayList<String> ();
        try {
            JSONArray json = new JSONArray (response);
            String texto = "";
            String texto2 = "";
            for(int i=0;i<json.length ();i++){
                texto = json.getJSONObject(i).getString("titulo");
                texto2 = json.getJSONObject(i).getString("descripcion");
                listado.add(texto + "\n" +texto2);
            }
        } catch (Exception e) {}
        return listado;
    }
    //Aquí es donde va TODA LA CHICHA
    public void cargarListView(ArrayList<String> datos){
        ArrayAdapter<String> adaptador = new ArrayAdapter<String > (this, android.R.layout.simple_list_item_1, datos);
        lstCursos = (ListView) findViewById (R.id.listCursos);
        lstCursos.setAdapter (adaptador);
    }
}
    
asked by StarApps 04.01.2017 в 20:50
source

1 answer

0

The work is being done by your code, after receiving a response it is parsed by this method:

  public ArrayList<String> ArregloLista(String response){

and the cargarListView() method is responsible for filling the data in Adapter and assigning the adapter to your ListView ( lstCursos ):

 public void cargarListView(ArrayList<String> datos){
        ArrayAdapter<String> adaptador = new ArrayAdapter<String > (this, android.R.layout.simple_list_item_1, datos);
        lstCursos = (ListView) findViewById (R.id.listCursos);
        lstCursos.setAdapter (adaptador);
    }

Here the problem is to verify that you are actually getting results when making the request:

 final String resultado = enviarGET (recuperado);
    
answered by 04.01.2017 в 21:32