0

我制作了一个 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++;
            } 
        }
    };
4

1 回答 1

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+"')");
}
于 2013-05-02T09:04:28.400 回答