在运行时

时间:2015-10-01 22:16:14

标签: java android android-fragments arraylist

我会尽量保持清晰

我的问题是这个,我有一个带有一些字段和一个表的屏幕,通过按下Ok键同时插入表中的行和ArrayList中的项。 该行有一个与行一起插入的ImageButton。我需要单击此按钮删除行和相应的Array项。我试了几件但没有成功。通过按ImageButton没有任何反应。 在我忘记我使用片段之前。

这是我的观点:

http://snag.gy/l3FTW.jpg

我的线路布局

        <?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bordas">

    <TextView
        android:id="@+id/tv_animais"
        style="@style/TextoCorpoTabela" />

    <TextView
        android:id="@+id/tv_peso_bruto"
        style="@style/TextoCorpoTabela" />

    <TextView
        android:id="@+id/tv_peso_liquido"
        style="@style/TextoCorpoTabela" />


    <ImageButton
        android:id="@+id/bt_excluir"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:background="@drawable/ic_lixeira" />

</TableRow>

我的onClick方法

public void onClick(View view) {
    recebeValores();
    if (view == btOk) {
        if (verificaCampos()) {
            insereLinha();
            calculaTotal();
            limpaCampos();
            etNumeroTanque.setEnabled(false);
        }
    } else {
        if (view == btFinalizar) {
            //salvar();
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction().addToBackStack("Biometria").replace(R.id.principal, new BiometriaRelatorioFragment()).commit();
        } else {
            for (ItemBiometria itemBiometria : itemBiometriaArrayList) {
                if (view == itemBiometria.botaoExcluir) {
                    itemBiometriaArrayList.remove(itemBiometria);
                }
            }
        }
    }
}

我的私人课程

private class ItemBiometria {
    private int animais;
    private float peso;
    private float pesoLiquido;
    private float tara;
    private View botaoExcluir;

    public int getAnimais() {
        return animais;
    }

    public void setAnimais(int animais) {
        this.animais = animais;
    }

    public float getPeso() {
        return peso;
    }

    public void setPeso(float peso) {
        this.peso = peso;
    }

    public float getPesoLiquido() {
        return pesoLiquido;
    }

    public void setPesoLiquido(float pesoLiquido) {
        this.pesoLiquido = pesoLiquido;
    }

    public float getTara() {
        return tara;
    }

    public void setTara(float tara) {
        this.tara = tara;
    }

    public View getBotaoExcluir() {
        return botaoExcluir;
    }

    public void setBotaoExcluir(View botaoExcluir) {
        this.botaoExcluir = botaoExcluir;
    }
}

我的方法在表格中插入行

   public void insereLinha() {
    ItemBiometria itemBiometria = new ItemBiometria();

    TableLayout tableLayout = (TableLayout) viewPai.findViewById(R.id.tl_biometria);
    LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    final TableRow novaLinha = (TableRow) layoutInflater.inflate(R.layout.layout_linha_tabela_biometria, null);

    itemBiometria.setAnimais(Integer.valueOf(etAnimais.getText().toString()));
    itemBiometria.setPeso(Float.valueOf(etPeso.getText().toString()));
    itemBiometria.setTara(Float.valueOf(etTara.getText().toString()));

    TextView tvAnimais = (TextView) novaLinha.findViewById(R.id.tv_animais);
    tvAnimais.setText(animais);

    TextView tvPesoBruto = (TextView) novaLinha.findViewById(R.id.tv_peso_bruto);
    float peso = itemBiometria.getPeso();
    tvPesoBruto.setText(String.format("%.2f", peso));

    TextView tvPesoLiquido = (TextView) novaLinha.findViewById(R.id.tv_peso_liquido);
    float pesoLiquido = itemBiometria.getPeso() - itemBiometria.getTara();
    tvPesoLiquido.setText(String.format("%.2f", pesoLiquido));
    itemBiometria.setPesoLiquido(Float.valueOf(tvPesoLiquido.getText().toString()));

    ImageButton excluir = (ImageButton) novaLinha.findViewById(R.id.bt_excluir);
    excluir.setOnClickListener(this);
    itemBiometria.setBotaoExcluir(excluir);

    tableLayout.addView(novaLinha);
    itemBiometriaArrayList.add(itemBiometria);
}

1 个答案:

答案 0 :(得分:0)

我可以解决我的问题, 只需删除onClick方法,并传递一个新的OnClickListner来创建按钮:

     ImageButton excluir = (ImageButton) novaLinha.findViewById(R.id.bt_excluir);
    excluir.setOnClickListener(new ImageButton.OnClickListener() {
        @Override
        public void onClick(View view) {
            novaLinha.setVisibility(viewPai.GONE);
            for (ItemBiometria itemBiometria : itemBiometriaArrayList) {
                if (view == itemBiometria.getBotaoExcluir()) {
                    itemBiometriaArrayList.remove(itemBiometria);
                    break;
                }
            }
            calculaTotal();
        }
    });
    itemBiometria.setBotaoExcluir(excluir);