1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
| package ch.srvinfo.interfire.interfire;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
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.TimePicker;
import android.widget.Toast;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Inter extends AppCompatActivity {
//Création button heure date
Date d = new Date();
SimpleDateFormat f = new SimpleDateFormat("HH:mm");
String heure = f.format(d);
private Bundle NameChef = null;
private String namefile = ".csv";
private String userName = "Chef d'inter;";
private String userName2 = "Nom inter";
private String orderName = "ordre";
private File mFile = null;
private Button mWrite = null;
private Button mRead = null;
private String Textv;
Button btnSelectDate, btnSelectTime;
static final int DATE_DIALOG_ID = 0;
static final int TIME_DIALOG_ID = 1;
// variables to save user selected date and time
public int year, month, day, hour, minute;
// declare the variables to Show/Set the date and time when Time and Date Picker Dialog first appears
private int mYear, mMonth, mDay, mHour, mMinute;
// constructor
public Inter() {
// Assign current Date and Time Values to Variables
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH)+1;
mDay = c.get(Calendar.DAY_OF_MONTH);
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inter);
/////////////////////////////Tech preference
// SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
// final String NameCIOK = preferences.getString("VIntent", "NameChef");
//////////////////////////////// Avec Intent
//Dans onCreate() par exemple...
// Bundle bundle = getIntent().getExtras();
// String act2MyValue= bundle.getString(NameChef);
Bundle data = getIntent().getExtras();
String nom_utilisateur= bundle.getString("nom");
/////////////////////////////////////////////
//création des bouton heure et dates
// get the references of buttons
btnSelectDate=(Button)findViewById(R.id.buttonSelectDate);
btnSelectTime=(Button)findViewById(R.id.buttonSelectTime);
// Set ClickListener on btnSelectDate
btnSelectDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Show the DatePickerDialog
showDialog(DATE_DIALOG_ID);
}
});
// Set ClickListener on btnSelectTime
btnSelectTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Show the TimePickerDialog
showDialog(TIME_DIALOG_ID);
}
});
/////////////////////
Spinner spinner = (Spinner) findViewById(R.id.ordre_spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.ordre, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
final int mois = d.getMonth() + 1;
final int annees = d.getYear() -100;
final int jours = d.getDay();
final String datesok = jours + "." + mois + "." + annees;
mFile = new File(this.getExternalFilesDir(null),"Intervention du_"+d.getDay()+"_"+mois+"_"+annees+"_");
mWrite = (Button) findViewById(R.id.Sauvegarde);
mWrite.setOnClickListener(new View.OnClickListener() {
public void onClick(View pView) {
//récuperation de l'ordre au clique
//Récuperation ordre
Spinner spinner = (Spinner) findViewById(R.id.ordre_spinner);
String text = spinner.getSelectedItem().toString();
//test de l'ecriture du fichier
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
Toast.makeText(Inter.this, "test", Toast.LENGTH_SHORT).show();
else
Toast.makeText(Inter.this, "Erreur : ", Toast.LENGTH_SHORT).show();
try {
// Flux interne
FileOutputStream output = openFileOutput(namefile, MODE_PRIVATE);
//écriture interne pas indispansable
// On écrit dans le flux interne
output.write(userName.getBytes());
output.write(userName2.getBytes());
// output.write(Heure.getBytes());
// output.write("cellule1,");
//output.write("cellule2");
output.flush();
output.close();
output = null;
//output.write(userName.getBytes());
if (output != null)
output.close();
// Si le fichier est lisible et qu'on peut écrire dedans
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
&& !Environment.MEDIA_MOUNTED_READ_ONLY.equals(Environment.getExternalStorageState())) {
// On crée un nouveau fichier. Si le fichier existe déjà, il ne sera pas créé
//création de variable pour saut de cellule
String Cellule = ";";
String Saut = "\n";
String NameCI = NameChef + Cellule;
String NameInterNew = Cellule;
String NameCISpiner = Cellule;
String NameType = Cellule;
String orderName = text + Cellule;
String NameDate = datesok + Cellule;
String NameHeure = heure + Cellule;
String NameDateNew =+day+"-"+month+"-"+year+ Cellule;
String NameHeureNew = hour+"-"+minute + Saut;
// création du fichier
mFile.createNewFile();
output = new FileOutputStream(mFile+"_"+NameCIOK+"_"+NameType+".csv", true);
// output.write(NameHeure.getBytes());
output.flush();
output.write(Cellule.getBytes());
output.flush();
output.write(Cellule.getBytes());
output.flush();
output.write(Cellule.getBytes());
output.write(Cellule.getBytes());
output.write(orderName.getBytes());
output.write(NameDate.getBytes());
output.write(NameHeure.getBytes());
output.write(Cellule.getBytes());
output.write(Cellule.getBytes());
output.write(Cellule.getBytes());
output.write(NameDateNew.getBytes());
output.write(NameHeureNew.getBytes());
output.flush();
if (output != null)
output.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
// Register DatePickerDialog listener
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
// the callback received when the user "sets" the Date in the DatePickerDialog
public void onDateSet(DatePicker view, int yearSelected,
int monthOfYear, int dayOfMonth) {
year = yearSelected;
month = monthOfYear;
day = dayOfMonth;
// Set the Selected Date in Select date Button
btnSelectDate.setText("Date Séléctioner : "+day+"-"+month+"-"+year);
}
};
// Register TimePickerDialog listener
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener()
{
// the callback received when the user "sets" the TimePickerDialog in the dialog
public void onTimeSet(TimePicker view, int hourOfDay, int min) {
hour = hourOfDay;
minute = min;
// Set the Selected Date in Select date Button
btnSelectTime.setText("Heure Séléctioner :"+hour+"-"+minute);
}
};
// Method automatically gets Called when you call showDialog() method
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// create a new DatePickerDialog with values you want to show
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
// create a new TimePickerDialog with values you want to show
case TIME_DIALOG_ID:
return new TimePickerDialog(this,
mTimeSetListener, mHour, mMinute, false);
}
return null;
}
public void Heure_Date(View v) {
//on creer une nouvelle intent on definit la class de depart ici this et la class d'arrivé ici SecondActivite
Intent intent=new Intent(this,HeureDate.class);
//on lance l'intent, cela a pour effet de stoper l'activité courante et lancer une autre activite ici SecondActivite
startActivity(intent);
}
}
class SpinnerActivity extends Activity implements AdapterView.OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
parent.getItemAtPosition(pos);
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
} |
Partager