Francisco
Francisco

Reputation: 113

how can I save the time from a chronometer into a listview?

I have created a chronometer with three buttons, "start", "stop" and "reset". once I stop the time I want that time to be save into a second activity which would be a 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());
    }
};

then this is my ListView code

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

this is my 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;
}

}

Upvotes: 0

Views: 681

Answers (2)

Werner
Werner

Reputation: 769

In your chronometer Activity (the one in which the user can start, stop, and reset the time), you could attach an onClickListener to the stop button, in which you include the following code:

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

Then, in your new activity with the ListView, you can get the values you send with .putExtra() with:

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

The above line should return the value sent using the key "nameOfKey"

Upvotes: 1

Andrew
Andrew

Reputation: 122

You just need to add to the list that you gave to the adapter and then call notifyDataSetChanged() on your adapter.

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

Upvotes: 0

Related Questions