2012-08-15 4 views
2

J'essaie de me re-familiariser avec Android en mettant en œuvre les exemples donnés sur developer.android.com. J'essaie this example sur RelativeLayouts. Bien que la mise en page soit claire pour moi, je ne suis pas en mesure d'ajouter de la fonctionnalité aux filateurs donnés ici. Voici ce que j'essaie de faire:
Donner à l'utilisateur la possibilité de choisir une date parmi les 7 prochains jours pour régler une alarme. Pour cela, je veux remplir le spinner avec un ArrayAdapter qui est défini dynamiquement chaque fois que l'application est démarrée. Le contenu du ArrayAdapter serait la date du jour et les 7 prochains jours. Je suis incapable d'initialiser le ArrayAdapter.
Étant donné que le contenu est dynamique, ArrayAdapter est la meilleure structure de données à utiliser?ArrayAdapter dans la disposition relative - Android

Le code:

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> 

La fonction principale:

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


} 

Répondre

1

Vous devez créer un Arraylist puis ajoutez le Date.toString à ce arraylist plutôt que d'ajouter objet Date à l'adaptateur.

En utilisant cette ArrayList créer un ArrayAdapter

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

Merci pour votre réponse. L'initialisation de arrayList a aidé. Après avoir initialisé le arrayAdapter, je suis incapable de remplir le spinner avec son contenu. J'ai édité la classe principale de manière appropriée. Plus d'aide est nécessaire! – Sriram

+0

déplacez setContentView (R.layout.activity_main); immédiatement après super.oncreate, sinon findviewbyid ne fonctionne pas – nandeesh

+0

qui a fonctionné. Je suppose que j'ai beaucoup à réapprendre! Merci! – Sriram

Questions connexes