I am having some issues with writing and reading with android.
here is my code:
package filereader.testing.com;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class FileReaderTestingActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button submit_btn = (Button) findViewById (R.id.submit_btn);
final EditText textbox = (EditText) findViewById (R.id.first_text);
final TextView newtext = (TextView) findViewById (R.id.read_text);
submit_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
byte[] readinfo = new byte[3];
String FILENAME = "first_file";
FileOutputStream mystream;
try {
mystream = openFileOutput (FILENAME, Context.MODE_PRIVATE);
String variabletowrite = textbox.toString();
mystream.write(variabletowrite.getBytes());
mystream.close();
FileInputStream readstream = openFileInput (FILENAME);
readstream.read(readinfo);
newtext.setText(readinfo.toString());
readstream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
the try and catch was added by eclipse
here is my xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<EditText android:id="@+id/first_text" android:layout_width="match_parent"
android:layout_height="wrap_content">
<requestFocus></requestFocus>
</EditText>
<Button android:layout_height="wrap_content" android:text="Submit"
android:id="@+id/submit_btn" android:layout_width="match_parent"></Button>
<TextView android:textAppearance="?android:attr/textAppearanceLarge" android:layout_height="wrap_content" android:text="TextView" android:id="@+id/read_text" android:layout_weight="0.12" android:layout_width="280dp"></TextView>
</LinearLayout>
and here is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="filereader.testing.com"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="@drawable/icon" android:label="@string/app_name" android:permission="android.permission.WRITE_EXTERNAL_STORAGE">
<activity android:name=".FileReaderTestingActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
when I run the code all of the text boxes appear and so does the submit button, but when I type something in and then hit the submit button the text box changes to things like [B@40532d08 and I have no idea what it means. the number seems to count up every time that I click the submit button and the [B@ part never changes, just the numbers and letters after it.
any help would be greatly appreciated, thank you.
Change this line…
to…
The first line (the way you are doing it) is calling
toString()on theEditTextobject itself (i.e., it’s reference) and not its text contents.EDIT
When setting the text for the
TextViewthis line…is also simply converting your
byte[]reference to a string. You need to do something like the following to create a string from an array of bytes…