Question
an application on Android Studio that allows user to enter data in SQLite database instead of a file - user input will be saved to
an application on Android Studio that allows user to enter data in SQLite database instead of a file - user input will be saved to a SQLite database and displayed back from the database. using the code below.
activityMain.java
package siyas.abc;
import android.os.Environment; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast;
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader;
public class MainActivity extends ActionBarActivity {
public EditText editText; public TextView textView; public Button save, load;
public String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/abcfile";
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText); textView = (TextView) findViewById(R.id.textView); save = (Button) findViewById(R.id.save); load = (Button) findViewById(R.id.load);
File dir = new File(path); dir.mkdirs();
}
public void buttonSave (View view) { File file = new File (path + "/savedFile.txt"); String [] saveText = String.valueOf(editText.getText()).split(System.getProperty("line.separator"));
editText.setText("");
Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_LONG).show();
Save (file, saveText); }
public void buttonLoad (View view) { File file = new File (path + "/savedFile.txt"); String [] loadText = Load(file);
String finalString = "";
for (int i = 0; i < loadText.length; i++) { finalString += loadText[i] + System.getProperty("line.separator"); }
textView.setText(finalString);
}
public static void Save(File file, String[] data) { FileOutputStream fos = null; try { fos = new FileOutputStream(file); } catch (FileNotFoundException e) {e.printStackTrace();} try { try { for (int i = 0; i public static String[] Load(File file) { FileInputStream fis = null; try { fis = new FileInputStream(file); } catch (FileNotFoundException e) {e.printStackTrace();} InputStreamReader isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr); String test; int anzahl=0; try { while ((test=br.readLine()) != null) { anzahl++; } } catch (IOException e) {e.printStackTrace();} try { fis.getChannel().position(0); } catch (IOException e) {e.printStackTrace();} String[] array = new String[anzahl]; String line; int i = 0; try { while((line=br.readLine())!=null) { array[i] = line; i++; } } catch (IOException e) {e.printStackTrace();} return array; } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } activity_main.xml AndroidManifest.xml
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started