Open an Edittext when a Switch component is turned off

Asked

Viewed 368 times

2

I have a Adapter custom rendering list, in this list has a switch that by default comes on and when the user shuts down, the system validates a rule and if that rule was true, he has to show a EditText for the user to justify why the shutdown.

The problem is that I can’t get the Edittext is shown on the screen when the switch is shown.

Adapter:

public class ListaFaltaProvaExameAdapter extends BaseAdapter implements ListAdapter {

    private Context context;
    private List<FaltaMo> faltaList;
    private List<FaltaMo> faltasList;
    private EditText editTextJustificativa;

    public ListaFaltaProvaExameAdapter(Context context, List<FaltaMo> faltaList) {
        this.context = context;
        this.faltaList = faltaList;
        this.faltasList = faltaList;
    }

    @Override
    public int getCount() {
        return faltaList.size();
    }
    @Override
    public Object getItem(int position) {
        return faltaList.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        FaltaMo falta = faltaList.get(position);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View view = inflater.inflate(R.layout.listview_faltas_prova_exame, null);

        TextView textCod = (TextView) view.findViewById(R.id.textCodigo);
        textCod.setText("Código: " + falta.getTipoFalta().getCodigo());

        TextView textFalta = (TextView) view.findViewById(R.id.textFalta);
        textFalta.setText(falta.getTipoFalta().getDescricao());

        TextView textHorarioRegistro = (TextView) view.findViewById(R.id.textHorario);
        textHorarioRegistro.setText("Horário: " + Util.getFormatedStringFromEpoch2000(falta.getRegistro()));

        final FaltaMo faltaAtual = faltaList.get(position); //Pega a falta atual

        Switch mySwitch = (Switch) view.findViewById(R.id.switch1);
        mySwitch.setVisibility(View.VISIBLE);
        mySwitch.setChecked(true);

        editTextJustificativa = (EditText) view.findViewById(R.id.editTextJustificativa);

        mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!isChecked) {
                    faltasList.remove(faltaAtual);
                    faltaAtual.setValidada(false);
                    faltasList.add(faltaAtual);
                    if (faltaAtual.getTipoFalta().isJustificativa()) {
                        editTextJustificativa.setVisibility(view.VISIBLE);
                    }
                }
            }
        });
        return view;
    }

    public List<FaltaMo> getListaFaltas(){
        return faltasList;
    }}
}

XML layout:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="600sp"
    android:padding="5dp">

    <TextView
        style="?android:textAppearanceLarge"
        android:id="@+id/textCodigo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="15sp"
        android:textColor="#000000"
        android:paddingLeft="5dp"/>

    <TextView
        style="?android:textAppearanceLarge"
        android:id="@+id/textFalta"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textCodigo"
        android:text=""
        android:textColor="#000000"
        android:paddingLeft="5dp"/>

    <TextView
        style="?android:textAppearanceLarge"
        android:id="@+id/textHorario"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textFalta"
        android:text=""
        android:textColor="#000000"
        android:paddingLeft="5dp"/>

    <Button
        style="?android:textAppearanceListItem"
        android:id="@+id/btFinalizar"
        android:layout_width="130dp"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="@string/btFinalizar"
        android:textStyle="bold"
        android:visibility="invisible"
        />

    <ListView
        android:paddingTop="0dp"
        android:layout_width="1200sp"
        android:layout_height="wrap_content"
        android:id="@+id/listViewFaltasProvaExame"
        android:layout_alignParentStart="false" />

    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:visibility="invisible"
        android:id="@+id/switch1"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:paddingTop="5dp"/>

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"
        android:ems="10"
        android:id="@+id/editTextJustificativa"
        android:textSize="20sp"
        android:hint="Digite aqui a justificativa"
        android:width="400dp"
        android:visibility="invisible"
        android:layout_alignEnd="@+id/textCodigo"/>
</RelativeLayout>
}

Activity:

   public class ActListaFaltasProvaExame extends Activity{

    private ProvaMo prova;
    private List<FaltaMo> listaFaltas;
    private ListaFaltaProvaExameAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        try{
            super.onCreate(savedInstanceState);
            recuperarSessao();
            verificaConteudoListaFaltas();
            setContentView(R.layout.listview_faltas_prova_exame);
            ListView lView = (ListView)findViewById(R.id.listViewFaltasProvaExame);
            adapter = retornaAdapterPopuladoComListaDeFaltas();
            View footerView = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                    .inflate(R.layout.footer_layout, null, false);
            lView.addFooterView(footerView);
            lView.setAdapter(adapter);

            final EditText observacao = (EditText) lView.findViewById(R.id.editTextObservacao);
            Button btFinalizar = (Button) lView.findViewById(R.id.btConfirmar);
            btFinalizar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    insereFaltasNaBase(adapter.getListaFaltas());
                    prova.setFim(Util.getDataHoraAtualEmEpoch2000());
                    Logger.writeLog("Observação do examinador: " + observacao.getText().toString());
                    Logger.writeLog("## Prova Finalizada ##");
                    insereObservacaoNaProva(observacao.getText().toString());
                    openCandidatosActivity();
                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }
    }

1 answer

0

Try to take the Swich click and check if it is active if it does nothing and if it is disabled shows Edittext, but I think you are already doing so.

Example:

Switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {

                if (isChecked) {

                } else {
                    EditText.setVisibility(VISIBLE);

                }

            }
        });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.