我对Android编程很新,我遇到了一个小问题。我有一个arraylist包含从多选列表视图中选择的人的名字,问题是每当我将这些arraylist值插入数据库时,它会将其作为一个字符串插入: Database row。我如何通过arraylist进行迭代,同时将其值插入数据库?
这是我的代码:
try {
Connection con = connectionClass.CONN();
if (con == null) {
Toast.makeText(getApplicationContext(), "CONNECTION FAIL", Toast.LENGTH_LONG).show();
} else {
String samp = "";
String names = "";
samp = myArrayAdapter.getCheckedItems().toString();
ArrayList<String> data1 = new ArrayList<String>();
data1.add(samp);
for(int x=0; x<data1.size(); x++)
{
names += String.valueOf(data1.get(x));
String query = "INSERT INTO AUTOINC(PersonName)"+"VALUES('"+names+"')";
PreparedStatement preparedStatement = con.prepareStatement(query);
preparedStatement.executeUpdate();
}
Toast.makeText(getApplicationContext(), "INSERT SUCCESS", Toast.LENGTH_LONG).show();
}
} catch (Exception ex) {
Toast.makeText(getApplicationContext(), "INSERT FAILED", Toast.LENGTH_LONG).show();
Log.e("MYAPP", "exception", ex);
}
感谢您今后的任何回复。
答案 0 :(得分:2)
你可以试试吗?
List<String> data1 = new ArrayList<String>(Arrays.asList(samp.replace("[","").replace("]","").split(",")));
for (String name : data1) {
names += name; // This lines concatenate the name.
//If you want to insert single name the you can directly insert the name value into databas.
String query = "INSERT INTO AUTOINC(PersonName)"+"VALUES('"+name+"')";
PreparedStatement preparedStatement = con.prepareStatement(query);
preparedStatement.executeUpdate();
}
答案 1 :(得分:0)
看看这段代码......
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo pi=new PropertyInfo();
pi.setName("UserName");
pi.setValue(txtName.getText().toString());
pi.setType(String.class);
request.addProperty(pi);
pi=new PropertyInfo();
pi.setName("UserAge");
pi.setValue(txtVal.getText().toString());
pi.setType(String.class);
request.addProperty(pi);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(webRequest);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION + METHOD_NAME, envelope);
Log.d("CheckLogin-SOAP ACTION", SOAP_ACTION + METHOD_NAME);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
Log.d("CheckLogin - Response", response.toString());
status= Boolean.valueOf(response.toString());