私は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.");
}
}