Input 2 EditText into Array, retrieve Array in another class for a Dialog Box Checkbox List View submit and save the checkboxes to display selected Checkboxes into a JSON ListView.
Please help! I really have no idea how to do this and I've been searching for over 8 hours for an answer, step by step searching too but still to my dismay since most of the codes don't seem to be working. Could you give me a link that could show how I could do this? Or possibly a sample code? It would be heaven if you could.
EDIT: Here is my code. My requirement was to store the values of my subject and their units in the internal, external, and shared preferences. The trouble I'm having is storing 2 EditText in an array and the code to retrieve them for a dialog pop up checkbox of the 2 EditText values given.
I'm rather new to Android, sorry.
package edu.usls.yared_projectendterm;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class AddSubjects extends Activity {
//Internal Storage - Initialization.
EditText editSubjects;
EditText editUnits;
//Shared Preferences - Initialization.
public static final String PREFS_NAME = "MyPrefsFile";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_subjects);
//Retrieving Text from EditText box.
editSubjects = (EditText) findViewById(R.id.Subjects);
editUnits = (EditText) findViewById(R.id.Units);
}
public void Submit(View v) {
//Retrieving from EditText and Initialization.
String subjname = editSubjects.getText().toString();
String subjtitle = subjname + ".txt";
String subjunits = editUnits.getText().toString();
String subjuntit = " - " + subjunits;
if(subjname.isEmpty() || subjunits.isEmpty()){
Toast.makeText(getApplicationContext(), "Please enter a filename or some data.",
Toast.LENGTH_LONG).show();
}
else{
try{
//Internal Storage
FileOutputStream fOut = openFileOutput(subjtitle,Context.MODE_PRIVATE);
fOut.write(subjname.getBytes());
fOut.write(subjuntit.getBytes());
fOut.close();
//Shared Preferences=
EditText editTextFileName = (EditText) findViewById(R.id.Subjects);
EditText editTextUnits = (EditText) findViewById(R.id.Units);
SharedPreferences settings = getSharedPreferences(PREFS_NAME,Context.MODE_PRIVATE);
SharedPreferences.Editor outputs = settings.edit();
outputs.putString("Subject", editTextFileName.getText().toString());
outputs.putString("Units", editTextUnits.getText().toString());
outputs.commit();
//External Storage
File myFile= new File(Environment.getExternalStorageDirectory().getPath(), subjtitle);
myFile.createNewFile();
FileOutputStream eOut = new FileOutputStream(myFile);
eOut.write(subjname.getBytes());
eOut.write(subjuntit.getBytes());
OutputStreamWriter Outputer = new OutputStreamWriter(eOut);
Outputer.close();
fOut.close();
editSubjects.setText("");
Toast.makeText(getApplicationContext(), "Data has successfully been saved.",
Toast.LENGTH_LONG).show();
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
} catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
}
}
public void BackButton(View v) {
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
}
}
Aucun commentaire:
Enregistrer un commentaire