安卓工作室列表视图与日期和时间选择器

时间:2017-03-12 15:26:39

标签: java android listview

我正在尝试使用日期和时间选项创建待办事项列表。我想将它放在列表视图中,并在其旁边显示日期。我创建了单独的日期和时间选择器,我只需要知道如何使用它们来创建列表视图。我一个月前才开始编码,所以我真的不知道我在大部分时间做了什么。这是带有日期和时间选择器的代码,如果有帮助的话。任何建议都是适用的。

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.util.DisplayMetrics;
import android.util.TimeUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

import java.util.Calendar;

public class Activity_act extends AppCompatActivity implements View.OnClickListener {

    Button btnTime, btnDate;
    TextView tvTime, tvDate;

    TimePickerDialog timePickerDialog;
    DatePickerDialog datePickerDialog;

    Calendar calendar = Calendar.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity);

        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager(). getDefaultDisplay(). getMetrics(dm);

        int width = dm.widthPixels;
        int height = dm.heightPixels;

        getWindow().setLayout((int)(width*.8),(int)(height*.6));

        btnTime = (Button) findViewById(R.id.PickTime);
        btnTime.setOnClickListener(this);
        btnDate = (Button) findViewById(R.id.PickDate);
        btnDate.setOnClickListener(this);

        tvTime = (TextView) findViewById(R.id.ShowTime);
        tvDate = (TextView) findViewById(R.id.ShowDate);
    }

    @Override
    public void onClick(View v) {

        calendar = Calendar.getInstance();

        switch (v.getId()){

            case R.id.PickTime:{

                timePickerDialog = new TimePickerDialog(Activity_act.this, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

                        Calendar timeCalendar = Calendar.getInstance();
                        timeCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                        timeCalendar.set(Calendar.MINUTE, minute);

                        String timestring = DateUtils.formatDateTime(Activity_act.this, timeCalendar.getTimeInMillis(), DateUtils.FORMAT_SHOW_TIME);
                        tvTime.setText(timestring);


                    }
                },calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), android.text.format.DateFormat.is24HourFormat(Activity_act.this));

                timePickerDialog.show();
                break;
            }

            case R.id.PickDate:{

                datePickerDialog = new DatePickerDialog(Activity_act.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

                        Calendar dateCalendar = Calendar.getInstance();
                        dateCalendar.set(Calendar.YEAR, year);
                        dateCalendar.set(Calendar.MONTH, monthOfYear);
                        dateCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

                        String dateString = DateUtils.formatDateTime(Activity_act.this, dateCalendar.getTimeInMillis(), DateUtils.FORMAT_SHOW_DATE);
                        tvDate.setText(dateString);

                    }
                }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));

                datePickerDialog.show();
                break;
            }


        }

    }
}

1 个答案:

答案 0 :(得分:0)

您需要为listView创建一个Adapter。我希望本教程可以帮助您 listview with custom adapter example tutorial