CountDownTimer 中的 android-putParcelableArrayList 将相同的值放在返回的列表上
Posted
技术标签:
【中文标题】CountDownTimer 中的 android-putParcelableArrayList 将相同的值放在返回的列表上【英文标题】:android- putParcelableArrayList in CountDownTimer put same value on List returned 【发布时间】:2017-01-03 00:35:56 【问题描述】:我尝试在 countDown 中从 AudioRecord 获取 val,但是当我在 Activity 中获取额外列表时,包含相同的值(我已经检查过 AudioRecord 是否返回了不同的值)。请帮我!。提前致谢。 注意:我调用 countDown 传递它 45 * 1000 和 100
// Class of CountDownTimer
dataValues = new ArrayList<Data>();
(MenuActivity)context).runOnUiThread(new Runnable()
@Override
public void run()
new CountDownTimer(millis, intervalInMillis)
@Override
public void onTick(long millisUntilFinished)
firstValues.add(val); // val from AudioRecord
if (firstValues.size() >=10)
firstValues.remove(0);
data.setValue(avgFromIntList(firstValues));
data.setTime(new Date().getTime());
dataValues.add(data);
@Override
public void onFinish()
Bundle extras = new Bundle();
extras.putLong("idSession", session.getId());
extras.putParcelableArrayList("values",dataValues);
Intent toPast = new Intent(context, SessionPastActivity.class);
toPast.putExtras(extras);
context.startActivity(toPast);
.start();
);
// Model class that implement Parcelable
公共类数据实现 Parcelable
private Integer value;
private Long time;
public Data(Integer value, Long time)
this.value = value;
this.time = time;
public Data()
public Integer getValue()
return value;
public void setValue(Integer value)
this.value = value;
public Long getTime()
return time;
public void setTime(Long time)
this.time = time;
@Override
public String toString()
return "Data" +
", value=" + value +
", time=" + time +
'';
@Override
public int describeContents()
return 0;
@Override
public void writeToParcel(Parcel dest, int flags)
dest.writeInt(value);
dest.writeLong(time);
Log.e("inWrite", toString());
// this is used to regenerate your object. All Parcelables must have a CREATOR that implements these two methods
public static final Parcelable.Creator<Data> CREATOR = new Parcelable.Creator<Data>()
public Data createFromParcel(Parcel in)
return new Data(in);
public Data[] newArray(int size)
return new Data[size];
;
// example constructor that takes a Parcel and gives you an object populated with it's values
private Data(Parcel in)
value = in.readInt();
time = in.readLong();
Log.e("inREAD", toString());
// 活动类
ArrayList values_amplitude = getIntent().getExtras().getParcelableArrayList("values");
【问题讨论】:
请创建一个Minimal, Complete, Verifiable Example。 【参考方案1】:试试这个
Bundle bundle=getIntent.getExtra();
if(bundle != null)
ArrayList<Data> values_amplitude = (ArrayList<Data>).getParcelable("values");
else
希望对你有用
【讨论】:
不工作,抛出此错误:键值预期 Parcelable 但值是 java.util.ArrayList。返回默认值以上是关于CountDownTimer 中的 android-putParcelableArrayList 将相同的值放在返回的列表上的主要内容,如果未能解决你的问题,请参考以下文章
当活动不在前面时,在Android中暂停CountDownTimer