gpt4 book ai didi

android - 相对布局中的 ArrayAdapter - Android

转载 作者:行者123 更新时间:2023-11-29 02:02:25 25 4
gpt4 key购买 nike

我正在尝试通过实现 developer.android.com 上给出的示例重新熟悉 Android .

我正在尝试 this example在相对布局上。虽然布局对我来说很清楚,但我无法向此处提供的微调器添加功能。这是我正在尝试做的事情:
让用户可以选择 future 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.");
}


}

最佳答案

您必须创建一个 Arraylist,然后将 date.tostring 添加到此 arraylist,而不是将 Date 对象添加到适配器。

使用这个 ArrayList 创建一个 ArrayAdapter

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

关于android - 相对布局中的 ArrayAdapter - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11964844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com