1

Приветствую сообщество.Есть необходимость парсить определенные данные(в данном случае дату) из xml файла находящегося в директории приложения. Был написан вот такой простой код для таймера обратного отсчета.Его минус в том, что дата должна быть заложена прямо в нем(в строке Date futureDate = dateFormat.parse("2017-06-05-10-00-00");)Нужно сделать так, чтобы эта дата бралась из xml файла.Если есть возможность, то можно ответить прямо кодом, а не ссылкой на туториалы.Такой пример не гуглится, а я пока начинающий разработчик, не совсем освоился пока в этой теме

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import android.os.*;
import java.text.*;
import java.util.*;
import android.text.method.*;

public class Tab1 extends Fragment {

    View rootView;

    private TextView txtTimerDay, txtTimerHour, txtTimerMinute, txtTimerSecond;
    private TextView tvEvent;
    private Handler handler;
    private Runnable runnable;

    private TextView txtTextView6;
    public Tab1() {


    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


        View v = inflater.inflate(R.layout.fragment_tab1, container, false);



        txtTimerDay = (TextView) v.findViewById(R.id.txtTimerDay);
        txtTimerHour = (TextView) v.findViewById(R.id.txtTimerHour);
        txtTimerMinute = (TextView) v.findViewById(R.id.txtTimerMinute);
        txtTimerSecond = (TextView) v.findViewById(R.id.txtTimerSecond);
        tvEvent = (TextView) v.findViewById(R.id.tvhappyevent);
        txtTextView6 =(TextView) v.findViewById(R.id.textView6);
        txtTextView6.setMovementMethod(new ScrollingMovementMethod());

        countDownStart();
        return v;

    }




    public void countDownStart() {
        handler = new Handler();
        runnable = new Runnable() {
            @Override
            public void run() {
                handler.postDelayed(this, 1000);
                try {
                    SimpleDateFormat dateFormat = new SimpleDateFormat(
                        "yyyy-MM-dd-hh-mm-ss");
                         Date futureDate = dateFormat.parse("2017-06-05-10-00-00");
                    Date currentDate = new Date();
                    if (!currentDate.after(futureDate)) {
                        long diff = futureDate.getTime()
                            - currentDate.getTime();
                        long days = diff / (24 * 60 * 60 * 1000);
                        diff -= days * (24 * 60 * 60 * 1000);
                        long hours = diff / (60 * 60 * 1000);
                        diff -= hours * (60 * 60 * 1000);
                        long minutes = diff / (60 * 1000);
                        diff -= minutes * (60 * 1000);
                        long seconds = diff / 1000;
                        txtTimerDay.setText("" + String.format("%02d", days));
                        txtTimerHour.setText("" + String.format("%02d", hours));
                        txtTimerMinute.setText(""
                                               + String.format("%02d", minutes));
                        txtTimerSecond.setText(""
                                               + String.format("%02d", seconds));
                    } else {
                        tvEvent.setVisibility(View.VISIBLE);
                        tvEvent.setText("Restart?");

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        handler.postDelayed(runnable, 1 * 1000);
    }


}

1 ответ 1

0

Вот статья про парсер XML Pull Parser. Думаю, он Вам подойдёт.

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими пользовательским соглашением, политикой конфиденциальности и политикой о куки

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.