Android TableLayout删除所有信息

时间:2015-03-12 21:50:09

标签: android rows tablelayout

我有一个TableLayout并用WebService填充它并且它的工作正常,但是当我想第二次执行WebService时,数据显示在上面的下面,我必须在显示新信息之前清理,怎么能那样做?

代码onTaskComplete和showTablaPos(填充TableLayOut)

// Called after task finished
@Override
public void onTaskComplete(JSONObject result) {
    // TODO Auto-generated method stub
    try{
        ljsonObject = result;
        //Se obtienen los valores de status y mensaje
        int status = ljsonObject.getInt("status");
        String mensaje = ljsonObject.getString("mensaje");
        mensaje = mensaje.concat(", favor de llamar a la oficina de tú cancha para reportar este mensaje, gracias");
        //status = 0 para el Web Service de Obtener catálogo de Torneos, status = 1 para obtener la tabla de posiciones
        try {
                ljsonArray = ljsonObject.getJSONArray(tag);
                //Arma y carga la tabla de Posiciones
                showTablaPos();
            } catch (Exception e) {
                    Log.e("Error", e.getMessage());
                    e.printStackTrace();
            }

    }catch (Exception e) {
        e.printStackTrace();
    }
}

public void showTablaPos(){
    TableLayout tablaPos = (TableLayout) findViewById(R.id.tabla_pos);
    //Create table row header 
    TableRow tr_head = new TableRow(this);
    tr_head.setId(10);
    tr_head.setBackgroundColor(Color.GRAY);
    //tr_head.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
    //Se crean los header
    TextView label_equipo = new TextView(this);
    label_equipo.setId(20);
    label_equipo.setText("EQUIPO");
    label_equipo.setTextColor(Color.WHITE);
    label_equipo.setPadding(5, 5, 5, 5);
    tr_head.addView(label_equipo);

    TextView label_jj = new TextView(this);
    label_jj.setId(20);
    label_jj.setText("JJ");
    label_jj.setTextColor(Color.WHITE);
    label_jj.setPadding(5, 5, 5, 5);
    tr_head.addView(label_jj);

    TextView label_jg = new TextView(this);
    label_jg.setId(20);
    label_jg.setText("JG");
    label_jg.setTextColor(Color.WHITE);
    label_jg.setPadding(5, 5, 5, 5);
    tr_head.addView(label_jg);

    TextView label_je = new TextView(this);
    label_je.setId(20);
    label_je.setText("JE");
    label_je.setTextColor(Color.WHITE);
    label_je.setPadding(5, 5, 5, 5);
    tr_head.addView(label_je);

    TextView label_jp = new TextView(this);
    label_jp.setId(20);
    label_jp.setText("JP");
    label_jp.setTextColor(Color.WHITE);
    label_jp.setPadding(5, 5, 5, 5);
    tr_head.addView(label_jp);

    TextView label_gf = new TextView(this);
    label_gf.setId(20);
    label_gf.setText("GF");
    label_gf.setTextColor(Color.WHITE);
    label_gf.setPadding(5, 5, 5, 5);
    tr_head.addView(label_gf);

    TextView label_ge = new TextView(this);
    label_ge.setId(20);
    label_ge.setText("GE");
    label_ge.setTextColor(Color.WHITE);
    label_ge.setPadding(5, 5, 5, 5);
    tr_head.addView(label_ge);

    TextView label_dif = new TextView(this);
    label_dif.setId(20);
    label_dif.setText("DIF");
    label_dif.setTextColor(Color.WHITE);
    label_dif.setPadding(5, 5, 5, 5);
    tr_head.addView(label_dif);

    TextView label_ptos = new TextView(this);
    label_ptos.setId(20);
    label_ptos.setText("PTOS.");
    label_ptos.setTextColor(Color.WHITE);
    label_ptos.setPadding(5, 5, 5, 5);
    tr_head.addView(label_ptos);

    tablaPos.addView(tr_head, new TableLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    Integer count = 0;
    Integer cuantos = ljsonArray.length();
    try{
        for (int i = 0; i < ljsonArray.length(); i++) {
            ljsonObject = ljsonArray.getJSONObject(i);                  

            String equipo = (ljsonObject.optString("equ_nombre"));
            Integer jj = (ljsonObject.optInt("jj"));
            Integer jg = (ljsonObject.optInt("jg"));
            Integer je = (ljsonObject.optInt("je"));
            Integer jp = (ljsonObject.optInt("jp"));
            Integer gf = (ljsonObject.optInt("gf"));
            Integer ge = (ljsonObject.optInt("ge"));
            Integer dif = (ljsonObject.optInt("dif"));
            Integer pto = (ljsonObject.optInt("puntos"));

            TableRow tr = new TableRow(this);
            if(count%2!=0) tr.setBackgroundColor(Color.GRAY);
            tr.setId(100+count);
            //tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

            //Create columns to add as table data
            // Create a TextView to add equipo

            TextView labelEquipo = new TextView(this);
            labelEquipo.setId(200+count); 
            labelEquipo.setText(equipo);
            labelEquipo.setPadding(2, 0, 5, 0);
            labelEquipo.setTextColor(Color.WHITE);
            tr.addView(labelEquipo);

            TextView labelJj = new TextView(this);
            labelJj.setId(200+count); 
            labelJj.setText(Integer.toString(jj));
            labelJj.setPadding(2, 0, 5, 0);
            labelJj.setTextColor(Color.WHITE);
            labelJj.setGravity(Gravity.CENTER);
            tr.addView(labelJj);

            TextView labelJg = new TextView(this);
            labelJg.setId(200+count); 
            labelJg.setText(Integer.toString(jg));
            labelJg.setPadding(2, 0, 5, 0);
            labelJg.setTextColor(Color.WHITE);
            labelJg.setGravity(Gravity.CENTER);
            tr.addView(labelJg);

            TextView labelJe = new TextView(this);
            labelJe.setId(200+count); 
            labelJe.setText(Integer.toString(je));
            labelJe.setPadding(2, 0, 5, 0);
            labelJe.setTextColor(Color.WHITE);
            labelJe.setGravity(Gravity.CENTER);
            tr.addView(labelJe);

            TextView labelJp = new TextView(this);
            labelJp.setId(200+count); 
            labelJp.setText(Integer.toString(jp));
            labelJp.setPadding(2, 0, 5, 0);
            labelJp.setTextColor(Color.WHITE);
            labelJp.setGravity(Gravity.CENTER);
            tr.addView(labelJp);

            TextView labelGf = new TextView(this);
            labelGf.setId(200+count); 
            labelGf.setText(Integer.toString(gf));
            labelGf.setPadding(2, 0, 5, 0);
            labelGf.setTextColor(Color.WHITE);
            labelGf.setGravity(Gravity.CENTER);
            tr.addView(labelGf);

            TextView labelGe = new TextView(this);
            labelGe.setId(200+count); 
            labelGe.setText(Integer.toString(ge));
            labelGe.setPadding(2, 0, 5, 0);
            labelGe.setTextColor(Color.WHITE);
            labelGe.setGravity(Gravity.CENTER);
            tr.addView(labelGe);

            TextView labelDif = new TextView(this);
            labelDif.setId(200+count); 
            labelDif.setText(Integer.toString(dif));
            labelDif.setPadding(2, 0, 5, 0);
            labelDif.setTextColor(Color.WHITE);
            labelDif.setGravity(Gravity.CENTER);
            tr.addView(labelDif);

            TextView labelPtos = new TextView(this);
            labelPtos.setId(200+count); 
            labelPtos.setText(Integer.toString(pto));
            labelPtos.setPadding(2, 0, 5, 0);
            labelPtos.setTextColor(Color.WHITE);
            labelPtos.setGravity(Gravity.CENTER);

            tr.addView(labelPtos);

            // finally add this to the table row
            tablaPos.addView(tr, new TableLayout.LayoutParams(
                                    LayoutParams.WRAP_CONTENT,
                                    LayoutParams.WRAP_CONTENT));
            count++;


        }
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
}

}

1 个答案:

答案 0 :(得分:0)

尝试在填充数据之前调用此函数

private void deleteRows(){
        TableLayout tablaPos = (TableLayout) findViewById(R.id.tabla_pos);
        tablaPos.removeAllViews();
}

更新:

试试这个

private void deleteRows(){
        TableLayout tablaPos = (TableLayout) findViewById(R.id.tabla_pos);
        for(int i = 0; i < tablaPos.getChildCount(); i++){ 
            tablaPos.removeViewAt(i); 
        } 
    }