android - 如何将 EditText 中的文本保存为 Android 中的表格文本文件?

标签 android

我正在尝试将多个文本框中的文本保存到一个表格中的单个文件中

例如:

学生:....
类:...
标记:...

这应该保存为:

Student                Class          Marks

....                   ....           ....

....                   ....           ....

Everytime it saves, a new row should be created for a new student....

Is this possible as a text file ? If not, how else will I be able to do this ?

Also, the activity should display this table.... if possible

The code I got is :

public class Save extends Activity implements SwipeInterface, OnFocusChangeListener {
Double marks = 0.0;
EditText et1 = (EditText) findViewById(R.id.roll_no);
EditText et2 = (EditText) findViewById(R.id.name);
EditText et3 = (EditText) findViewById(R.id.grade);
EditText et4 = (EditText) findViewById(R.id.subject);
EditText et5 = (EditText) findViewById(R.id.marks);
EditText et6 = (EditText) findViewById(R.id.filename);
....
ActivitySwipeDetector swipe = new ActivitySwipeDetector(this, this);
    RelativeLayout swipe_layout = (RelativeLayout) findViewById(R.id.Save_port);
    swipe_layout.setOnTouchListener(swipe);

    Intent receiveIntent = this.getIntent();
    Double e3 = receiveIntent.getDoubleExtra("DoubleValue_e3", 0.00);
    marks = e3;
    EditText ebar = (EditText) findViewById(R.id.marks);
    ebar.setText(marks.toString());

    et3.setOnFocusChangeListener((OnFocusChangeListener)this);
    try{
        File dir = new File("/mnt/sdcard/Teacher's Aid/");

        if (!dir.exists()) {
            dir.mkdir();
          } 

    }catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
}
 ....
public void butt_save(View view)
{
    try {
        String row = et1.getText().toString() + "," + et2.getText().toString() + "," + et3.getText().toString() + "," + et4.getText().toString() + "," + et5.getText().toString() + "\n";
        File myFile = new File("/mnt/sdcard/Teacher's Aid/" + et6.getText().toString() + ".csv");
   if (!myFile.exists()) {
           myFile.createNewFile();
           FileOutputStream fOut = new FileOutputStream(myFile.getAbsoluteFile(),true);
            OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);


                   //Write the header : in your case it is : Student class Marsk (only one time)
            myOutWriter
            .write("Roll no,Name,Grade,Subject,Marks"+"\n");

            myOutWriter.close();
   }
   FileOutputStream fOut = new FileOutputStream(myFile.getAbsoluteFile(),true);
   OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);

               //write to CSV
   myOutWriter.write(row);
   myOutWriter.close();
   Toast.makeText(getApplication(), "Sucessful",Toast.LENGTH_LONG).show();

} catch (Exception e) {
 e.printStackTrace();
 }
}

@Override
public void onFocusChange(View v, boolean hasFocus) {
    if(!hasFocus)
    {
        et6.setText("Class " + et3.getText().toString());

}

}}

但是,这甚至没有打开...当我从主要 Activity 转到此时,您的应用程序崩溃了... LogCat 显示 ...

02-20 05:53:38.581: E/AndroidRuntime(1664): FATAL EXCEPTION: main
02-20 05:53:38.581: E/AndroidRuntime(1664): Process: com.Candy.teacher, PID: 1664
02-20 05:53:38.581: E/AndroidRuntime(1664): java.lang.RuntimeException: Unable to instantiate  activity ComponentInfo{com.Candy.teacher/com.Candy.teacher.Save}: java.lang.NullPointerException
02-20 05:53:38.581: E/AndroidRuntime(1664):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2102)
02-20 05:53:38.581: E/AndroidRuntime(1664):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
02-20 05:53:38.581: E/AndroidRuntime(1664):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
02-20 05:53:38.581: E/AndroidRuntime(1664):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
02-20 05:53:38.581: E/AndroidRuntime(1664):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-20 05:53:38.581: E/AndroidRuntime(1664):     at android.os.Looper.loop(Looper.java:137)
02-20 05:53:38.581: E/AndroidRuntime(1664):     at android.app.ActivityThread.main(ActivityThread.java:4998)
02-20 05:53:38.581: E/AndroidRuntime(1664):     at java.lang.reflect.Method.invokeNative(Native Method)
02-20 05:53:38.581: E/AndroidRuntime(1664):     at java.lang.reflect.Method.invoke(Method.java:515)
02-20 05:53:38.581: E/AndroidRuntime(1664):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
02-20 05:53:38.581: E/AndroidRuntime(1664):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
02-20 05:53:38.581: E/AndroidRuntime(1664):     at dalvik.system.NativeStart.main(Native Method)
02-20 05:53:38.581: E/AndroidRuntime(1664): Caused by: java.lang.NullPointerException
02-20 05:53:38.581: E/AndroidRuntime(1664):     at android.app.Activity.findViewById(Activity.java:1883)
02-20 05:53:38.581: E/AndroidRuntime(1664):     at com.Candy.teacher.Save.<init>(Save.java:15)
02-20 05:53:38.581: E/AndroidRuntime(1664):     at java.lang.Class.newInstanceImpl(Native Method)
02-20 05:53:38.581: E/AndroidRuntime(1664):     at java.lang.Class.newInstance(Class.java:1208)
02-20 05:53:38.581: E/AndroidRuntime(1664):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
02-20 05:53:38.581: E/AndroidRuntime(1664):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2093)
02-20 05:53:38.581: E/AndroidRuntime(1664):     ... 11 more
02-20 05:53:50.241: I/Process(1664): Sending signal. PID: 1664 SIG: 9

最佳答案

首先包含权限:

android.permission.WRITE_EXTERNAL_STORAGE

您将不得不使用 FileOutputStream :

File externalStorageDir = Environment.getExternalStorageDirectory();
File myFile = new File(externalStorageDir , "mysdfile.txt");

if(myFile.exists())
{
   try
   {
       FileOutputStream fOut = new FileOutputStream(myFile);
       OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
       myOutWriter.append(string);
       myOutWriter.close();
       fOut.close();
   } catch(Exception e)
      {

      }
}
 else
  {
    myFile.createNewFile();
   }

在将字符串写入文件之前附加字符串以存储详细信息:

StringBuffer string = new StringBuffer();

然后你可以简单地使用:

string.append(editText1.getText());
string.append("  ;  ");
string.append(editText2.getText());
string.append("  ;  ");
string.append(editText3.getText());

关于android - 如何将 EditText 中的文本保存为 Android 中的表格文本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21828904/

相关文章:

java - android studio 按钮不起作用

android - 将应用程序中的自签名 SSL 证书列入白名单?

android - java.lang.RuntimeException : Unable to start activity ComponentInfo 错误

android - 在 NAT 后面工作 - 一种设备通信方案

android - 如何删除 TextView 上边距?

android - 不要在Android Studio中使用GitHub上的库

android - 找不到符号 Android React Native

android - 如何在 Android 中的 Button Click 事件上删除项目后刷新 Listview?

java - android支持视频裁剪吗?

android - 如何在android中检索用户输入?