Agent47 Agent47 - 11 days ago 6
Android Question

Android List View, In Alert Dialog Showing Same Item

I have created an Alert Dialog which shows a list view of addresses that the user has searched. However when the alert dialog is shown with the list view items, I get same item repeated, so if I got 6 addresses i'll get item 3 in the address collection repeating 6 times.

I have debugged, and the address collection does show unique items, however something is going wrong in between creating the dialog, and setting the adapter for the listview. I think it is something to do with the convertView in the AddressRowAdapter class, but I am not too sure.

Here is the code.

This is the SearchLocation class, the resource_address_listview contains a ListView widget, which is in a RelativeLayout

//set our adapter
AddressRowAdapter dataAdapter = new AddressRowAdapter(getActivity(), addressList);

//Create Address Selection Dialog
AlertDialog.Builder addressSelectionDialog = new AlertDialog.Builder(getActivity());
//Get the layout file
LayoutInflater alertDialogInflater = getActivity().getLayoutInflater();
//Get our custom view
View getAlertDialogView = alertDialogInflater.inflate(R.layout.resource_address_listview,null);

//Set our custom view

//Set up, confirmation buttons and events for dialog
addressSelectionDialog.setPositiveButton("Select", new AlertDialogPositiveButtonClick());
addressSelectionDialog.setNegativeButton("Cancel", new AlertDialogCancelButtonClick());

//Set up our adapter

//Get our list view
listViewAddressList = (ListView)getAlertDialogView.findViewById(;

//listViewAddressList.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);//Single choice mode, radio buttons
listViewAddressList.setOnItemClickListener(new AddressListViewOnItemClick());//Set our item click listener.

//Create and show dialog
createDialog = addressSelectionDialog.create();;

Here is the code for the AddressRowAdapter, I believe something here is wrong but I can't see anything wrong with the code.

public class AddressRowAdapter extends ArrayAdapter {

//Initialize private variables.
private Context context;
private List<Address> addresses;
private LayoutInflater inflater;

public AddressRowAdapter(Context context, List<Address> objects) {
super(context, 0, objects);

this.context = context;
this.addresses = objects;
this.inflater = LayoutInflater.from(context);


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

//Setup location variables.
String _county = "";
String _subCounty = "";
String _country = "";
String _countryCode = "";
String _postcode = "";
String _addressLine="";
String _seperator = " ";
String _comma = ",";

//If no view is provided, get the view
if (convertView == null) {
convertView = inflater.inflate(android.R.layout.select_dialog_singlechoice, parent, false);

//Find all controls in view.
TextView textView = (TextView) convertView.findViewById(;

//loop through all the addresses.
for (int i=0;i<addresses.size();i++)
//Get address.
Address thisAddress = addresses.get(i);

//if it has no long/lat coordinates we do not need it.
if (!thisAddress.hasLatitude() && !thisAddress.hasLongitude())

if (thisAddress.getAddressLine(0) != null)
_addressLine = thisAddress.getAddressLine(0) + _seperator;

if (thisAddress.getAdminArea() != null)
_county=thisAddress.getAdminArea() + _comma;

if(thisAddress.getSubAdminArea() != null)
_subCounty=thisAddress.getSubAdminArea() + _seperator;

if (thisAddress.getPostalCode() != null)
_postcode = thisAddress.getPostalCode() + _seperator;

if (thisAddress.getCountryCode() != null)
_countryCode=thisAddress.getCountryCode() + _comma;

if (thisAddress.getCountryName() != null)
_country = thisAddress.getCountryName();

textView.setText(_addressLine + _county + _subCounty + _postcode + _countryCode + _country);

return convertView;

public Address getItem(int position) {
return addresses.get(position);

public long getItemId(int position) {
return super.getItemId(position);

public int getCount() {
return addresses.size();


I have debugged this countless times, and the address collection always shows unique items, and I don't seem to be overwriting/replacing them, so I am completely lost to what is going on.

Any help would be appreciated

Kind regards


Why are you using for loop in Adapter? There is no need of for loop. You can directly use

Address thisAddress = addresses.get(position);

Adapter will create the view for the number of count returned by getCount() method. So, if you are returning correct count it will automatically create view for all addresses.

public int getCount(){
     return addresses.size();