Good afternoon, in my project I need to open a search screen to retrieve an object from a list, so far so good. Before trying to use Dialog I was using the AlertDialog that was serving me very well, but I needed to do a schematic with onScroll not to load the whole list at once, then I had this problem. I need to click on an item in that list and return it to my Activity, and I have no idea how.
My 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();
}
};}
Dialog Layout
<?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>