2

I am fairly new to android programming and I have faced a small problem. I have an arraylist consisting of names of person selected from a multi-select listview, the problem is that Whenever I insert those arraylist values into the database, It inserts it as one string: Database row. How do i iterate thru an arraylist and at the same time insert its values into the database?

here is my code:

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);
   }

Thank you for any future replies.

2
  • What do you want to achieve with the names += String.valueOf(data1.get(x)) ? The plus sign on this line will concatenate one name each iteration and on the last iteration it will add a new line with all names concatenated. Commented Nov 30, 2015 at 9:33
  • Thank You Mr. @Trunst, for pointing that out. Commented Nov 30, 2015 at 9:42

2 Answers 2

2

Can you try with that?

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();
}
Sign up to request clarification or add additional context in comments.

3 Comments

It works sir, thank you. But whenever I insert it there are always braces involved [] at the start and end of the insertion process.
Check my update answer that will solve your problem.
@ShiladittyaChakraborty do you know how to implement a JsonArrayRequest using the google's volley lib? please check this question I hope you can help stackoverflow.com/questions/33995091/…
0

look at this code...

 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());

1 Comment

Thank you for the feedback sir, but I am new to android programming and can not comprehend much of your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.