我创建了一个EditText,显示多行这样的值......
我希望将该值保存在SQLite数据库中。这是我使用的代码:
export.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final View export_layout = getLayoutInflater().inflate(R.layout.export_layout, null);
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
builder.setView(export_layout);
builder.setTitle("Input new DB");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
editText1 = (EditText) export_layout.findViewById(R.id.editText1);
String table = editText1.getText().toString();
String val = textStatus.getText().toString();
db.execSQL("create table "+table+"(ANY text)");
db.execSQL("insert into "+table+" values('"+val+"')");
}
});
builder.setNegativeButton("back",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
TextStatus是一个EditText,其值显示如上图所示。 editText1是用户输入表名的位置。
问题是当我保存值时,整个值被插入到一个单元格中。我希望每行分离值,然后插入每行的单个单元格中。
有没有办法做到这一点?
编辑: 这就是我在TextStatus中设置文本的方式:
x = new BroadcastReceiver()
{
@Override
public void onReceive(Context c, Intent intent)
{
results = wifi.getScanResults();
size = results.size();
if (size > 0) {
for (int i=0; i<size; i++){
ScanResult scanresult = wifi.getScanResults().get(i);
String ssid = scanresult.SSID;
int rssi = scanresult.level;
String bssid = scanresult.BSSID;
String rssiString = String.valueOf(rssi);
textStatus.append(ssid + "," + bssid + "," + rssiString + "\n");
}
unregisterReceiver(x); //stops the continuous scan
textStatus.append("------------"+j+"\n");
j++;
}
}
};
答案 0 :(得分:0)
试试扫描仪:
Scanner scanner = new Scanner(val);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
//Insert the line here
}
更新:这是你的代码中的样子
editText1 = (EditText) export_layout.findViewById(R.id.editText1);
String table = editText1.getText().toString();
String val = textStatus.getText().toString();
db.execSQL("create table "+table+"(ANY text)");
Scanner scanner = new Scanner(val);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
db.execSQL("insert into "+table+" values('"+line+"')");
}