时间选择器显示到listview的数据

时间:2016-09-04 07:54:36

标签: java android listview android-timepicker

我的timepicker在另一个布局中,我想再次将它显示到另一个布局的列表视图中...我该怎么做,这是我的代码。

ArrayList<String> listItems=new ArrayList<String>();
ArrayAdapter<String> adapter;
CalendarView cv;
private TimePicker timePicker;
private TextView time;
private Calendar calendar;
private String format = "";



@Override
protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_main);
    cv = (CalendarView)findViewById(R.id.calendarView);
    cv.setShowWeekNumber(false);


    adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,listItems);
    ListView list = (ListView)findViewById(R.id.eventss);
    list.setAdapter(adapter);
    adapter.notifyDataSetChanged();



    FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {

            final View view1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_add_event,null);
                    final AlertDialog.Builder builderDialog = new AlertDialog.Builder(MainActivity.this);
            builderDialog.setTitle("Set Your Event");


            builderDialog.setView(view1)
                    .setPositiveButton("Save", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            EditText et = (EditText)view1.findViewById(R.id.eventName);
                            timePicker = (TimePicker)view1.findViewById(R.id.timePicker);
                            time = (TextView)findViewById(R.id.textView2);

                            int hour = calendar.get(Calendar.HOUR_OF_DAY);
                            int min = calendar.get(Calendar.MINUTE);
                            showTime(hour, min);

这是我wana显示它的地方                                 listItems.add(et.getText()的toString());                                 adapter.notifyDataSetChanged();

                        }

                    })
                    .setNegativeButton("Cancel",null)
                    .setCancelable(false);

            AlertDialog alert = builderDialog.create();
            alert.show();

        }
    });



}

我的格式

public void setTime(View view){
    int hour = timePicker.getCurrentHour();
    int min = timePicker.getCurrentMinute();
    showTime(hour, min);
}

private void showTime(int hour, int min) {
    if(hour == 0){
        hour += 12;
        format = "AM";
    }
    else if (hour == 12){
        format = "PM";
    }else if (hour > 12){
        hour -= 12;
        format = "PM";
    }else{
        format = "AM";
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

0 个答案:

没有答案