Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. package com.example.listviewdemo;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.view.Menu;
  7. import android.view.MenuInflater;
  8. import android.view.MenuItem;
  9. import android.widget.ListView;
  10.  
  11. public class MainActivity extends Activity {
  12. TimeTrackerAdapter timeTrackerAdapter;
  13. @Override
  14. public void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17. ListView listView = (ListView)
  18. findViewById(R.id.times_list);
  19. timeTrackerAdapter = new TimeTrackerAdapter();
  20. listView.setAdapter(timeTrackerAdapter);
  21. }
  22.  
  23. public boolean onCreateOptionsMenu(Menu m) {
  24. super.onCreateOptionsMenu(m);
  25. MenuInflater menuInflater = getMenuInflater();
  26. menuInflater.inflate(R.menu.time_list_menu, m );
  27. return true;
  28. }
  29.  
  30. public static final int TIME_ENTRY_REQUEST_CODE = 1;
  31. public boolean onMenuItemSelected(int featureId, MenuItem item) {
  32. if (item.getItemId() == R.id.add_time_menu_item) {
  33. Intent intent = new Intent(this, AddTimeActivity.class);
  34. startActivityForResult(intent, TIME_ENTRY_REQUEST_CODE);
  35. return true;
  36. }
  37. return super.onOptionsItemSelected(item);
  38. }
  39.  
  40.  
  41. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  42. if (requestCode == TIME_ENTRY_REQUEST_CODE) {
  43. if (resultCode == RESULT_OK) {
  44. String notes = data.getStringExtra("notes");
  45. String time = data.getStringExtra("time");
  46.  
  47. timeTrackerAdapter.addTimeRecord( new TimeRecord(time, notes));
  48. timeTrackerAdapter.notifyDataSetChanged();
  49. }
  50. }
  51. }
  52.  
  53.  
  54.  
  55. }
  56.  
  57. package com.example.listviewdemo;
  58.  
  59. import android.app.Activity;
  60. import android.content.Intent;
  61. import android.os.Bundle;
  62. import android.view.View;
  63. import android.widget.EditText;
  64.  
  65. public class AddTimeActivity extends Activity {
  66.  
  67. public void onCreate(Bundle savedInstanceState) {
  68. super.onCreate(savedInstanceState);
  69. setContentView(R.layout.add_time);
  70. }
  71. public void onCancel(View view) {
  72. finish();
  73. }
  74. public void onSave(View view) {
  75. Intent intent = getIntent();
  76.  
  77. EditText timeView = (EditText)findViewById(R.id.time_view);
  78. intent.putExtra("time", timeView.getText().toString());
  79.  
  80. EditText notesView = (EditText)findViewById(R.id.notes_view);
  81. intent.putExtra("notes", notesView.getText().toString());
  82.  
  83. this.setResult(RESULT_OK, intent);
  84. finish();
  85. }
  86. }
  87.  
  88. package com.example.listviewdemo;
  89.  
  90. import java.util.ArrayList;
  91.  
  92. import android.view.LayoutInflater;
  93. import android.view.View;
  94. import android.view.ViewGroup;
  95. import android.widget.BaseAdapter;
  96. import android.widget.TextView;
  97.  
  98. public class TimeTrackerAdapter extends BaseAdapter {
  99. private ArrayList<TimeRecord> times = new ArrayList<TimeRecord>();
  100. public TimeTrackerAdapter() {
  101. times.add(new TimeRecord(
  102. "38:23", "Feeling good!"));
  103. times.add(new TimeRecord(
  104. "49:01", "Tired. Needed more caffeine"));
  105. times.add(new TimeRecord(
  106. "26:21", "Iā€™m rocking it!"));
  107. times.add(new TimeRecord(
  108. "29:42", "Lost some time on the hills, but pretty good."));
  109. }
  110.  
  111. @Override
  112. public int getCount() {
  113. return times.size();
  114. }
  115.  
  116. @Override
  117. public Object getItem(int index) {
  118. return getItem(index);
  119. }
  120.  
  121. @Override
  122. public long getItemId(int index) {
  123. return index;
  124. }
  125.  
  126. @Override
  127. public View getView(int index, View view, ViewGroup parent) {
  128. if (view == null) {
  129. LayoutInflater inflater =
  130. LayoutInflater.from(parent.getContext());
  131. view = inflater.inflate(
  132. R.layout.time_list_item, parent, false);
  133. }
  134. TimeRecord time = times.get(index);
  135. TextView timeTextView = (TextView)
  136. view.findViewById(R.id.time_view);
  137. timeTextView.setText(time.getTime());
  138. TextView notesTextView = (TextView)
  139. view.findViewById(R.id.notes_view);
  140. notesTextView.setText(time.getNotes());
  141. return view;
  142.  
  143. }
  144.  
  145. public void addTimeRecord(TimeRecord timeRecord) {
  146. // TODO Auto-generated method stub
  147.  
  148. }
  149.  
  150.  
  151.  
  152.  
  153.  
  154. }
  155.  
  156. public void addTimeRecord(TimeRecord timeRecord) {
  157. // TODO Auto-generated method stub
  158.  
  159. }
  160.  
  161. public void addTimeRecord(TimeRecord timeRecord) {
  162. times.add(timeRecord);
  163. notifyDataSetChanged();
  164. }
  165.  
  166. public void addTimeRecord(TimeRecord timeRecord) {
  167. times.add(timeRecord);
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement