Я новичок в android, я пытаюсь создать несколько таймеров обратного отсчета в списке в пределах вкладок. Тем не менее, таймер обратного отсчета сбрасывается, когда я меняю вкладку или начинаю другое намерение. Как я могу убедиться, что таймер никогда не остановится и не позволит таймеру сбросить настройки?Запуск таймера обратного отсчета после начала другого действия
Вот мой код:
public class OneFragment extends ListFragment {
View rootView;
TextView scheduleId;
ListAdapter listadapter;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final DBController controller = new DBController(getActivity());
final ArrayList<HashMap<String, String>> scheduleList = controller.getAllSchedules();
if (scheduleList.size()!=0){
ListView lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
scheduleId = (TextView) view.findViewById(R.id.scheduleId);
String getId = scheduleId.getText().toString();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(getActivity(), getId, duration);
toast.show();
controller.deleteSchedule(getId);
onResume();
}
});
ArrayList schedule = new ArrayList();
schedule.clear();
String query = "SELECT * FROM schedules";
Cursor c1 = controller.selectQuery(query);
if (c1 != null & c1.getCount() != 0) { //if there is item in the cursor
if (c1.moveToFirst()) { //start to move to position
do {
getSchedule schedule1 = new getSchedule(); //create object of the class getParticipant
schedule1.setscheduleId(c1.getString(c1
.getColumnIndex("scheduleId"))); //set the friend id in the object
schedule1.setscheduleName(c1.getString(c1
.getColumnIndex("scheduleName"))); //set the friend name in the object
schedule1.setscheduleDuration(System.currentTimeMillis()+(60*Integer.parseInt(c1.getString(c1.getColumnIndex("scheduleDuration")))*1000));
schedule.add(schedule1); //add the object to arraylist
} while (c1.moveToNext()); //if still have item in the cursor, loop again
}
}
c1.close();
CustomAdapter custom = new CustomAdapter(OneFragment.this, schedule);//create object of adapter class by
//passing a context and arraylist
setListAdapter(custom); //set the adapter
}
}
@Override
public void onResume() { //when user resume from an intent
super.onResume();
if (getListView() != null) { //if the list view is not null
updateData(); //call the method updateData() to update the listView
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragment_one, container, false);
return rootView;
}
public void deleteData(String id){
DBController controller = new DBController(getActivity());
controller.deleteSchedule(id);
}
public void updateData(){
final DBController controller = new DBController(getActivity());
final ArrayList<HashMap<String, String>> scheduleList = controller.getAllSchedules();
if (scheduleList.size()!=0){
ListView lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
scheduleId = (TextView) view.findViewById(R.id.scheduleId);
String getId = scheduleId.getText().toString();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(getActivity(), getId, duration);
toast.show();
controller.deleteSchedule(getId);
onResume();
}
});
ArrayList schedule = new ArrayList();
schedule.clear();
String query = "SELECT * FROM schedules";
Cursor c1 = controller.selectQuery(query);
if (c1 != null & c1.getCount() != 0) { //if there is item in the cursor
if (c1.moveToFirst()) { //start to move to position
do {
getSchedule schedule1 = new getSchedule(); //create object of the class getParticipant
schedule1.setscheduleId(c1.getString(c1
.getColumnIndex("scheduleId"))); //set the friend id in the object
schedule1.setscheduleName(c1.getString(c1
.getColumnIndex("scheduleName"))); //set the friend name in the object
schedule1.setscheduleDuration(System.currentTimeMillis()+(60*Integer.parseInt(c1.getString(c1.getColumnIndex("scheduleDuration")))*1000));
schedule.add(schedule1); //add the object to arraylist
} while (c1.moveToNext()); //if still have item in the cursor, loop again
}
}
c1.close();
CustomAdapter custom = new CustomAdapter(OneFragment.this, schedule);//create object of adapter class by
//passing a context and arraylist
setListAdapter(custom); //set the adapter
}
}
}
class CustomAdapter extends ArrayAdapter<getSchedule> {
private LayoutInflater lf;
private List<ViewHolder> lstHolders;
private Handler mHandler = new Handler();
private Runnable updateRemainingTimeRunnable = new Runnable() {
@Override
public void run() {
synchronized (lstHolders) {
long currentTime = System.currentTimeMillis();
for (ViewHolder holder : lstHolders) {
holder.updateTimeRemaining(currentTime);
}
}
}
};
public CustomAdapter(OneFragment context, List<getSchedule> objects) {
super(context.getActivity(), 0, objects);
lf = LayoutInflater.from(context.getActivity());
lstHolders = new ArrayList<>();
startUpdateTimer();
}
private void startUpdateTimer() {
Timer tmr = new Timer();
tmr.schedule(new TimerTask() {
@Override
public void run() {
mHandler.post(updateRemainingTimeRunnable);
}
}, 1000, 1000);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = lf.inflate(R.layout.view_schedule_entry, parent, false);
holder.tvProduct = (TextView) convertView.findViewById(R.id.scheduleName);
holder.tvTimeRemaining = (TextView) convertView.findViewById(R.id.scheduleDuration);
convertView.setTag(holder);
synchronized (lstHolders) {
lstHolders.add(holder);
}
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.setData(getItem(position));
return convertView;
}
}
class ViewHolder {
TextView tvProduct;
TextView tvTimeRemaining;
String getid;
getSchedule mProduct;
public void setData(getSchedule item) {
mProduct = item;
tvProduct.setText(item.scheduleName);
getid = item.getscheduleId();
updateTimeRemaining(System.currentTimeMillis());
}
public void updateTimeRemaining(long currentTime) {
long timeDiff = mProduct.getscheduleDuration() - currentTime;
if (timeDiff > 0) {
int seconds = (int) (timeDiff/1000) % 60;
int minutes = (int) ((timeDiff/(1000 * 60)) % 60);
int hours = (int) ((timeDiff/(1000 * 60 * 60)) % 24);
tvTimeRemaining.setText(hours + " hrs " + minutes + " mins " + seconds + " sec");
} else {
}
}
}
Я stuck..Please помочь мне. Благодарю.