0

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

}

Francisco
  • 113
  • 1
  • 1
  • 7

2 Answers2

1

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"

Werner
  • 769
  • 2
  • 6
  • 19
  • okay so I input the code you provide and when I stop the timer the activity switches to the listview but each row is equal to "0" not the time from the chronometer. – Francisco May 05 '14 at 05:19
  • this code is what i put on my chronometer activity. `View.OnClickListener mStopListener = new OnClickListener() { @Override public void onClick(View view) { Timer.stop(); saveTime = Timer.getBase() - SystemClock.elapsedRealtime(); Intent intent = new Intent(getApplicationContext(), TableScreen.class).putExtra("Timer", saveTime); startActivity(intent); } };'` – Francisco May 05 '14 at 05:20
  • then on the listview activity I have this `protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_table_screen); int seconds = getIntent().getExtras().getInt("Timer"); String[] time = new String[6]; for(int i = 0; time.length > i; i++){ time[i] = String.valueOf(seconds); } ListView lv = (ListView)findViewById(R.id.listView); StringArrayAdapter ad = new StringArrayAdapter(time,this); lv.setAdapter(ad); }` – Francisco May 05 '14 at 05:26
0

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();
}
Andrew
  • 77
  • 10
  • so on the chronometer java file, I would create a variable to save the time once I stop it. for example. long saveTime; then saveTime = " " ; then that would put in the code you gave me ? – Francisco May 05 '14 at 00:03
  • Post your relevant code so we can see what you have so far. – Andrew May 05 '14 at 00:06
  • This question could be helpful too. http://stackoverflow.com/questions/4540754/add-dynamically-elements-to-a-listview-android – Andrew May 05 '14 at 01:01
  • This is also a fairly simple listView tutorial. http://windrealm.org/tutorials/android/android-listview.php – Andrew May 05 '14 at 01:03
  • yes i have a listview working at the moment. I just created a random for loop where it would fill the list view with the String "Time" 6 times. so in the listview I have six rows displaying "time" – Francisco May 05 '14 at 01:35
  • 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); – Francisco May 05 '14 at 01:39
  • now in the for loop, what i want is a way to save the chronometer time, for example, time[i] = ChronometerTime something simmilar to this. thanks again for your quick response Andrew. – Francisco May 05 '14 at 01:43
  • Can you add just the Chonometer code to your question? – Andrew May 05 '14 at 02:40