drecy22400 2014-01-26 10:57 采纳率: 0%
浏览 121
已采纳

将两个值从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 2014-01-26 11:05
    关注

    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)
        {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀