我正在使用列表视图,每一行都有自己的计时器。我想在计时器完成时间间隔时删除列表视图的位置。我已经实现如下但是位置总是错误地捕获并且最后一项每次都删除。请看下面我的代码,在计时器的onfinish()里面我试图删除位置,但它没有被正确删除,请帮帮我。
ProductListAdapter.java
package com.devpoint.adapter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.w3c.dom.Document;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.os.Handler;
import android.text.Html;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TableLayout;
import android.widget.TextView;
import com.devpoint.PlacesandMaps.GMapV2Direction;
import com.devpoint.PlacesandMaps.PlacesMapActivity;
import com.devpoint.common.GetAllGooglePlaces;
import com.devpoint.model.ListDetails;
import com.devpoint.rprtgnet.LoadActivity;
import com.devpoint.rprtgnet.MapFragmentActivity;
import com.devpoint.rprtgnet.R;
import com.devpoint.sharedpreferences.SharedPreference;
import com.devpoint.tabsswipe.ListOnGoing;
import com.devpoint.tabsswipe.SwipeViewPagerAdapter;
import com.devpoint.user.PostLogcatErrors;
import com.devpoint.volley.AppController;
import com.google.android.gms.maps.GoogleMap;
import com.squareup.picasso.Picasso;
import android.view.animation.Transformation;
@SuppressLint({ "InflateParams", "ShowToast" })
public class ProductListAdapter extends ArrayAdapter<ListDetails> {
private Activity context;
List<ListDetails> products;
SharedPreference sharedPreference;
public static com.android.volley.toolbox.ImageLoader imageLoader;
LayoutInflater inflater;
//ViewHolder holder;
protected String Day = "";
public static View alertLayout;
private static GMapV2Direction gmapdirection;
public static GoogleMap gmap;
protected Document doc;
static final int ANIMATION_DURATION = 200;
//TextView timer;
//String Pagename;
private HashMap<TextView,CountDownTimer> counters;
static class TestViewHolder
{
public TextView tvCounter;
}
public ProductListAdapter(Context context, List<ListDetails> products) {
super(context, R.layout.adapter_offer_list_item, products);
this.context = (Activity) context;
this.products = products;
sharedPreference = new SharedPreference();
imageLoader = AppController.getInstance().getImageLoader();
this.counters = new HashMap<TextView, CountDownTimer>();
inflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
}
private class ViewHolder {
TextView productNameTxt;
//TextView Ratingsno;
TextView productPriceTxt;
//TextView timedist;
TableLayout distancesection;
TextView categoryname;
ImageView favoriteImg;
RatingBar rb;
ImageView OfferImage;
//ImageView MapImage;
ImageView listimage;
public TextView distance;
public ImageView indicator;
TextView timer;
TextView move;
//TextView Postedon;
}
@Override
public int getCount() {
return products.size();
}
@Override
public ListDetails getItem(int position) {
return products.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int po, View convertView, final ViewGroup parent) {
try {
ViewHolder holder = null;
Day = "";
final ListDetails product = products.get(po);
//final int abc = po;
/*LayoutInflater li = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);*/
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.offerlist_new, parent,
false);
final View v= convertView;
// convertView.requestLayout();
holder.productNameTxt = (TextView) convertView
.findViewById(R.id.name);
holder.productPriceTxt = (TextView) convertView
.findViewById(R.id.listview_description);
holder.distance = (TextView) convertView
.findViewById(R.id.listview_distane);
/*holder.timedist = (TextView) convertView
.findViewById(R.id.timedist);*/
/*holder.Ratingsno = (TextView) convertView
.findViewById(R.id.Ratingsno);*/
/*holder.Postedon = (TextView) convertView
.findViewById(R.id.Postedon);*/
holder.favoriteImg = (ImageView) convertView
.findViewById(R.id.fav_checkbox);
holder.rb = (RatingBar) convertView
.findViewById(R.id.ratingbar);
holder.timer = (TextView) convertView.findViewById(R.id.time);
holder.categoryname = (TextView) convertView
.findViewById(R.id.Categoryname);
holder.listimage = (ImageView) convertView
.findViewById(R.id.list_image);
/*holder.distancesection = (TableLayout) convertView
.findViewById(R.id.distancesection);*/
/*holder.MapImage = (ImageView) convertView
.findViewById(R.id.imgmap);*/
holder.move = (TextView) convertView
.findViewById(R.id.listview_description3);
holder.move.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
removeListItem(v,po) ;
} catch (Exception e) {
e.printStackTrace();
PostLogcatErrors ple = new PostLogcatErrors();
ple.PostLogcatErorrs(e);
}
}
});
String text = "<font color=#222222>" + product.getShopName()
+ "</font>"
+ "<small><font color=#0091EA> ( </font></small>"
+ " <small><font color=#0091EA>" + product.getAreaName()
+ "</font></small>"
+ "<small><font color=#0091EA> ) </font></small>";
holder.productNameTxt.setText(Html.fromHtml(text));
// holder.productNameTxt.setText(product.getShopName() + " " + "( "
// + Html.fromHtml("<font color='#0091EA'>" + product.getAreaName()
// + "</font>") + " )");
holder.productPriceTxt.setText(product.getDeal() + "");
holder.rb.setRating(Float.valueOf(product.getRatingAvg()));
//holder.Ratingsno.setText(product.getTotalRatings() + " Ratings");
//holder.timedist.setText(product.getTime());
holder.distance.setText(String.valueOf(product.getDistance())
+ " Km");
holder.categoryname.setText(product.getCategoryName());
//holder.listimage.setImageUrl(product.getListImage(), imageLoader);
Picasso.with(context).load(product.getListImage()).into(holder.listimage);
if (checkFavoriteItem(product)) {
holder.favoriteImg.setImageResource(R.drawable.checked);
holder.favoriteImg.setTag(context
.getString(R.string.red_favcolor));
} else {
holder.favoriteImg.setImageResource(R.drawable.unchecked);
holder.favoriteImg.setTag(context
.getString(R.string.grey_favcolor));
}
/*holder.Postedon.setText("Posted On" + " "
+ product.getPostedDate());*/
final TextView tv = holder.timer;
CountDownTimer cdt = counters.get(tv);
if(cdt!=null)
{
cdt.cancel();
cdt=null;
}
cdt = new CountDownTimer(Long.parseLong(product.getOfferEndTime()), 1000)
{
@Override
public void onTick(long millisUntilFinished)
{
int minutes = 0;
int seconds = 0;
String sDate = "";
if(millisUntilFinished > DateUtils.MINUTE_IN_MILLIS)
{
minutes = (int) (millisUntilFinished / DateUtils.MINUTE_IN_MILLIS);
}
millisUntilFinished -= (minutes*DateUtils.MINUTE_IN_MILLIS);
if(millisUntilFinished > DateUtils.SECOND_IN_MILLIS)
{
seconds = (int) (millisUntilFinished / DateUtils.SECOND_IN_MILLIS);
}
sDate += " "+"00"+":"+String.format("%02d",minutes)+":"+String.format("%02d",seconds);
tv.setText(sDate.trim());
}
@Override
public void onFinish() {
tv.setText("Finished");
final int abc = po;
final Animation animation = AnimationUtils.loadAnimation(
context, android.R.anim.slide_in_left);
v.startAnimation(animation);
Handler handle = new Handler();
handle.postDelayed(new Runnable() {
public void run() {
products.remove(abc);
ListOnGoing.productListAdapter = new ProductListAdapter(
LoadActivity.activity, products);
ListOnGoing.swipelisview
.setAdapter(ListOnGoing.productListAdapter);
ListOnGoing.productListAdapter.notifyDataSetChanged();
}
}, 1000);
}
};
counters.put(tv, cdt);
cdt.start();
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
} catch (Exception e) {
e.printStackTrace();
PostLogcatErrors ple = new PostLogcatErrors();
ple.PostLogcatErorrs(e);
}
return convertView;
}
/* Checks whether a particular product exists in SharedPreferences */
public boolean checkFavoriteItem(ListDetails checkProduct) {
boolean check = false;
List<ListDetails> favorites = sharedPreference.getFavorites(context);
if (favorites != null) {
for (ListDetails product : favorites) {
/*
* if (product.equals(checkProduct)) { check = true; break; }
*/
if (product.getOfferID().equals(checkProduct.getOfferID())) {
check = true;
break;
}
}
}
return check;
}
public void cancelAllTimers()
{
Set<Entry<TextView, CountDownTimer>> s = counters.entrySet();
Iterator it = s.iterator();
while(it.hasNext())
{
try
{
Map.Entry pairs = (Map.Entry)it.next();
CountDownTimer cdt = (CountDownTimer)pairs.getValue();
cdt.cancel();
cdt = null;
}
catch(Exception e){}
}
it=null;
s=null;
counters.clear();
}
}
答案 0 :(得分:1)
如果abc
被声明为您班级的一个字段(例如private int abc
),则每次po
被调用时,您都会abc
分配给getView
。以后,当你打电话
products.remove(abc);
您正在移除索引为最后一个指定值abc
的元素,该元素很可能是最后一个元素的索引。
用
替换上面的行products.remove(po);
或更改
abc = po;
到
final int abc = po;
还尝试使用以下行执行类似操作:
holder = null;