Retrieve information from a Dialog via Activity

Asked

Viewed 118 times

0

Good afternoon, in my project I need to open a search screen to recover an object in a list, until here everything right. Before trying to use Dialog I was using Alertdialog that was serving me very well, however I had to do a scheme with onScroll to not load the whole list at once, then I came up with this problem. I need that by clicking on an item on that list, I return it to my Activity, and I have no idea how to do.

Meu Dialog:

public class DialogCadastro extends Dialog implements android.view.View.OnClickListener {

private EditText editCodigo;
private EditText editDescricao;
private ListView listCadastro;
private Button buttonPesquisar;
private int start = 0;
private boolean search = false;
private IpCadIptuDao iptuDao;


private ListaDialogCadastroAdapter adapter = null;

public DialogCadastro(@NonNull Context context) {
    super(context);
}


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_LEFT_ICON);
    setTitle("Escolha um Cadastro");

    setContentView(R.layout.dialog_cadastro);

    editCodigo = (EditText) findViewById(R.id.editCodigoDialogCadastro);
    editDescricao = (EditText) findViewById(R.id.editDescricaoDialogCadastro);
    buttonPesquisar = (Button) findViewById(R.id.buttonPesquisarDialogCadastro);
    listCadastro = (ListView) findViewById(R.id.listDialogCadastro);

    listCadastro.setOnItemClickListener(listCadastroOnItemClick);

    buttonPesquisar.setOnClickListener(buttonPesquisarOnClickListener);
    listCadastro.setOnScrollListener(onScrollListener());

    String mascara = new GrCadEmpresaDao(getContext()).getMascaraNumeroConsultaImovel();

    if (mascara != null)
        editCodigo.addTextChangedListener(Mask.insertMask(editCodigo, mascara));

    iptuDao = new IpCadIptuDao(getContext());

    List<IpCadIptu> lista = null;
    start = 0;
    try {
        lista = iptuDao.consultarImobiliario(null, null, null, null, null, null, null,
                null, null, null, start);
    } catch (java.text.ParseException e) {
        e.printStackTrace();
    }

    if (lista != null)
        adapter = new ListaDialogCadastroAdapter(getContext(), lista);

    listCadastro.setAdapter(adapter);
}

@Override
public void onClick(View view) {

}


private AbsListView.OnScrollListener onScrollListener() {
    return new AbsListView.OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                             int totalItemCount) {

            if (search && adapter != null) {

                if (start > totalItemCount)
                    return;

                boolean loadMore = firstVisibleItem + visibleItemCount >= totalItemCount;

                if (loadMore) {
                    start += 100;
                    List<IpCadIptu> lista = null;
                    try {
                        iptuDao = new IpCadIptuDao(getContext());
                        lista = iptuDao.consultarImobiliario(null, null, null, null, null, null, null,
                                null, null, null, start);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    adapter.addCadastros(lista);
                    adapter.notifyDataSetChanged();
                }

            }
            search = true;
        }

    };
}

AdapterView.OnClickListener buttonPesquisarOnClickListener = new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {

        iptuDao = new IpCadIptuDao(arg0.getContext());

        List<IpCadIptu> lista = null;
        start = 0;
        try {
            lista = iptuDao.consultarImobiliario(null, null, null, null, null, null, null,
                    null, null, null, start);
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }

        if (lista != null)
            adapter = new ListaDialogCadastroAdapter(arg0.getContext(), lista);

        listCadastro.setAdapter(adapter);
    }
};

AdapterView.OnItemClickListener listCadastroOnItemClick = new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                            long arg3) {

        IpCadIptu iptu = (IpCadIptu) arg0.getItemAtPosition(arg2);

        dismiss();
    }
};}

My Activity:

    public class ConsultarRecadastramentoActivity extends Activity implements Transacao, AbsListView.OnScrollListener{ 
OnClickListener buttonPesquisarCadastroOnClick = new OnClickListener() {
        @Override
        public void onClick(View arg0) {

            DialogCadastro dialog = new DialogCadastro(ConsultarRecadastramentoActivity.this);
            dialog.show();

        }

    };}

Layout of the Dialog

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/layer_padding"
            android:layout_weight=".25"
            android:orientation="vertical">

            <TextView
                style="@style/SimpleTextView"
                android:text="@string/codigo"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/editCodigoDialogCadastro"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="number" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/layer_padding"
            android:layout_weight=".4"
            android:orientation="vertical">

            <TextView
                style="@style/SimpleTextView"
                android:text="@string/descricao"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/editDescricaoDialogCadastro"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="text" />

        </LinearLayout>

        <Button
            android:id="@+id/buttonPesquisarDialogCadastro"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/layer_padding"
            android:layout_weight=".35"
            android:text="@string/pesquisar" />

    </LinearLayout>

    <ListView
        android:id="@+id/listDialogCadastro"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_marginTop="10dp" />

</LinearLayout>
  • Save the selected code in a variable and close the dialog. When loading Activity again check if you have selected code, if you select and load the screen with the corresponding code.

  • The value of this variable is not lost when I close my Dialog?

  • Not if you save it in a global variable.

No answers

Browser other questions tagged

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