2012-08-15 35 views
2

我試圖通過實現developer.android.com上給出的示例來重新熟悉Android。相對佈局中的ArrayAdapter - Android

我想在RelativeLayouts上使用this example。雖然佈局對我來說很清楚,但我無法爲那裏的紡紗人員添加功能。以下是我正在嘗試執行的操作:
爲用戶提供從未來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.");   
    } 


} 

回答

1

您必須創建一個ArrayList那麼date.tostring增加此ArrayList,而不是增加Date對象適配器。

使用此ArrayList創建一個ArrayAdapter

array_adapter_dates = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, Arraylist<String>) 
+0

謝謝您的回答。初始化arrayList確實有幫助。在初始化arrayAdapter之後,我無法用其內容填充微調器。我已經適當地編輯了主要課程。需要更多幫助! – Sriram 2012-08-16 03:40:18

+0

移動setContentView(R.layout.activity_main); super.oncreate後立即,否則findviewbyid不工作 – nandeesh 2012-08-16 10:24:50

+0

工作。猜猜我有很多要重新學習!謝謝! – Sriram 2012-08-16 18:46:59