我的ListvIEw有问题,我想为所有ListVIEw的项目设置一个倒数计时器,我已经用Google搜索了一个解决方案,但它无法正常工作.问题是ListVIEw重用(回收)一个视图,我总是得到一个错误的项目时间.我使用标签作为我的观点,但它仍然不起作用,我无法理解我在哪里犯了错误,请帮助我.谢谢.
所以这里有一张图片显示我的问题:
pic1我刚开始活动的地方;
pic2我只是向下滚动
在这里我的代码(全班):
更新
public class PromoListActivity extends SherlockActivity {private ListVIEw mPromoList;private Promolistadapter mAdapter;private VIEwFlipper mFlipper;private button mBtnRepeat;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.fragment_news_List); getSupportActionbar().setdisplayHomeAsUpEnabled(true); setTitle("Сохранённые акции"); mFlipper = (VIEwFlipper) findVIEwByID(R.ID.flipper); mPromoList = (ListVIEw) findVIEwByID(R.ID.newsList); mBtnRepeat = (button) findVIEwByID(R.ID.btnRepeat); //--> final Handler timerHandler = new Handler(); Runnable timerRunnable = new Runnable() { @OverrIDe public voID run() { mAdapter.notifyDataSetChanged(); timerHandler.postDelayed(this, 1000); // run every minute } }; //<-- mBtnRepeat.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw arg0) { mFlipper.setdisplayedChild(0); getDummyData(); } }); mPromoList.setonItemClickListener(new OnItemClickListener() { @OverrIDe public voID onItemClick(AdapterVIEw<?> arg0, VIEw arg1, int arg2, long arg3) { startActivity(new Intent(PromoListActivity.this, PromoActivityDetails.class)); } }); getDummyData();}private class Promolistadapter extends BaseAdapter { private ArrayList<PromoAction> mItems = new ArrayList<PromoAction>(); private LayoutInflater layoutInflater; private Promolistadapter(Context context, ArrayList<PromoAction> mItems) { layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); this.mItems = mItems; } public int getCount() { return mItems.size(); } public PromoAction getItem(int position) { return mItems.get(position); } public long getItemID(int position) { return position; } public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) { VIEwItem vIEwItem; PromoAction promoAction = getItem(position); if (convertVIEw == null) { vIEwItem = new VIEwItem(); convertVIEw = layoutInflater.inflate(R.layout.ListvIEwitem_action, null); vIEwItem.name = (TextVIEw) convertVIEw.findVIEwByID(R.ID.promoAction_name); vIEwItem.desc = (TextVIEw) convertVIEw.findVIEwByID(R.ID.promoAction_desc); vIEwItem.timer = (TextVIEw) convertVIEw.findVIEwByID(R.ID.promoAction_timer); vIEwItem.timer.setTag(position); convertVIEw.setTag(vIEwItem); } else { vIEwItem = (VIEwItem) convertVIEw.getTag(); } setTime(promoAction,vIEwItem.timer,vIEwItem.timer.getTag().toString()); vIEwItem.name.setText(promoAction.name); vIEwItem.desc.setText(promoAction.descr); return convertVIEw; } private voID setTime(final PromoAction promoAction, final TextVIEw tv, final String tag) { if (tv.getTag().toString().equals(tag)) { long outputTime = Math.abs(promoAction.timer_end - System.currentTimeMillis()); Date date = new java.util.Date(outputTime); String result = new SimpleDateFormat("hh:mm:ss").format(date); tv.setText(result); } } public class VIEwItem { TextVIEw name; TextVIEw desc; TextVIEw timer; }}private voID getDummyData() { ArrayList<PromoAction> List = new ArrayList<PromoAction>(); for (int i = 1; i < 10; i++) { PromoAction action = new PromoAction(); action.name = "Lorem ipsum dolor sit amet"; action.descr = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incIDIDunt ut labore et dolore magna aliqua. "; switch (i) { case 1: { action.timer_start = 1385971000; action.timer_end = 1386104000; } case 2: { action.timer_start = 1385889000; action.timer_end = 1385812550; break; } case 3: { action.timer_start = 1385884200; action.timer_end = 1385912100; break; } default: { action.timer_start = 1385856000; action.timer_end = 1385892000; break; } } List.add(action); } mAdapter = new Promolistadapter(PromoListActivity.this, List); mPromoList.setAdapter(mAdapter); mFlipper.setdisplayedChild(1);}
}
解决方法:
在我的情况下,我解决了这个问题.我没有在你的getVIEw()中设置一个计时器处理程序,而是在每次调用getVIEw()时设置当前时间和你想要的TextVIEw所需时间之间的时差.所以将你的代码移回getVIEw():
long outputTime = Math.abs(promoAction.timer_end - System.currentTimeMillis());Date date = new java.util.Date(outputTime);String result = new SimpleDateFormat("hh:mm:ss").format(date);tv.setText(result);
然后在活动中创建一个处理程序,在ListvIEw的适配器上每隔一分钟调用notifyDatasetChanged():
Handler timerHandler = new Handler();Runnable timerRunnable = new Runnable() { @OverrIDe public voID run() { myAdapter.notifyDataSetChanged(); timerHandler.postDelayed(this, 60000); //run every minute }};
我在onPause()上停止了这个处理程序:
@OverrIDeprotected voID onPause() { timerHandler.removeCallbacks(timerRunnable); super.onPause();}
我在onResume()上再次启动它:
@OverrIDeprotected voID onResume() { timerHandler.postDelayed(timerRunnable, 500); super.onResume();}
就是这样. 总结
以上是内存溢出为你收集整理的android – ListView和带倒数计时器的项目全部内容,希望文章能够帮你解决android – ListView和带倒数计时器的项目所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)