在我的应用程序中,我将文本存储在字符串中,一个参数是字节数组形式我已经获取数据但是在字节数组中,索引数据显示在toast中,所有数据都以字符串形式显示,但最后的数据以字节显示数组形式[B @ 436ac990如果我下次单击显示按钮并显示吐司它们会改变字节数组[B @ 436ac991它们会一次又一次地变化如何以字符串形式显示我知道这是内存地址但是如何转换成字符串。如果我错了请解决我的问题
这是我的主要活动课
public class MainActivity extends Activity {
EditText F_name, L_name, Mob_num, Email_Add, Repeat_email, password;
DBHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new DBHelper(this);
F_name = (EditText) findViewById(R.id.F_name);
L_name = (EditText) findViewById(R.id.L_name);
Mob_num = (EditText) findViewById(R.id.mobile_no);
Email_Add = (EditText) findViewById(R.id.E_address);
Repeat_email = (EditText) findViewById(R.id.repet_Email_address);
password = (EditText) findViewById(R.id.password);
// EditText F_name=(EditText)findViewById(R.id.F_name);
Button save = (Button) findViewById(R.id.save);
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String first_name = F_name.getText().toString();
String Last_name = L_name.getText().toString();
String Mobile_numb = Mob_num.getText().toString();
String Emailadd = Email_Add.getText().toString();
String Repeat_email_add = Repeat_email.getText().toString();
byte[] Passwwrd = password.getText().toString().getBytes();
// String pasS=new String(Passwwrd);
db.openToWrite();
long id = db.insert(first_name, Last_name, Mobile_numb, Emailadd, Repeat_email_add, Passwwrd);
db.close();
}
});
Button Show = (Button) findViewById(R.id.show);
Show.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
db.openToRead();
Cursor c = db.fetch_all();
if (c.moveToFirst()) {
do {
DisplayContact(c);
} while (c.moveToNext());
}
db.close();
}
});
}
public void DisplayContact(Cursor c) {
Toast.makeText(
this,
"FirstName: " + c.getString(0) + "\n" + "Lastname: " + c.getString(1) + "\n" + "Mobile_numb: " + c.getString(2) + "\n" + "Emailadd: "
+ c.getString(3) + "\n" + "Repeat_email_add: " + c.getString(4) + "\n" + "password: " + c.getBlob(5) + "\n", Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这是我的dbhelper类
package com.example.dbhelpe;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper {
public static final String MYDATABASE_NAME = "property_detail.db";
public static final String MYDATABASE_TABLE = "registration_table";
public static final String MYDATABASE_TABLE_1 = "deatil_table";
public static final String MYDATABASE_TABLE_2 = "Groups_table";
public static final int MYDATABASE_VERSION = 1;
public static final String ID = "_id";
private static final String FIRST_NAME = "firstname"; // a column named
// "_id" is required
// for cursor
private static final String LAST_NAME = "lastname";
private static final String MOBILE_NO = "mobileno";
private static final String EMAIL_ADDRESS = "emailaddress";
private static final String REPEAT_EMAIL_ADDRESS = "remailaddress";
private static final String PASSWORD = "password";
private static final String OWNER = "owner"; // a column named "_id" is
// required for cursor
private static final String ADDRESS = "address";
private static final String CASH = "cash";
private static final String REPORTED_BY = "reportedby";
private static final String DATE = "date";
private static final String PROPERTY_ID = "propertyid";
private static final String COMGROUP_NAME = "comgroup_name"; // a column
// named
// "_id" is
// required
// for
// cursor
private static final String NODE_NAME = "node_name";
private static final String LICENSE_COUNT = "license_count";
private static final String NODE_MAC_ADDRESS = "node_mac_address";
private SQLiteHelper sqLiteHelper;
private static SQLiteDatabase sqLiteDatabase;
private Context context;
private static final String SCRIPT_CREATE_DATABASE = "create table " + MYDATABASE_TABLE + " (" + ID + " integer primary key autoincrement, " + FIRST_NAME
+ " text not null," + LAST_NAME + " text not null," + MOBILE_NO + " text not null, " + EMAIL_ADDRESS + " text not null, " + REPEAT_EMAIL_ADDRESS
+ " text not null, " + PASSWORD + " text not null);";
private static final String SCRIPT_CREATE_DATABASE1 = "create table " + MYDATABASE_TABLE_1 + " (" + ID + " integer primary key autoincrement, " + OWNER
+ " text not null," + ADDRESS + " text not null," + CASH + " text not null," + REPORTED_BY + " text not null," + DATE + " text not null,"
+ PROPERTY_ID + " text not null);";
private static final String SCRIPT_CREATE_DATABASE2 = "create table " + MYDATABASE_TABLE_2 + " (" + ID + " integer primary key autoincrement, "
+ COMGROUP_NAME + " text not null," + NODE_NAME + " text not null," + LICENSE_COUNT + " text not null," + NODE_MAC_ADDRESS + " text not null);";
// private static final String SCRIPT_CREATE_DATABASE_1 =
// "create table " + MYDATABASE_TABLE_1 + " ("
// + ID + " integer primary key autoincrement, "
// + SMS1 + " text not null);";
//
// private static final String SCRIPT_CREATE_DATABASE_2 =
// "create table " + MYDATABASE_TABLE_2 + " ("
// + ID + " integer primary key autoincrement, "
// + VALUE + " integer not null);";
public DBHelper(Context c) {
context = c;
}
public DBHelper openToRead() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getReadableDatabase();
return this;
}
public DBHelper openToWrite() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
return this;
}
public void close() {
sqLiteHelper.close();
}
public long insert(String owner, String address, String cash, String reportedby, String date, byte[] passwwrd) {
ContentValues contentValues = new ContentValues();
contentValues.put(OWNER, owner);
contentValues.put(ADDRESS, address);
contentValues.put(PROPERTY_ID, passwwrd);
contentValues.put(DATE, date);
contentValues.put(REPORTED_BY, reportedby);
contentValues.put(CASH, cash);
return sqLiteDatabase.insert(MYDATABASE_TABLE_1, null, contentValues);
}
public long insert_register(String firstname, String lastname, String mobileno, String emailaddress, String remailaddress, String password) {
ContentValues contentValues = new ContentValues();
contentValues.put(FIRST_NAME, firstname);
contentValues.put(LAST_NAME, lastname);
contentValues.put(MOBILE_NO, mobileno);
contentValues.put(EMAIL_ADDRESS, emailaddress);
contentValues.put(REPEAT_EMAIL_ADDRESS, remailaddress);
contentValues.put(PASSWORD, password);
return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
}
public Cursor fetch_all() {
String[] columns = new String[] { OWNER, ADDRESS, CASH, REPORTED_BY, DATE, PROPERTY_ID };
Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE_1, columns, null, null, null, null, null);
return cursor;
}
// public long update_communication(String id,String cast,String port,String
// broadcast,String unicast,String multicast){
//
// ContentValues contentValues = new ContentValues();
//
// // contentValues.put(LISTID, k);
//
// contentValues.put(ID, id);
// contentValues.put(PORT, port);
// contentValues.put(CAST_TYPE, cast);
// contentValues.put(BROADCAST_ADDRESS, broadcast);
// contentValues.put(UNICAST_ADDRESS, unicast);
// contentValues.put(MULTICAST_ADDRESS, multicast);
// return sqLiteDatabase.update(MYDATABASE_TABLE, contentValues, null,
// null);
// // return sqLiteDatabase.insert(MYDATABASE_TABLE_1, null, contentValues);
// }
// public long update_audio(String id,String codecbitrate,String
// codecstatus,String echostatus){
//
// ContentValues contentValues = new ContentValues();
//
// // contentValues.put(LISTID, k);
//
// contentValues.put(ID, id);
// contentValues.put(CODEC_BIT_RATE, codecbitrate);
// contentValues.put(CODEC_STATUS, codecstatus);
// contentValues.put(ECHO_STATUS, echostatus);
// return sqLiteDatabase.update(MYDATABASE_TABLE_1, contentValues, null,
// null);
// // return sqLiteDatabase.insert(MYDATABASE_TABLE_1, null, contentValues);
// }
// public long insert2(int value){
//
// ContentValues contentValues = new ContentValues();
//
// contentValues.put(VALUE, value);
//
// return sqLiteDatabase.insert(MYDATABASE_TABLE_2, null, contentValues);
// }
//
// public boolean deleterow(String item){
//
// return sqLiteDatabase.delete(MYDATABASE_TABLE, SMS + "=?", new
// String[]{item}) >0;
// }
public int deleteAll() {
return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
}
// public Cursor queueAll(){
//
// String[] columns = new
// String[]{CAST_TYPE,BROADCAST_ADDRESS,PORT,UNICAST_ADDRESS,MULTICAST_ADDRESS};
//
// Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,null,
// null, null, null, null);
//
// return cursor;
// }
// public Cursor queueAll1(){
//
// String[] columns = new String[]{CODEC_STATUS,ECHO_STATUS,CODEC_BIT_RATE};
//
// Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE_1, columns,null,
// null, null, null, null);
//
// return cursor;
// }
//
// public Cursor queueAll2(){
//
// String[] columns = new String[]{VALUE};
//
// Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE_2, columns,null,
// null, null, null, null);
//
// return cursor;
// }
// public void update_byID(String item){
//
// ContentValues values = new ContentValues();
//
// values.put(CAST_TYPE, item);
// sqLiteDatabase.update(MYDATABASE_TABLE, values, null, null);
// }
// public void codec_update(String item){
//
// ContentValues values = new ContentValues();
//
// values.put(CODEC_STATUS, item);
// sqLiteDatabase.update(MYDATABASE_TABLE_1, values, null, null);
// }
// public void codecbitrate_update(String item){
//
// ContentValues values = new ContentValues();
//
// values.put(CODEC_BIT_RATE, item);
// sqLiteDatabase.update(MYDATABASE_TABLE_1, values, null, null);
// }
//
// public void echo_update(String item){
//
// ContentValues values = new ContentValues();
//
// values.put(ECHO_STATUS, item);
// sqLiteDatabase.update(MYDATABASE_TABLE_1, values, null, null);
// }
// public void port_update(String item){
//
// ContentValues values = new ContentValues();
//
// values.put(PORT, item);
// sqLiteDatabase.update(MYDATABASE_TABLE, values, null, null);
// }
// public void broadcast_update(String item){
//
// ContentValues values = new ContentValues();
//
// values.put(BROADCAST_ADDRESS, item);
// sqLiteDatabase.update(MYDATABASE_TABLE, values, null, null);
// }
// public void unicast_update(String item){
//
// ContentValues values = new ContentValues();
//
// values.put(UNICAST_ADDRESS, item);
// sqLiteDatabase.update(MYDATABASE_TABLE, values, null, null);
// }
// public void multi_update(String item){
//
// ContentValues values = new ContentValues();
//
// values.put(MULTICAST_ADDRESS, item);
// sqLiteDatabase.update(MYDATABASE_TABLE, values, null, null);
// }
// public void cast_update(String item){
//
// ContentValues values = new ContentValues();
//
// values.put(CAST_TYPE, item);
// sqLiteDatabase.update(MYDATABASE_TABLE, values, null, null);
// }
// public Cursor insert_bySearch(String item){
// String[] columns = new String[]{SMS};
// Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,SMS +"="+
// item, null, null, null, null);
// // sqLiteDatabase.update(MYDATABASE_TABLE, values, SMS +"="+ item ,
// null);
// return cursor ;
// }
public class SQLiteHelper extends SQLiteOpenHelper {
private long l;
private DBHelper myDB;
public SQLiteHelper(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SCRIPT_CREATE_DATABASE);
db.execSQL(SCRIPT_CREATE_DATABASE1);
// db.execSQL(SCRIPT_CREATE_DATABASE2);
// db.execSQL("INSERT INTO " + MYDATABASE_TABLE
// +" Values ('0','0','20938','255.255.255.255','192.168.1.154','239.5.5.5');");
// db.execSQL("INSERT INTO " + MYDATABASE_TABLE_1
// +" Values ('0','true','5','false');");
// PreferenceConnector.writeString(context,
// PreferenceConnector.NODE,"2");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
public Cursor rawQueryNum() {
Cursor cursor = sqLiteDatabase.query(true, MYDATABASE_TABLE, null, null, null, null, null, null, null);
return cursor;
}
public Cursor rawQuery(String string, Object object) {
// TODO Auto-generated method stub
return null;
}
}
这是xml
<EditText
android:id="@+id/F_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="FIRST_NAME"
android:text="" />
<EditText
android:id="@+id/L_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="LAST_NAME"
android:text="" />
<EditText
android:id="@+id/mobile_no"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="MOBILE_NO"
android:text="" />
<EditText
android:id="@+id/E_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="EMAIL_ADDRESS"
android:text="" />
<EditText
android:id="@+id/repet_Email_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="REPEAT_EMAIL_ADDRESS"
android:text="" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="PASSWORD"
android:text="" />
<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save" />
<Button
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SHOW" />
答案 0 :(得分:0)
要在字符串和字节数组之间进行转换,必须考虑charset编码:
String original = "some text";
byte[] utf8buffer = sometext.getBytes("UTF8");
String fromUtf8 = new String(utf8buffer, "UTF8");
请参阅Byte Encodings and Strings和String.String (byte[], String)。