drecy22400
drecy22400
采纳率0%
2014-01-26 10:57

将两个值从doInBackground传递给onPostExecute

i have this code:

static class Tarea1 extends AsyncTask<ListView, Void, ArrayAdapter<mostrar_alumnos>>
{
    Context contexto;
    ListView list;
    InputStream is;
    ArrayList<mostrar_alumnos> listaalumnos = new ArrayList<mostrar_alumnos>();

    public void cargarContenido (Context contexto)
    {
        this.contexto= contexto;
    }

    @Override
    protected ArrayAdapter<mostrar_alumnos> doInBackground(ListView... params)
    {
        list = params[0];
        String resultado = "fallo";
        mostrar_alumnos alum;
        mostrar_curso_clase_asignatura asign;



        HttpClient cliente = new DefaultHttpClient();
        String URL = "http://"+ip_conexion.IP_Server+"/android/sesion_profesor.php?dia_semana="+dia_semana+"&campo_hora="+campo_hora+"&id_usuario="+id_usuario+"";

        Log.i("httpget", URL);
        HttpGet peticionGet = new HttpGet(URL);
        try{
            HttpResponse response = cliente.execute(peticionGet);
            HttpEntity contenido = response.getEntity();
            is = contenido.getContent(); 
        }catch (ClientProtocolException e)
        {
            e.printStackTrace();
        }catch (IOException e)
        {
            e.printStackTrace();
        }

        BufferedReader buferlector = new BufferedReader (new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String linea = null;
        try{
            while((linea = buferlector.readLine())!=null)
            {
                sb.append(linea);
            }
        }catch (IOException e)
        {
            e.printStackTrace();
        }
        try{
            is.close(); 
        }catch (IOException e) {
            e.printStackTrace();
        }
        resultado = sb.toString(); 
        Log.d("Debug", "Resultado: " + resultado);

        Pair p=new Pair();
        try{

            JSONArray arrayJson = new JSONArray(resultado);

            for(int i= 0; i<arrayJson.length();i++){ 
                JSONObject objetoJson = arrayJson.getJSONObject(i);
                alum = new mostrar_alumnos(objetoJson.getInt("id_alumno"), objetoJson.getString("nombre"), objetoJson.getString("apellidos"));
                JSONObject objetoJson2 = arrayJson.getJSONObject(arrayJson.length()-1);
                String asignatura = new mostrar_curso_clase_asignatura(objetoJson2.getInt("curso"), objetoJson2.getString("clase"), objetoJson2.getString("asignatura")).toString();
                //mTxtView1.setText(asignatura);
                listaalumnos.add(alum); 
                p.asignatura=asignatura;
            }
        }catch (JSONException e){
            e.printStackTrace();
        }

        ArrayAdapter<mostrar_alumnos> adaptador = new ArrayAdapter<mostrar_alumnos>(contexto, android.R.layout.simple_list_item_1, listaalumnos); //Contexto de ver clientes (Muestra la lista con los datos de clientes que le he pasado
        p.result=adaptador;
        return p; 
            }

    public class Pair
    {
        public ArrayAdapter<mostrar_alumnos> result;
        public String asignatura;
    }
    protected void onPostExecute(Pair p) 
    {
        list.setAdapter(p.result);
        mTxtView1.setText(p.asignatura);
        }

    }

}

And i would like to show in the Activity, at the top the text view with "curso", "clase" and "asignatura" and below, the list of pupils with "nombre", asignatura" and "id_alumno".

However, here it says that cannot resolve these variables:

protected void onPostExecute(Pair p) 
    {
        list.setAdapter(result);
        mTxtView1.setText(asignatura);
        }

    }

result and asignatura.

The JSON Output is this:

[{"id_alumno":"109","nombre":"Cristian","apellidos":"Ari\u00f1o Garc\u00eda"},{"id_alumno":"110","nombre":"Amanda","apellidos":"Barbastro Cauh\u00e9"},{"id_alumno":"111","nombre":"Manuel","apellidos":"Barcel\u00f3 Borrell"},{"id_alumno":"112","nombre":"Alejandro","apellidos":"Bueno Bel\u00edo"},{"id_alumno":"113","nombre":"Alba","apellidos":"Casanova Alquillu\u00e9"},{"id_alumno":"114","nombre":"Gema","apellidos":"Cortijo Romance"},{"id_alumno":"115","nombre":"Raquel","apellidos":"De la Fuente Tirado"},{"id_alumno":"116","nombre":"\u00c1lvaro","apellidos":"Del ni\u00f1o Jes\u00fas Mu\u00f1oz"},{"id_alumno":"117","nombre":"Alejandro","apellidos":"Escudero Cerd\u00e1"},{"id_alumno":"118","nombre":"Olaya","apellidos":"Fern\u00e1ndez Luengo"},{"id_alumno":"119","nombre":"Noelia","apellidos":"Fern\u00e1ndez Palacios"},{"id_alumno":"120","nombre":"Paloma","apellidos":"Ib\u00e1\u00f1ez P\u00e9rez"},{"id_alumno":"121","nombre":"Pablo","apellidos":"L\u00f3pez Chico"},{"id_alumno":"122","nombre":"Bruno","apellidos":"Mart\u00edn Viver"},{"id_alumno":"123","nombre":"Javier","apellidos":"Mart\u00ednez Zapata"},{"id_alumno":"124","nombre":"Daniel","apellidos":"Mill\u00e1n Vidal"},{"id_alumno":"125","nombre":"Javier","apellidos":"Ort\u00edn Gual"},{"id_alumno":"126","nombre":"Andrea","apellidos":"Palacio Sanz"},{"id_alumno":"127","nombre":"Gloria","apellidos":"P\u00e9rez Pascual"},{"id_alumno":"128","nombre":"Sol","apellidos":"P\u00e9rez Sabroso"},{"id_alumno":"129","nombre":"Gonzalo","apellidos":"Pina Cubero"},{"id_alumno":"130","nombre":"Roc\u00edo","apellidos":"Portillo G\u00f3mez"},{"id_alumno":"131","nombre":"Juan","apellidos":"Prades Fern\u00e1ndez"},{"id_alumno":"132","nombre":"Claudia","apellidos":"Rivero P\u00e9rez"},{"id_alumno":"133","nombre":"Victor","apellidos":"Rodrigo De la Cruz"},{"id_alumno":"134","nombre":"Alba","apellidos":"Rosales Salas"},{"id_alumno":"135","nombre":"Eduardo","apellidos":"Ru\u00edz Palacios"},{"id_alumno":"136","nombre":"Miguel","apellidos":"Sanz G\u00f3mez"},{"id_alumno":"137","nombre":"Nayara","apellidos":"Sole Rivero"},{"id_alumno":"138","nombre":"Valeriy ","apellidos":"Zuazo Egea"},{"curso":"2","clase":"B","asignatura":"Matem\u00e1ticas"}]

How can i solve this problem?

Thanks!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • duanpo7354 duanpo7354 7年前

    change to:

    protected void onPostExecute(Pair p) 
        {
            list.setAdapter(p.result);
            mTxtView1.setText(p.asignatura);
            }
    

    And in doInBackground:

     Pair p=new Pair();
    
    try{
    
                JSONArray arrayJson = new JSONArray(resultado);
    
                for(int i= 0; i<arrayJson.length();i++){ 
                    JSONObject objetoJson = arrayJson.getJSONObject(i);
                    alum = new mostrar_alumnos(objetoJson.getInt("id_alumno"), objetoJson.getString("nombre"), objetoJson.getString("apellidos"));
                    JSONObject objetoJson2 = arrayJson.getJSONObject(arrayJson.length()-1);
                    String asignatura = new mostrar_curso_clase_asignatura(objetoJson2.getInt("curso"), objetoJson2.getString("clase"), objetoJson2.getString("asignatura")).toString();
                    //mTxtView1.setText(asignatura);
                    listaalumnos.add(alum); 
                    p.asignatura=asignatura;
                }
            }catch (JSONException e){
                e.printStackTrace();
            }
    
            ArrayAdapter<mostrar_alumnos> adaptador = new ArrayAdapter<mostrar_alumnos>(contexto, android.R.layout.simple_list_item_1, listaalumnos); //Contexto de ver clientes (Muestra la lista con los datos de clientes que le he pasado
             p.result=adaptador;
            return p; 
                }
    

    Extend you class with following:

    AsyncTask<Params, Progress, Result>
    

    Here last type is return type of doInBackgorund as well as argument of onPostExecute so change this to Pair as you are returning a Pair object.

    change to:

    static class Tarea1 extends AsyncTask<ListView, Void, Pair>
    {
        Context contexto;
        ListView list;
        InputStream is;
        ArrayList<mostrar_alumnos> listaalumnos = new ArrayList<mostrar_alumnos>();
    
        public void cargarContenido (Context contexto)
        {
            this.contexto= contexto;
        }
    
        @Override
        protected Pair doInBackground(ListView... params)
        {
    
    点赞 评论 复制链接分享
  • dongzhitao4839 dongzhitao4839 7年前

    Your doInBackground should return Pair type, AsyncTask is extended as follows:

     private class MyAsync extends AsyncTask<URL, Integer, Pair> {
         protected Pair doInBackground(URL... urls) {    
             // myPair
             return myPair;
         }
    
         protected void onPostExecute(Pair result) {
         }
     }
    

    from code you have provided it looks like doInBackground is returning: ArrayAdapter<mostrar_alumnos> (at leas it looks like it is for doInBackground). Also:

      protected void onPostExecute(Pair p) 
        {
            list.setAdapter(result);
            mTxtView1.setText(asignatura);
    

    should be :

      protected void onPostExecute(Pair p) 
        {
            list.setAdapter(p.result);
                            ^^
            mTxtView1.setText(p.asignatura);
                              ^^  
    

    also if you look into SDK, you will find that Pair type is already there:

    http://developer.android.com/reference/android/util/Pair.html

    点赞 评论 复制链接分享

为你推荐