I WANT TO DISMISS ALERT DIALOG on click of ListView Item which I have bind in Custom Array Adapter.
I have used following code to bind this API:
Getting Data:
RetrofitInterface retrofitInterface = RetrofitClient.getPinCodeApiService();
Call<List<PinCodeModel>> call = retrofitInterface.getAreaDetailsByPinCode(pinCode);
call.enqueue(new Callback<List<PinCodeModel>>() {
@Override
public void onResponse(Call<List<PinCodeModel>> call, Response<List<PinCodeModel>> response) {
if (response.isSuccessful()) {
List<PinCodeModel> list = response.body();
if (list != null) {
PinCodeModel model = list.get(0);
if (model.getStatus().equalsIgnoreCase(RetrofitClient.SUCCESS)) {
List<PostOffice> postOffices = model.getPostOffice();
if (postOffices.size() > 0) {
showAreaDialog(postOffices);
} else {
Toast.makeText(AddMyBusinessActivity.this, model.getMessage(), Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(AddMyBusinessActivity.this, model.getMessage(), Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(AddMyBusinessActivity.this, "Unable to fetch data, Try again.", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(AddMyBusinessActivity.this, getString(R.string.string_something_went_wrong), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<List<PinCodeModel>> call, Throwable t) {
Toast.makeText(AddMyBusinessActivity.this, getString(R.string.string_something_went_wrong), Toast.LENGTH_SHORT).show();
Crashlytics.logException(t);
Log.e("TAG", "onFailure: " + t.getLocalizedMessage());
}
});
Showing Dialog:
private void showAreaDialog(List<PostOffice> postOffices) {
AlertDialog.Builder builderSingle = new AlertDialog.Builder(AddMyBusinessActivity.this);
ListView listView = new ListView(this);
AreaAdapter areaAdapter = new AreaAdapter(this, postOffices);
listView.setAdapter(areaAdapter);
builderSingle.setTitle("Select Area");
builderSingle.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
PostOffice strName = postOffices.get(i);
Toast.makeText(AddMyBusinessActivity.this, strName.getName(), Toast.LENGTH_SHORT).show();
alertDialog.dismiss();
}
});
builderSingle.setView(listView);
alertDialog = builderSingle.show();
}
I have tried this one too:
AlertDialog.Builder builderSingle = new
AlertDialog.Builder(AddMyBusinessActivity.this);
//ListView listView = new ListView(this);
AreaAdapter areaAdapter = new AreaAdapter(this, postOffices);
builderSingle.setAdapter(areaAdapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
PostOffice strName = postOffices.get(i);
Toast.makeText(AddMyBusinessActivity.this, strName.getName(), Toast.LENGTH_SHORT).show();
Log.d("TAG", "onItemClick: ");
alertDialog.cancel();
}
});
builderSingle.setTitle("Select Area");
builderSingle.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
I am clicking on any item but nothing happen. Can anyone please help me?
I HAVE TRIED ALL THE RELATED LINKS on STACKOVERFLOW but didn't get succeed.
I have also tried to make xml file with listview and inflate it to AlertDialog but now worked for me.
Edited:
Here is row_layout_area.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<androidx.cardview.widget.CardView
android:id="@+id/parentView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
app:cardBackgroundColor="@android:color/white"
app:cardCornerRadius="@dimen/_4sdp"
app:cardElevation="@dimen/_2sdp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:padding="@dimen/_8sdp"
android:layout_height="match_parent">
<TextView
android:id="@+id/area_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="1"
android:textColor="@color/colorAccent"
android:textSize="17sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Soham ERP Pvt. Ltd." />
<TextView
android:id="@+id/city_state_country"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_4sdp"
android:ellipsize="end"
android:textColor="@android:color/background_dark"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/area_text_view"
app:layout_constraintTop_toBottomOf="@+id/area_text_view"
tools:text="Software, Software Company" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</layout>
try with by removing or set to false
android:clickable="true"
android:focusable="true"
like
<androidx.cardview.widget.CardView
android:id="@+id/parentView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false"
android:foreground="?attr/selectableItemBackground"
app:cardBackgroundColor="@android:color/white"
app:cardCornerRadius="@dimen/_4sdp"
app:cardElevation="@dimen/_2sdp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true">