gpt4 book ai didi

android - ListView 和带倒数计时器的项目

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:30:30 25 4
gpt4 key购买 nike

我的 Listview 有问题,我想为所有 ListView 的项目设置一个倒数计时器,我已经在 google 上搜索了解决方案,但它无法正常工作。问题是 ListView 重用(回收) View ,我总是得到错误的项目时间。我为我的 View 使用了标签,但它仍然不起作用,我不明白我在哪里犯了错误,请帮助我。谢谢。

所以这里的图片显示了我的问题:pic1 我刚开始 Activity 的地方; startActivity

pic2 我刚刚向下和向上滚动的地方

listWithError

这是我的代码(全类):

已更新

    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() 中设置计时器处理程序,而是每次 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);

然后在 Activity 中创建一个处理程序,每隔一分钟在 ListView 的适配器上调用 notifyDatasetChanged():

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();
}

就是这样。 :)

希望对您有所帮助。

关于android - ListView 和带倒数计时器的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20265493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com