我想要做的是将多个数据保存在内部存储Android中,我想存储不同的产品详细信息,其中包含产品的图像,产品名称,它的简短描述和价格,正如我所说的,我想保存大约10种产品的所有这些信息。我的问题是如何保存这些数据。
发布于 2014-03-16 20:11:35
首先创建一个项目,然后在项目中添加以下代码,并添加.java和.xml代码
main.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” >
<TextView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Please enter some text”
/>
<EditText
android:id=”@+id/txtText1”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content” />
<Button
android:id=”@+id/btnSave”
android:text=”Save”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content” />
<Button
android:id=”@+id/btnLoad”
android:text=”Load”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content” />
</LinearLayout>
mainActivity.Java
package net.learn2develop.Files;
import android.app.Activity;
import android.view.View;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText textBox;
private static final int READ_BLOCK_SIZE = 100;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textBox = (EditText) findViewById(R.id.txtText1);
Button saveBtn = (Button) findViewById(R.id.btnSave);
Button loadBtn = (Button) findViewById(R.id.btnLoad);
saveBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String str = textBox.getText().toString();
try
{
FileOutputStream fOut =
openFileOutput(“textfile.txt”,
MODE_WORLD_READABLE);
OutputStreamWriter osw = new
OutputStreamWriter(fOut);
//---write the string to the file---
osw.write(str);
osw.flush();
osw.close();
//---display file saved message---
Toast.makeText(getBaseContext(),
“File saved successfully!”,
Toast.LENGTH_SHORT).show();
//---clears the EditText---
textBox.setText(“”);
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
});
loadBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try
{
FileInputStream fIn =
openFileInput(“textfile.txt”);
InputStreamReader isr = new
InputStreamReader(fIn);
char[] inputBuffer = new char[READ_BLOCK_SIZE];
String s = “”;
int charRead;
while ((charRead = isr.read(inputBuffer))>0)
{
//---convert the chars to a String---
String readString =
String.copyValueOf(inputBuffer, 0,
charRead);
s += readString;
inputBuffer = new char[READ_BLOCK_SIZE];
}
//---set the EditText to the text that has been
// read---
textBox.setText(s);
Toast.makeText(getBaseContext(),
“File loaded successfully!”,
Toast.LENGTH_SHORT).show();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
});
}
}
上面是演示的代码。
使用osw.write在上面的代码中添加所需的数据(“必需的文本”)
发布于 2014-03-16 20:15:19
我认为最好的方法是使用SQLite数据库。
这里有一个很好的教程:http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/
你可以随时在互联网上搜索其他人。
https://stackoverflow.com/questions/22436593
复制相似问题