如何将数据从日期选择器和微调器传递到另一个活动

时间:2012-01-03 02:50:47

标签: android android-activity android-intent

我是Android开发人员的开始。

我需要将数据从两个日期选择器和一个微调器传递到另一个活动。

有人可以帮我解决这个问题:

package qi.com;
import java.text.DateFormat;
import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

//multi date input
public class CoaActivity extends Activity implements AdapterView.OnItemSelectedListener {
    TextView selection;
    TextView mDateStart;
    TextView mDateEnd;
    Button mPickStart;
    Button mPickEnd;
    Calendar startDate;
    Calendar EndDate;
    int mYear;
    int mMonth;
    int mDay;

    //list menu
    String[] items = {"Pendapatan","Biaya"};

    static final int DATE_DIALOG_ID = 0;

    private TextView activeDateDisplay;
    private Calendar activeDate;



    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //display to text view from list View
        selection = (TextView) findViewById(R.id.selection);
        Spinner spin= (Spinner) findViewById(R.id.spinner);
        spin.setOnItemSelectedListener(this);

        ArrayAdapter<String> aa= new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, items);
        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spin.setAdapter(aa);

        //capture view elements
        mDateStart = (TextView) findViewById(R.id.DateStar);
        mPickStart = (Button) findViewById(R.id.btn_PickDateStart);

        //current date
        startDate = Calendar.getInstance();

        //listener click button
        mPickStart.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDateDialog(mDateStart, startDate);
            }
        });

        //END Date
        mDateEnd = (TextView) findViewById(R.id.DateEnd);
        mPickEnd = (Button) findViewById(R.id.btn_PickDateEnd);

        EndDate = Calendar.getInstance();

        //listener click button
        mPickEnd.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDateDialog(mDateEnd, EndDate);
            }
        });


        //display current date
        updateDisplay(mDateStart, startDate);
        updateDisplay(mDateEnd, EndDate);

        //Detail("tes");

    }

    private void updateDisplay(TextView dateDisplay, Calendar date) {
        dateDisplay.setText(
                new StringBuilder()
                    // Month is 0 based so add 1
                    //.append(date.get(Calendar.DAY_OF_MONTH)).append("-")
                    .append(date.get(Calendar.DATE)).append("-")
                    .append(date.get(Calendar.MONTH) + 1).append("-")
                    .append(date.get(Calendar.YEAR)).append(" "));

                //.append(DateFormat.getDateInstance(Calendar.MONTH)));

    }

    public void showDateDialog(TextView dateDisplay, Calendar date) {
        activeDateDisplay = dateDisplay;
        activeDate = date;
        showDialog(DATE_DIALOG_ID);
    }


    private OnDateSetListener dateSetListener = new OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            activeDate.set(Calendar.YEAR, year);
            activeDate.set(Calendar.MONTH, monthOfYear);
            //activeDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            activeDate.set(Calendar.DATE, dayOfMonth);
            updateDisplay(activeDateDisplay, activeDate);
            unregisterDateDisplay();


        }
    };




    private void unregisterDateDisplay() {
        activeDateDisplay = null;
        activeDate = null;
    }

    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_DIALOG_ID:

                return new DatePickerDialog(this, dateSetListener, activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DATE));
        }
        return null;
    }

    protected void onPrepareDialog(int id, Dialog dialog) {
        super.onPrepareDialog(id, dialog);
        switch (id) {
            case DATE_DIALOG_ID:
                ((DatePickerDialog) dialog).updateDate(activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DATE));
                break;
        }
    }

    public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
        selection.setText(items[position]);

        Intent grid=new Intent(this, Grid.class);
        this.startActivity(grid);

        //send to another activity
        //Detail(items[position]);

    }

    public void onNothingSelected(AdapterView<?> parent) {
        selection.setText("");
    }


    //another activity
    public void Detail(String id)
    //public void Detail()
    {   
        //Intent detail=new Intent(con, Detail.class);

        Intent grid=new Intent(this, Grid.class);
        grid.putExtra("IDP", id);
        this.startActivity(grid);


        //Toast.makeText(this, "list : " , Toast.LENGTH_SHORT).show();
    }
}

这是xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Mulai dari :" />

    <TextView
        android:id="@+id/DateStar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="" />

    <Button
        android:id="@+id/btn_PickDateStart"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Tanggal Awal" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Sampai dengan :" />

     <TextView
        android:id="@+id/DateEnd"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="" />

    <Button
        android:id="@+id/btn_PickDateEnd"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Tanggal Akhir" />


    <TextView
        android:id="@+id/selection"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>


    <Spinner
        android:id="@+id/spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="false" />

 </LinearLayout>

1 个答案:

答案 0 :(得分:0)

使用long为您提供的Date值,并将其添加到其他内容中,以及您发送给活动的所有内容。