Android Open Source - PlayTogether Wanna Play Fragment






From Project

Back to project page PlayTogether.

License

The source code is released under:

This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a co...

If you think the Android project PlayTogether 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.rockylearnstodevelop.playtogether;
//w  w  w  .j  ava2  s. co  m
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

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

public class WannaPlayFragment extends Fragment {

  protected Spinner sportSpinner;
  protected Spinner timeSpinner;
  protected Spinner levelSpinner;
  protected Button findButton;
  
  protected String selectedSport;
  protected String selectedTime;
  protected String selectedLevel;
  
  protected String[] name_Sport ={ParseConstants.KEY_BADMINTON, ParseConstants.KEY_PINGPONG,
      ParseConstants.KEY_RUNNING,
      ParseConstants.KEY_TENNIS };
  protected String[] time_Sport = {"Saturday afternoon","Sunday morning", "Sunday afternoon" };
  protected String[] level_Sport = {"Beginner", "Intermediate", "Dancer"};
  
  private ParseUser currentUser; //current user
  private String currentUserId; //current userId
  private String currentUserName; //current name
  private String birthday;
  
  private static final String TAG = WannaPlayFragment.class.getSimpleName();

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    
    View rootView = inflater.inflate(R.layout.fragment_wanna_play, container, false);

    setSpinnerContent(rootView);
    
    Intent mIntent = getActivity().getIntent();
    
    findButton = (Button) rootView.findViewById(R.id.button_find_partner);
    findButton.setOnClickListener(new View.OnClickListener() {
      
      @Override
      public void onClick(View arg0) {
        //upload the user's data  
        getActivity().setProgressBarIndeterminateVisibility(true);
        UploadAndQuery uploadAndQuery = new UploadAndQuery();
        uploadAndQuery.execute();
      }
    });
    return rootView;
  }
  
  private void setSpinnerContent(View view) {
    sportSpinner = (Spinner) view.findViewById(R.id.sportSpinner);
    timeSpinner = (Spinner) view.findViewById(R.id.timeSpinner);
    levelSpinner = (Spinner) view.findViewById(R.id.levelSpinner);
    
    //Set the Sports Spinner
    ArrayAdapter<String> sportAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, name_Sport);
    sportAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
    sportSpinner.setAdapter(sportAdapter);
    
    //Set the Time Spinner
    ArrayAdapter<String> timeAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, time_Sport);
    timeAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
    timeSpinner.setAdapter(timeAdapter);
    
    //Set the Level Spinner
    ArrayAdapter<String> levelAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, level_Sport);
    levelAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
    levelSpinner.setAdapter(levelAdapter);
  }

  private class UploadAndQuery extends AsyncTask<Void, Void, Void>{
    @Override
    protected Void doInBackground(Void... params) {
      uploadData();
      return null;
    }
    
    @Override
    protected void onPostExecute(Void result) {
//      navigateToMatch();
      
      super.onPostExecute(result);
    }

    
  }
  
  private void navigateToMatch() {
    Intent intent = new Intent(getActivity(), MatchActivity.class);
    intent.putExtra(ParseConstants.KEY_SPORT, selectedSport);
    intent.putExtra(ParseConstants.KEY_LEVEL, selectedLevel);
    intent.putExtra(ParseConstants.KEY_TIME, selectedTime);
    startActivity(intent);
  }
  
  public void uploadData() {  
    //get the text from Spinners
    selectedSport = sportSpinner.getSelectedItem().toString();    
    selectedTime = timeSpinner.getSelectedItem().toString();    
    selectedLevel = levelSpinner.getSelectedItem().toString();
    
    navigateToMatch();
    
    //get the current user's info
    currentUser = ParseUser.getCurrentUser();
    currentUserId = currentUser.getObjectId();
    currentUserName = currentUser.getUsername();
    
    //upload the sport Activity to the parse.com
    ParseObject sportActivity = new ParseObject(ParseConstants.CLASS_SPORTACTIVITY);
    sportActivity.put(ParseConstants.KEY_TIME, selectedTime);
    sportActivity.put(ParseConstants.KEY_SPORT, selectedSport);
    sportActivity.put(ParseConstants.KEY_LEVEL, selectedLevel);
    sportActivity.put(ParseConstants.KEY_USERID, currentUserId);
    sportActivity.put(ParseConstants.KEY_USERNAME, currentUserName);
    sportActivity.put(ParseConstants.KEY_ALONE, true);
    
    sportActivity.saveInBackground(new SaveCallback() {
      @Override
      public void done(ParseException e) {
        Log.d(TAG, selectedSport);
        if(e == null){
          //upload success
          Log.d(TAG, "upload sport Activity successful");
        }else{
          //error!
          Log.i(TAG, e.getMessage());
        }
      }
    });
  }

}




Java Source Code List

com.rockylearnstodevelop.playtogether.ActivitiesFragment.java
com.rockylearnstodevelop.playtogether.ActivityAdapter.java
com.rockylearnstodevelop.playtogether.FileHelper.java
com.rockylearnstodevelop.playtogether.ImageResizer.java
com.rockylearnstodevelop.playtogether.LoginActivity.java
com.rockylearnstodevelop.playtogether.MainActivity.java
com.rockylearnstodevelop.playtogether.MatchActivity.java
com.rockylearnstodevelop.playtogether.NoMatchActivity.java
com.rockylearnstodevelop.playtogether.ParseConstants.java
com.rockylearnstodevelop.playtogether.PlayWithMe2Application.java
com.rockylearnstodevelop.playtogether.RequestAdapter.java
com.rockylearnstodevelop.playtogether.RequestFragment.java
com.rockylearnstodevelop.playtogether.SectionsPagerAdapter.java
com.rockylearnstodevelop.playtogether.SignupActivity.java
com.rockylearnstodevelop.playtogether.UserInfoActivity.java
com.rockylearnstodevelop.playtogether.WannaPlayFragment.java