当用户从我的应用程序中的日期选择器选项中选择日期时,如何从json检索数据

时间:2013-06-27 06:45:11

标签: android

我的应用程序包含日期选择器选项。当用户选择特定日期时,那天出现的数据应显示在选项卡内容上。我成功解析了JSON中的数据,但我没有得到如何用日期解析数据。请帮助我。谢谢

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    setCurrentDateOnView();
    addListenerOnButton();


    TabHost tabHost = getTabHost();

    // vehicle Tab
    TabSpec vehicleSpec = tabHost.newTabSpec(VEHICLE_SPEC);
    // Tab Icon
    vehicleSpec.setIndicator(VEHICLE_SPEC, getResources().getDrawable(R.drawable.v));
    Intent vehicleIntent = new Intent(this, vehicle.class);
    // Tab Content
    vehicleSpec.setContent(vehicleIntent);
 // driver Tab
    TabSpec driverSpec = tabHost.newTabSpec(DRIVER_SPEC);
    // Tab Icon
    driverSpec.setIndicator(DRIVER_SPEC, getResources().getDrawable(R.drawable.dr));
    Intent driverIntent = new Intent(this, driver.class);
    // Tab Content
    driverSpec.setContent(driverIntent);
 // ignition Tab
    TabSpec ignitionSpec = tabHost.newTabSpec(IGNITION_SPEC);
    // Tab Icon
    ignitionSpec.setIndicator(IGNITION_SPEC, getResources().getDrawable(R.drawable.i));
    Intent ignitionIntent = new Intent(this, ignition.class);
    // Tab Content
    ignitionSpec.setContent(ignitionIntent);
 // activity Tab
    TabSpec activitySpec = tabHost.newTabSpec(ACTIVITY_SPEC);
    // Tab Icon
    activitySpec.setIndicator(ACTIVITY_SPEC, getResources().getDrawable(R.drawable.activity));
    Intent activityIntent = new Intent(this, activity.class);
    // Tab Content
    activitySpec.setContent(activityIntent);

    // speed Tab
    TabSpec speedSpec = tabHost.newTabSpec(SPEED_SPEC);
    speedSpec.setIndicator(SPEED_SPEC, getResources().getDrawable(R.drawable.d));
    Intent speedIntent = new Intent(this, speed.class);
    speedSpec.setContent(speedIntent);

    // idle Tab
    TabSpec idleSpec = tabHost.newTabSpec(IDLE_SPEC);
    idleSpec.setIndicator(IDLE_SPEC, getResources().getDrawable(R.drawable.idle));
    Intent idleIntent = new Intent(this, idle.class);
    idleSpec.setContent(idleIntent);
    TabSpec distanceSpec = tabHost.newTabSpec(DISTANCE_SPEC);
    distanceSpec.setIndicator(DISTANCE_SPEC, getResources().getDrawable(R.drawable.s));
    Intent distanceIntent = new Intent(this, distance.class);
    distanceSpec.setContent(distanceIntent);

    // Adding all TabSpec to TabHost
    tabHost.addTab(distanceSpec); // Adding distance tab
    tabHost.addTab(idleSpec); // Adding idle tab
    tabHost.addTab(speedSpec); // Adding speed tab
    tabHost.addTab(ignitionSpec); // Adding ignition tab
    tabHost.addTab(driverSpec); // Adding driver tab
    tabHost.addTab(vehicleSpec); // Adding vehicle tab
    tabHost.addTab(activitySpec); // Adding activity tab
    TabWidget tabWidget = (TabWidget) findViewById(android.R.id.tabs);
    final int tabChildrenCount = tabWidget.getChildCount();
    View currentView;
    for (int i = 0; i < tabChildrenCount; i++) {
        currentView = tabWidget.getChildAt(i);
        LinearLayout.LayoutParams currentLayout =
            (LinearLayout.LayoutParams) currentView.getLayoutParams();
        currentLayout.setMargins(10, 10, 10, 10);
    }
    tabWidget.requestLayout();

}
private void addListenerOnButton() {
    // TODO Auto-generated method stub
    dbtn1 = (Button) findViewById(R.id.dbtn1);

    dbtn1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            showDialog(DATE_DIALOG_ID);

        }
    });
}
private void setCurrentDateOnView() {
    // TODO Auto-generated method stub
    tvDisplayDate = (TextView) findViewById(R.id.tvDate);
    dpResult = (DatePicker) findViewById(R.id.dpResult);

    final Calendar c = Calendar.getInstance();
    year = c.get(Calendar.YEAR);
    month = c.get(Calendar.MONTH);
    day = c.get(Calendar.DAY_OF_MONTH);

    // set current date into textview
    tvDisplayDate.setText(new StringBuilder()
        // Month is 0 based, just add 1
        .append(month + 1).append("-").append(day).append("-")
        .append(year).append(" "));

    // set current date into datepicker
    dpResult.init(year, month, day, null);

}
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
       // set date picker as current date
       return new DatePickerDialog(this, datePickerListener, 
                     year, month,day);
    }
    return null;
}

private DatePickerDialog.OnDateSetListener datePickerListener 
            = new DatePickerDialog.OnDateSetListener() {

    // when dialog box is closed, below method will be called.
    public void onDateSet(DatePicker view, int selectedYear,
            int selectedMonth, int selectedDay) {
        year = selectedYear;
        month = selectedMonth;
        day = selectedDay;

        // set selected date into textview
        tvDisplayDate.setText(new StringBuilder().append(month + 1)
           .append("-").append(day).append("-").append(year)
           .append(" "));

        // set selected date into datepicker also
        dpResult.init(year, month, day, null);

    }
};

}

我的json ........它每天都在服务器

中更新
{"results" :[{"id":"34492","vehicle_name":"Innova","driver_name":"Mahesh","coordinates":"17.3510,78.5561","location_name":"Sai Soba Construction, Central Bank Colony, LB Nagar, Hyderabad, Andhra Pradesh 500074, India","speed":"2","posted_on":"2013-04-24 10:33:40","created_on":"2013-04-24 10:33:53"}

1 个答案:

答案 0 :(得分:0)

您可以从here和来自here

的TImePicker找到有关DatePicker的帮助

根据您的JSON(来自here

,按照您想要的格式制作日期

现在您可以轻松过滤数据。谢谢!