ListView和与倒数计时器项目 [英] ListView and items with countdown timer

查看:301
本文介绍了ListView和与倒数计时器项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我的列表视图,我想设置一个倒数计时器所有ListView的项目,我媒体链接用Google搜索了一个解决方案,但它无法正常工作。问题是,ListView的重复使用(再利用)一个观点,我总是得到一个错误的项目时间。我用一个标签对我的看法,但它仍然无法正常工作,我能不明白的地方我犯了一个错误,请帮助我。谢谢。

i have a issue with my Listview, i want to set a countdown timer to all ListView's items, and i allready have googled a solution for this, but it isn't work correctly. The Problem is that ListView reuses(recycling) a views, and i always get a wrong item time. I use a tag for my view, but it still not work, i can't understand where i made a mistake, please help me. thx.

所以这里的图片,显示我的问题:
PIC1当我刚刚开始的活动;

So here a pictures that shows my problem: pic1 Where i've just started an Activity;

PIC2在哪里我只是滚动向下和向上

pic2 Where i've just scrolled down and up

在这里,我的code(整个班级):

And here my code(whole class):

    public class PromoListActivity extends SherlockActivity {
private ListView mPromoList;
private PromoListAdapter mAdapter;
private ViewFlipper mFlipper;
private Button mBtnRepeat;

@Override
protected 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(),我只是设置当前时间和所需的时间之间的时间差在的TextView 每次 getView()被调用。因此,移动你这个code回到屋里 getView()

I solved this differently in my case. Instead of having a timer handler set inside your getView(), I just set the time difference between the current time and your desired time to the TextView every time getView() is called. So move this code of yours back inside 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);

然后在活动创建一个处理程序来调用 notifyDatasetChanged() ListView的适配器上每分钟:

Then create a handler in the activity to call notifyDatasetChanged() every one minute on the listview's adapter:

Handler timerHandler = new Handler();
Runnable timerRunnable = new Runnable() {
    @Override
    public void run() {
        myAdapter.notifyDataSetChanged();
        timerHandler.postDelayed(this, 60000); //run every minute
    }
};

我停在的onPause此处理()

@Override
protected void onPause() {
    timerHandler.removeCallbacks(timerRunnable);
    super.onPause();
}

和我重新开始它 onResume()

@Override
protected void onResume() {
    timerHandler.postDelayed(timerRunnable, 500);
    super.onResume();
}

就是这样。 :)

And that's it. :)

希望它帮助。

这篇关于ListView和与倒数计时器项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆