我正在尝试通过实现developer.android.com上给出的示例来重新熟悉 Android 。
我正在RelativeLayouts 上尝试这个例子。虽然布局对我来说很清楚,但我无法为那里给出的微调器添加功能。这是我想做的事情:
让用户可以选择从未来 7 天的任何一天中选择一个日期来设置警报。为此,我想使用ArrayAdapter
每次启动应用程序时动态设置的微调器来填充微调器。的内容ArrayAdapter
将是当前日期和接下来的 7 天。我无法初始化ArrayAdapter
.
鉴于内容是动态的,是否ArrayAdapter
最好使用数据结构?
编码:
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<EditText android:id="@+id/reminderName"
android:hint="@string/reminderNameString"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
<Spinner android:id="@+id/dateSelect"
android:layout_below="@id/reminderName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/timeSelect"
android:hint="@string/selectDate"
/>
<Spinner android:id="@id/timeSelect"
android:hint="@string/selectTimeString"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@id/reminderName"
/>
<Button android:id="@+id/setButton"
android:hint="@string/setString"
android:layout_below="@id/timeSelect"
android:layout_alignRight="@id/reminderName"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:onClick="onSet"
/>
<TextView android:id="@+id/text"
/>
</RelativeLayout>
主要功能:
package com.example.relativelayout;
import java.util.ArrayList;
import java.util.Date;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class MainActivity extends Activity {
Spinner dates;
Spinner times;
@Override
public void onCreate(Bundle savedInstanceState) {
/* This function must move like so:
* 1. Generate the dates.
* 2. Add them to an arrayList
* 3. Set the data via an adapter into the spinner.
* 4. Set the layout.
*/
super.onCreate(savedInstanceState);
Log.v(this.toString(), "Loaded the layout");
//init ArrayList object to store all the dates.
ArrayList<String> dates_list = new ArrayList<String>(8); //there are always only 7 days.
Date d = new Date(); //initialize the date to current date and time.
dates_list.add(d.toString());
//GregorianCalendar g = new GregorianCalendar();
Log.v(this.toString(), "Current date = " + d.toString());
for(int i = 0; i < 7; i++) {
if( (d.getMonth() % 2 != 0) || (d.getMonth() == 8) ) {
//the month has 31 days.
int dateAdj = d.getDate();
if( (dateAdj + i) > 31 ) {
dateAdj = (dateAdj + i) - 31;
d.setDate(dateAdj);
d.setMonth(d.getMonth() + 1);
} else {
d.setDate((dateAdj + 1));
}
} else { //the month has only 30 days
int dateAdj = d.getDate();
if( (dateAdj + i) > 30 ) {
dateAdj = (dateAdj + i) - 30;
d.setDate(dateAdj);
d.setMonth(d.getMonth() + 1);
} else {
d.setDate((dateAdj + 1));
}
}
dates_list.add(d.toString()); //add it to the list.
//Log.v(this.toString(), "The date today = " + d.toString());
}
Log.v(this.toString(), "Printing details of the arrayList");
/*for(int i = 0; i < 8; i++) {
Log.v(this.toString(), "Date in position " + i + " " + dates_list.get(i));
}*/
//have all the dates in the prescribed format. add it to the spinners.
ArrayAdapter<String> array_adapter_dates = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dates_list);
Log.v(this.toString(), "Adding dates to array adapters.");
//init the spinner from the layouts.
dates = (Spinner) findViewById(R.id.dateSelect); //got the spinner
array_adapter_dates.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Log.v(this.toString(), "Spinners initialized. Moving to apply adapters to the spinner.");
//apply adapter to the spinner.
dates.setAdapter(array_adapter_dates);
Log.v(this.toString(), "Applied adapter to the spinner.");
//set the layout
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void onSet(View view) {
/*This is the function called when the Set button is pressed.
* 1. Take the details of the spinners.
* 2. Flash them.
* 3. Set an alarm with it.
*/
Log.v(this.toString(), "onSet() function inboked.");
}
}