1

So I made an Activity with textViews, editTexts, date dialog and time dialog picker. plus a button.

I used this to get the Date/Time dialogs to work:

public class RainbowBookActivity extends Activity {

private static final int DATE_DIALOG_ID=1; // for date 
  private static final  int TIME_DIALOG_ID=2; // for month 
  private int d,mo,y,h,m; // for date & month variables 
  Button b1,b2; // button objects 
  TextView e1,e2;  // textview objects

  // execution starts from here 
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_rainbowbook);  // calling activity_rainbowbook.xml 
     e1=(TextView)findViewById(R.id.textview1); // getting textview1 id from activity_rainbowbook.xml 
     b1=(Button)findViewById(R.id.button1); // getting  button id from activity_rainbowbook.xml

   b1.setOnClickListener(new OnClickListener() // setting listener for button one  

   {

   public void onClick(View arg0) {


    // TODO Auto-generated method stub
    showDialog(DATE_DIALOG_ID);   // generating dialog box 
     }
  });

  final Calendar cal=Calendar.getInstance(); // allocating memory for calendar instance
   d=cal.get(Calendar.DAY_OF_MONTH); // present date        
  mo=cal.get(Calendar.MONTH); // present month
     y=cal.get(Calendar.YEAR); // present year
     updateDate();  // update date 

  b2=(Button)findViewById(R.id.button2); // getting listener for button2 
   e2=(TextView)findViewById(R.id.textview2); 
   b2.setOnClickListener(new OnClickListener() // setting listener for button2 

   {
    public void onClick(View arg0) {


    // TODO Auto-generated method stub
    showDialog(TIME_DIALOG_ID);
 }
});

  h=Calendar.getInstance().get(Calendar.HOUR); // getting  present hour & minute
  m=Calendar.getInstance().get(Calendar.MINUTE);
  updateTime();  // updating time 
}


public void updateTime()
{

   e2.setText(new StringBuilder().append(h).append(':').append(m));

}
public void updateDate()
{

   e1.setText(new StringBuilder().append(d).append('/').append(mo+1).append('/').append(y));

  }


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


   public void onDateSet(DatePicker view,int year, int monthofyear, int day) 
   {

                     y=year;
                     mo=monthofyear;
                     d=day;
                     updateDate();
  }
 };

  private TimePickerDialog.OnTimeSetListener timelistener=new TimePickerDialog.OnTimeSetListener() 
  {
   public void onTimeSet(TimePicker view, int hourofday, int minute) 
   {
    // TODO Auto-generated method stub
    h=hourofday;
    m=minute;
     updateTime();
   }
 };

 protected Dialog onCreateDialog(int id) 
 {
    switch(id)
    {
      case DATE_DIALOG_ID:
                   return new DatePickerDialog(this,datelistener , y, mo, d);


      case TIME_DIALOG_ID:
                  return new TimePickerDialog(this,timelistener,h,m,false);
  }
  return null;
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_rainbowbook, menu);
    return true;
}

}

Now I would some how like to get this in there so I can have a button that transferes info from the editTexts:

    EditText inputName;
    EditText inputRoom;

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

        inputName = (EditText) findViewById(R.id.editText1);
        inputRoom = (EditText) findViewById(R.id.editText2);
        Button btnNextScreen = (Button) findViewById(R.id.button3);

        //Listening to button event
        btnNextScreen.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                //Starting a new Intent
                Intent nextScreen = new Intent(getApplicationContext(), BookingsActivity.class);

                //Sending data to another Activity
                nextScreen.putExtra("name", inputName.getText().toString());
                nextScreen.putExtra("room", inputRoom.getText().toString());

                Log.e("n", inputName.getText()+"."+ inputRoom.getText());

                startActivity(nextScreen);

            }
        });
    }

I cant seem to figure this out and am hoping someone can help me. Thanks.

* BookingsActivity:

public class BookingsActivity extends Activity {

   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bookings);

        TextView Name = (TextView) findViewById(R.id.textView2);
        TextView Room = (TextView) findViewById(R.id.textView3);
        Button btnClose = (Button) findViewById(R.id.btnClose);

        Intent i = getIntent();
        // Receiving the Data
        String name = i.getStringExtra("name");
        String room = i.getStringExtra("room");
        Log.e("Bookings", name + "." + room);

        // Displaying Received data
        Name.setText(name);
        Room.setText(room);

        // Binding Click event to Button
        btnClose.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                //Closing SecondScreen Activity
                finish();
            }
        });

    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_bookings, menu);
    return true;
}

}

Da Iceman
  • 21
  • 3
  • whats the problem again? oh, inputName and inputRoom have to be declared final for this to work, otherwise you'll get compaints from eclipse. was that it? – Shark Jul 30 '12 at 20:37
  • Right. Please give a problem so that we can solve it. If you can't compile give the error. If it crashes your apps, give the stack trace and culprit lines. – Snicolas Jul 30 '12 at 20:40
  • 1
    post BookingsActivity code please. also, consider using startActivityForResult(nextScreen, some_final_number); and fetching it's result in onActivityResult() callback – Shark Jul 30 '12 at 20:40
  • I added it in, as is, at the end of Public Void onCreate(). under everything else and above public boolean. and it compiles no errors. just the button3 doesn't work. and info isn't sent to the next activity. – Da Iceman Jul 30 '12 at 20:49
  • button3?? you mean btnNextScreen? – Shark Jul 30 '12 at 21:02
  • yea. sorry. was calling it button3 cause thats its xml name (or Res id). but yea btnNextScreen – Da Iceman Jul 30 '12 at 21:06
  • gimme these two lines Log.e("n", inputName.getText()+"."+ inputRoom.getText()); and Log.e("Bookings", name + "." + room); – Shark Jul 30 '12 at 21:09

1 Answers1

0
 maybe this in your BookingActivity

 Bundle b = i.getExtras();
 String name = b.getString("name", "default name");
 String room = b.getString("room", "default room");

edit: also, launche BookingActivity with startActivityForResult() so you can get data back from it later when you're done with it.

EDIT: examples of startActivityForResult can be found How to return a result (startActivityForResult) from a TabHost Activity? and http://android.rahulblogs.com/android-startactivityforresult-example/

Community
  • 1
  • 1
Shark
  • 6,513
  • 3
  • 28
  • 50
  • Sorry, I'm quiet a noob at this. and I learnt both of these things from tutorials. (was wondering if they can work together or im just going at this completely the wrong way. Anyways. Adding the above game me errors until i removed default name/room. And im not sure where startActivityForResult() should go. thanks – Da Iceman Jul 31 '12 at 14:30
  • instead of calling startActivity(Intent i) call startActivityForResult(Intent i, int requestCode); also, please, i cannot stress this enough..... post your errors, or do you expect me to magically guess them? help me help you. – Shark Jul 31 '12 at 14:47