Android recyclerview:如何修复传递数据到新活动?

时间:2017-05-10 04:12:17

标签: android android-recyclerview countdowntimer

我的适配器文件中有倒数计时器。对于每个RecyclerView项目,我将两个日期之间的时间差(以毫秒为单位)传递为" diff"捆绑到MainActivity。我得到了捆绑,然后传递"差异"在详细活动的意图。当我将DetailsText设置为Details Activity中的TextView时,我得到了零。我在这里缺少什么?

Adapter
...
final long countDownInterval = 250;
itemHolder.timer = new CountDownTimer(diff, countDownInterval) {
    public void onTick(long diff) {
        ... 
        bundle.putLong("spantimeinhours",diff);
    }

    public void onFinish() {                    
    }
}.start();
passDataFromAdapter(bundle);

Main Activity
...
public void passDataFromAdapter(Bundle bundle) {        
    joe = bundle.getLong("spantimeinhours");
}

@Override
public void onItemClick(int position, final View view) {

    Intent intent = new Intent(this, Details.class);        
    intent.putExtra("adapterSpanTimeInHours",joe);        /
    startActivity(intent);
}  

Details Activity
...
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    joe = intent.getLongExtra("adapterSpanTimeInHours",0);

    weeks = joe / (7 * 24 * 60 * 60 * 1000);
    days = joe  / (24 * 60 * 60 * 1000) % 7;
    hours = joe / (60 * 60 * 1000) % 24;
    timeinweeks = String.format(Locale.US, "%2dW %2dD %2dh", weeks, days, hours);

    cb11 = (TextView) findViewById(R.id.cb11);
    cb11.setText(timeinweeks);
} 

2 个答案:

答案 0 :(得分:0)

我看到你把它放到了Bundle joe = bundle.getLong("spantimeinhours");但你解析了Intent joe = intent.getLongExtra("adapterSpanTimeInHours",0);中的数据 你可以查看。

答案 1 :(得分:0)

Mainactivity ..

public interface valueUpdater(){
     void onvalueUpdate(int updatedValue)
}

long diff;

 Adapter abc = new Adapter(your date .. new valueUpdaer(){

  void onvalueUpdate(int updatedValue){
      diff = updatedValue; // get the updated value all the time
  }
});

@Override
public void onItemClick(int position, final View view) {

    Intent intent = new Intent(this, Details.class);        
    intent.putExtra("adapterSpanTimeInHours",diff );        // use the updated value of diff here
    startActivity(intent);
} 

适配器..

valueUpdater updater;
  AdapterConstructor(your members .. , valueUpdater updater){

  this.updater = updater;
}

final long countDownInterval = 250;
itemHolder.timer = new CountDownTimer(diff, countDownInterval) {
    public void onTick(long diff) {
        ... 
        updater.onvalueUpdate(diff); // send the updated value to mainactivity
    }

    public void onFinish() {                    
    }
}.start();

详情活动

...
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    joe = intent.getLongExtra("adapterSpanTimeInHours",0);

    weeks = joe / (7 * 24 * 60 * 60 * 1000);
    days = joe  / (24 * 60 * 60 * 1000) % 7;
    hours = joe / (60 * 60 * 1000) % 24;
    timeinweeks = String.format(Locale.US, "%2dW %2dD %2dh", weeks, days, hours);

    cb11 = (TextView) findViewById(R.id.cb11);
    cb11.setText(timeinweeks);
} 

希望这可以帮助你..