Android Open Source - GroupGoal Post Activity






From Project

Back to project page GroupGoal.

License

The source code is released under:

GNU General Public License

If you think the Android project GroupGoal listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.cs121.groupgoal;
/* ww  w.  j  a va 2  s  . co  m*/
import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.DialogFragment;
import android.app.FragmentTransaction;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.parse.ParseException;
import com.parse.ParseGeoPoint;
import com.parse.ParseUser;
import com.parse.SaveCallback;

/**
 * This activity provides the interface to create a goal, specifying name, description, location, privacy, category,
 * date and time of the goal, and will post it to the home page.
 */
@SuppressLint("SimpleDateFormat")
public class PostActivity extends FragmentActivity implements DatePickerDialog.OnDateSetListener, 
  TimePickerDialog.OnTimeSetListener {
  
  // UI references.
  private EditText goalTitleView;
  private EditText goalDescriptionView;
  private EditText goalLocationView;
  private EditText goalSizeView;
  
  private TextView goalDateView;
  private TextView goalTimeView;
  
  private Button createButton;
  private ParseGeoPoint geoPoint;
  
  private CheckBox isPrivateCheckbox;
  private Spinner categoryDropdown;

  private Date date;
  private SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("MM-dd-yyyy");
  private SimpleDateFormat TIME_FORMATTER = new SimpleDateFormat("hh:mm a");
  private SimpleDateFormat DATE_AND_TIME_FORMATTER = new SimpleDateFormat("MM-dd-yyyy hh:mm a");
  
  ParseUser user = ParseUser.getCurrentUser();
  private String goalID;
  
  @SuppressLint("DefaultLocale")
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_post);
    getActionBar().setDisplayShowTitleEnabled(false);
    
    date = new Date();
    
    categoryDropdown = (Spinner)findViewById(R.id.goal_category_spinner);
    String[] items = new String[GoalPost.Category.values().length+1];
    items[0] = "Select One:"; //the first item in the menu; gives the user some guidelines
    for(int i = 1; i < items.length; i++) {
      items[i] = GoalPost.Category.values()[i-1].toString().toLowerCase();
    }
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
    categoryDropdown.setAdapter(adapter);

    //retrieves all the values inputted by the user
    goalTitleView = (EditText) findViewById(R.id.goal_title);
    goalDescriptionView = (EditText) findViewById(R.id.goal_description);
    goalLocationView = (EditText) findViewById(R.id.goal_location);
    goalSizeView = (EditText) findViewById(R.id.goal_group_size_value);
    goalDateView = (TextView) findViewById(R.id.goal_date);
    goalTimeView = (TextView) findViewById(R.id.goal_time);
    isPrivateCheckbox = (CheckBox) findViewById(R.id.goal_checkbox_private);
    createButton = (Button) findViewById(R.id.submit_goal_button); 
    
    createButton.setOnClickListener(new View.OnClickListener() {
    
    @Override
    public void onClick(View v) {
      String goalTitle = goalTitleView.getText().toString();
      String goalDescription = goalDescriptionView.getText().toString();
      String goalLocation = goalLocationView.getText().toString();
      String goalDate = goalDateView.getText().toString();
      String goalTime = goalTimeView.getText().toString();
      boolean goalPrivate = isPrivateCheckbox.isChecked();
      int goalSize = goalSizeView.getText().toString().length() == 0 ? 0 :
        Integer.parseInt(goalSizeView.getText().toString());
      
      Date scheduledDate = parseDate(goalDate + " " + goalTime);
      
      //checks to make sure all fields are valid
      if(titleValid() && descriptionValid() && locationIsValid() && dateAndTimeValid() && categoryValid() ) {
        GoalPost.Category goalCategory = GoalPost.Category.valueOf(
            categoryDropdown.getSelectedItem().toString().toUpperCase());
    
        List<String> attendees = new ArrayList<String>();
        String currentId = ParseUser.getCurrentUser().getObjectId();
        attendees.add(currentId);
        List<String> commentList = new ArrayList<String>();
        
        final GoalPost goal = new GoalPost();
        goal.setOwnerId(currentId);
        goal.setName(goalTitle);
        goal.setDescription(goalDescription);
        goal.setOwner(ParseUser.getCurrentUser());
        goal.setDate(scheduledDate);
        goal.setEventLocation(goalLocation);
        goal.setPrivate(goalPrivate);
        goal.setTargetGroupSize(goalSize);
        goal.setCategory(goalCategory);
        goal.setAttendees(attendees);
        goal.setCommentsList(commentList);
        
        goal.saveInBackground(new SaveCallback() {
              @Override
              public void done(ParseException e) {  // add to get newly created goal ID and store with user
                goalID = goal.getObjectId();
                user.addAllUnique("createdGoals", Arrays.asList(goalID)); //add new goal to the users list of created goals. 
              user.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(ParseException e) {
                      finish();
                    }
                  });
                finish();
              }
            });
          
        Toast.makeText(getApplicationContext(), "Goal Successfully Created", 
            Toast.LENGTH_LONG).show();
        
        Intent i = new Intent(PostActivity.this, MainActivity.class);
        startActivity(i);
      }
    }
  });
  }

  //methods for pop out pickers, drop down menus, and input field validation.
  public void showDatePickerDialog(View v) {
      FragmentTransaction ft = getFragmentManager().beginTransaction();
      DialogFragment newFragment = new DatePickerDialogFragment(PostActivity.this);
      newFragment.show(ft, "date_dialog");
  }

  public void showTimePickerDialog(View v) {
      FragmentTransaction ft = getFragmentManager().beginTransaction();
      DialogFragment newFragment = new TimePickerDialogFragment(PostActivity.this);
      newFragment.show(ft, "time_dialog");
  }

  @Override
  public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
     Calendar cal = Calendar.getInstance();
       cal.set(Calendar.HOUR, hourOfDay);
       cal.set(Calendar.MINUTE, minute);
       goalTimeView.setText(TIME_FORMATTER.format(cal.getTime()));
  }
  
  @Override
  public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
     Calendar cal = new GregorianCalendar(year, monthOfYear, dayOfMonth);
       goalDateView.setText(DATE_FORMATTER.format(cal.getTime()));  
  }
  
  private Date parseDate(String dateAndTime) {
      Date date = null;
        try {
      date = DATE_AND_TIME_FORMATTER.parse(dateAndTime);
    } catch (java.text.ParseException e) {
      e.printStackTrace();
      Log.e("Parse Expiration Date Error", e.getMessage());
    }
        return date;
  }
  
  private boolean titleValid() {
    if(goalTitleView.getText().toString().length() == 0) {
      goalTitleView.setError("Title is a required field.");
      return false;
    } else {
      goalTitleView.setError(null);
      return true;
    }
  }
    
  private boolean descriptionValid() {
    if(goalDescriptionView.getText().toString().length() == 0) {
      goalDescriptionView.setError("Description is a required field.");
      return false;
    } else {
      goalDescriptionView.setError(null);
      return true;
    }
  }
  
  private boolean locationIsValid(){
    if (goalLocationView.getText().toString().length()==0){
      goalLocationView.setError("Please enter a location.");
      return false;
    }
    else {
      goalLocationView.setError(null);
      return true;
    }
  }
  
  private boolean dateAndTimeValid() {
    if(goalDateView.getText().toString().length() == 0 ||
        goalTimeView.getText().toString().length() == 0) {
      goalDateView.setError("Date and Time are required fields.");
      return false;
    } else {
      goalDateView.setError(null);
      goalTimeView.setError(null);
      return true;
    }
  }
  
  @SuppressLint("ShowToast")
  private boolean categoryValid() {
    if(categoryDropdown.getSelectedItem().toString().length() == 0) {
      Toast.makeText(getApplicationContext(), "Category is a required field", 
          Toast.LENGTH_LONG);
      return false;
    } else {
      return true;
    }
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
      // Respond to the action bar's Up/Home button
      case android.R.id.home:
          NavUtils.navigateUpFromSameTask(this);
          return true;
      }
      return super.onOptionsItemSelected(item);
  }
}




Java Source Code List

android.UnusedStub.java
com.cs121.groupgoal.Application.java
com.cs121.groupgoal.CommentActivity.java
com.cs121.groupgoal.Comment.java
com.cs121.groupgoal.ConfigHelper.java
com.cs121.groupgoal.DatePickerDialogFragment.java
com.cs121.groupgoal.DispatchActivity.java
com.cs121.groupgoal.GoalAdapter.java
com.cs121.groupgoal.GoalPost.java
com.cs121.groupgoal.InviteActivity.java
com.cs121.groupgoal.LoginActivity.java
com.cs121.groupgoal.MainActivity.java
com.cs121.groupgoal.MyFriendsActivity.java
com.cs121.groupgoal.NotificationsActivity.java
com.cs121.groupgoal.PostActivity.java
com.cs121.groupgoal.SignUpActivity.java
com.cs121.groupgoal.StartGuideActivity.java
com.cs121.groupgoal.TimePickerDialogFragment.java
com.cs121.groupgoal.UpcomingGoalsActivity.java
com.cs121.groupgoal.UserProfileActivity.java
com.cs121.groupgoal.ViewAttendeesActivity.java
com.cs121.groupgoal.ViewGoal.java
com.cs121.groupgoal.WelcomeActivity.java