如何将计时器中的时间保存到列表视图中?

时间:2014-05-04 20:55:06

标签: android

我创造了一个带有三个按钮的计时器,"开始","停止"和"重置"。一旦我停止了时间,我希望将时间保存到第二个活动中,这将是一个ListView。

public class TrackerScreen extends ActionBarActivity {
Chronometer Timer;
long saveTime;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tracker_screen);
    Button button;
    Timer = (Chronometer) findViewById(R.id.chronometer);

    button = (Button) findViewById(R.id.start);
    button.setOnClickListener(mStartListener);

    button = (Button) findViewById(R.id.stop);
    button.setOnClickListener(mStopListener);

    button = (Button) findViewById(R.id.reset);
    button.setOnClickListener(mResetListener);
}

View.OnClickListener mStartListener = new OnClickListener() {
    @Override
    public void onClick(View view) {
        Timer.setBase(SystemClock.elapsedRealtime());
        Timer.start();
    }
};

View.OnClickListener mStopListener = new OnClickListener() {
    @Override
    public void onClick(View view) {
        Timer.stop();
        saveTime = Timer.getBase() - SystemClock.elapsedRealtime();
    }
};

View.OnClickListener mResetListener = new OnClickListener() {
    @Override
    public void onClick(View view) {
        Timer.setBase(SystemClock.elapsedRealtime());
    }
};

然后这是我的ListView代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_table_screen);
    String[] time = new String[6];
    for(int i = 0; time.length > i; i++){
            time[i] = "time";
    }

    ListView lv = (ListView)findViewById(R.id.listView);
    StringArrayAdapter ad = new StringArrayAdapter(time,this);
    lv.setAdapter(ad);
}

这是我的StringArrayApdapter

public class StringArrayAdapter extends BaseAdapter{
String[] times;
Context ctxt;
LayoutInflater myInflater;
//constructor
public StringArrayAdapter(String[] array, Context c){
    times = array;
    ctxt = c;
    myInflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}
@Override
public int getCount() {
    return times.length;
}

@Override
public Object getItem(int i) {
    //return times at position
    return times[i];
}

@Override
public long getItemId(int i) {
    return 0;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    if(view == null)
        view = myInflater.inflate(android.R.layout.simple_list_item_1,viewGroup,false);
    TextView time = (TextView)view.findViewById(android.R.id.text1);
    time.setText(times[i]);

    return view;
}

}

2 个答案:

答案 0 :(得分:1)

在您的计时器Activity(用户可以启动,停止和重置时间的那个)中,您可以将onClickListener附加到停止按钮,其中包含以下代码:

Intent intent = new Intent(getApplicationContext(),
                ActivityWithListViewName.class).putExtra("nameOfKey", someInt);
startActivity(intent);

然后,在使用ListView的新活动中,您可以使用.putExtra()获取您发送的值:

int seconds = getIntent().getExtras().getInt("nameOfKey");

以上行应返回使用密钥"nameOfKey"

发送的值

答案 1 :(得分:0)

您只需要添加到您提供给适配器的列表中,然后在适配器上调用notifyDataSetChanged()。

public void addTime(long time) {
    timeList.add(time);
    adapter.notifyDataSetChanged();
}