Reputation: 1198
I am all of a sudden getting duplicate elements in my listview. When I onClick action on the list items, everything comes back to normal. I also checked my adapter, I am not adding multiple items in the ArrayList. What can possibly be wrong here.
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null || convertView.getTag() == null) {
convertView = inflater.inflate(R.layout.bookingresponse_row, null);
final CabbieViewHolder viewHolder = new CabbieViewHolder();
viewHolder.call = (ImageView) convertView.findViewById(R.id.callCab);
viewHolder.call.setOnClickListener(this);
viewHolder.driverName = (TextView) convertView.findViewById(R.id.driverName);
//SIMILAR CODE FOR MORE DATA ITEMs
convertView.setTag(viewHolder);
}
// Setting all values in listview
String temp = latestAcceptedCabbieName.get(position);
((CabbieViewHolder) (convertView.getTag())).driverName.setText(temp);
((CabbieViewHolder) (convertView.getTag())).inDetailsDriverName.setText(temp);
//SIMIAR CODE for other data items
// Smoothen out the list
new AsyncTask<Object, Void, Float>() {
CabbieViewHolder cabbieViewHolder;
@Override
protected Float doInBackground(Object... params) {
float cabDistanceFromCustomer = -1.0f;
cabbieViewHolder = (CabbieViewHolder) params[0];
if (cabbieViewHolder.distanceFromCustomer == -1.0f) {
int position = (int) ((Integer) params[1]).intValue();
cabbieViewHolder.distanceFromCustomer = cabDistanceFromCustomer = God
.getGeoDistance(Double.valueOf(latestAcceptedCabbieSourceGPSX
.get(position)), Double
.valueOf(latestAcceptedCabbieSourceGPSY.get(position)),
Double.valueOf(customerSourceLat / 1E6), Double
.valueOf(customerSourceLng / 1E6));
} else
cabDistanceFromCustomer = cabbieViewHolder.distanceFromCustomer;
return cabDistanceFromCustomer;
}
@Override
protected void onPostExecute(Float result) {
super.onPostExecute(result);
if (customerSourceLat != 0 && customerSourceLng != 0) {
String temp;
if (result != 0) {
temp = "Current Location @ Approx "
+ String.format("%.1f", result / 1000) + " km";
cabbieViewHolder.cabDistance.setText(temp);
cabbieViewHolder.inDetailsCabDistance.setText(temp);
} else {
cabbieViewHolder.cabDistance.setVisibility(View.GONE);
cabbieViewHolder.inDetailsCabDistance.setVisibility(View.GONE);
}
} else {
cabbieViewHolder.cabDistance.setVisibility(View.GONE);
cabbieViewHolder.inDetailsCabDistance.setVisibility(View.GONE);
}
}
}.execute((CabbieViewHolder) convertView.getTag(), new Integer(position));
return convertView;
}
Upvotes: 0
Views: 140
Reputation: 25863
I think you're having a race condition there. You're returning convertView
before it's initialized by the AsyncTask
. Keep in mind that return convertView
might well be executed before doInBackground
is called.
I suggest you either load all view data in one AsyncTask
or wait until postExecute
is called to return the convertView
. Or simply don't use AsyncTask
if the workload is not very slow.
Upvotes: 1
Reputation: 9434
I see that you are loading the content in an asynchronous task. If the task has not completed yet you will see the old content.
Try putting a "loading data" indicator in the view so you know the data is stale in an else block that executes when convertView is not null.
Upvotes: 1