我的孩子活动不会将结果发送回父母

时间:2012-12-21 03:42:07

标签: android android-intent android-activity

我正在尝试为我所在的类创建ToDo列表,以及我如何拥有该应用程序是因为我在顶部有一个EditText来输入TDL中的项目。然后我使用扩展的tablelayout来显示项目。我还有一个按钮,调用第二个活动,然后有另外两个按钮,以便在我按下按钮上的完成按钮后(希望)显示在TDL项目上的时间和日期(我希望返回到主要活动)到目前为止,我正在AVD上测试这个。

所以我认为我的问题是我不认为我的第二项活动是传回我想要的信息。根据调试器,第一个onActivityResults方法的调用早于第二个调用的调用。

这是第一个活动:

package com.parrishb.todo;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;

public class ToDoActivity extends Activity {

    SharedPreferences pref; 

    public final static int ACTIVITY =1;

    public static Globals g;

    public ToDoView tdv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_to_do);

        final EditText myEditText = (EditText)findViewById(R.id.myEditText);

        Button mPickDate = (Button) findViewById(R.id.pickDate);
        tdv = (ToDoView)findViewById(R.id.tdv);


        myEditText.setOnKeyListener(new View.OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if(event.getAction()== KeyEvent.ACTION_DOWN){
                    if((keyCode == KeyEvent.KEYCODE_DPAD_CENTER) || (keyCode == KeyEvent.KEYCODE_ENTER)){
                        tdv.addRow(ToDoActivity.this, myEditText.getText().toString());

                        myEditText.setText("");
                        return true;
                    }
                }
                return false;
            }
        });


        // CLick listener for the date/time button
        mPickDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(ToDoActivity.this, DatePickerActivity.class), ACTIVITY);
            }
        });

    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode== RESULT_OK && requestCode== ACTIVITY){
            String append= (new StringBuilder().append(data.getStringExtra("Time"))
                    .append(" ").append(data.getStringExtra("Date")).toString());
            tdv.editRow(ToDoActivity.this, append);
        }

    }




}

这是我的第二项活动:

package com.parrishb.todo;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
public class DatePickerActivity extends Activity {

    private TextView mDateDisplay;
    private Button mPickDate;
    private int mYear;
    private int mMonth;
    private int mDay;
    private TextView mTimeDisplay;
    private Button mPickTime;

    private Button done;

    private int mhour;
    private int mminute;

    static final int TIME_DIALOG_ID = 1;

    static final int DATE_DIALOG_ID = 0;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.buttonclick);

        mDateDisplay =(TextView)findViewById(R.id.date);
        mPickDate =(Button)findViewById(R.id.datepicker);
        mTimeDisplay = (TextView) findViewById(R.id.time);
        mPickTime = (Button) findViewById(R.id.timepicker);
        done = (Button)findViewById(R.id.done);


        //Pick time's click event listener
        mPickTime.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                showDialog(TIME_DIALOG_ID);


            }

        });

        //PickDate's click event listener 
        mPickDate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);


            }
        });

        done.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //DatePickerActivity.this.finishActivityFromChild(DatePickerActivity.this, ToDoActivity.ACTIVITY);
                //finishActivity(ToDoActivity.ACTIVITY);
                //return;
                //DatePickerActivity.this.finish(ToDoActivity.ACTIVITY);
                //finish();
                DatePickerActivity.this.finish();

            }
        });

        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);
        mhour = c.get(Calendar.HOUR_OF_DAY);
        mminute = c.get(Calendar.MINUTE);




    }


    @Override
    public void finish(){
        Intent resultIntent = new Intent();
        Bundle b = new Bundle();
            b.putString("Date", mDateDisplay.getText().toString());
            b.putString("Time", mTimeDisplay.getText().toString());
        resultIntent.putExtras(b);
        setResult(Activity.RESULT_OK, resultIntent);
        super.finish();
    }

    //-------------------------------------------update date----------------------------------------//    
    private void updateDate() {

        mDateDisplay.setText(
                new StringBuilder()
                // Month is 0 based so add 1
                .append(mMonth + 1).append("/")
                .append(mDay).append("/")
                .append(mYear).append(" "));

    }

    //-------------------------------------------update time----------------------------------------//    
    public void updatetime()
    {
        mTimeDisplay.setText(
                new StringBuilder()
                .append(pad(mhour)).append(":")
                .append(pad(mminute))); 

    }

    private static String pad(int c) {
        if (c >= 10)
            return String.valueOf(c);
        else
            return "0" + String.valueOf(c);
    }




    //Datepicker dialog generation  

    private DatePickerDialog.OnDateSetListener mDateSetListener =
            new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, 
                int monthOfYear, int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            updateDate();
        }
    };


    // Timepicker dialog generation
    private TimePickerDialog.OnTimeSetListener mTimeSetListener =
            new TimePickerDialog.OnTimeSetListener() {
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            mhour = hourOfDay;
            mminute = minute;
            updatetime();
        }


    };

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this,
                    mDateSetListener,
                    mYear, mMonth, mDay);

        case TIME_DIALOG_ID:
            return new TimePickerDialog(this,
                    mTimeSetListener, mhour, mminute, true);

        }
        return null;
    }
}

1 个答案:

答案 0 :(得分:0)

我抓住了第二项活动并将日期和时间选择器带入了主要活动。