title: Passing checked items in a list view to an arraylist/array
body: <p>Hey I'm new to android app development and in this situation I'm creating a mobile application which stores data in SQLite database and show it in a list view and the items in the list can be checked by check boxes.</p>
  
  <p>In here I want to get the position of the checked items so I can pass them to the database, and when the application opens again previously checked items will be already checked. I want to save the position when the button is clicked.</p>
  
  <p>I've used a SparseBooleanArray to store the checked items but I've no idea to pass the values of SparseBooleanArray to DB and I'm stuck there.
  Help me with this and thanks in advance.</p>
  
  <p>*</p>
  
  <pre><code>public class Subscribe extends AppCompatActivity {
  
  
      ListView lanList;
      DatabaseHelper translateDB;
      TextView textView;
      Button button;
      ArrayList&lt;String&gt; list = new ArrayList&lt;&gt;();
      String clicked;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_subscribe);
          translateDB = new DatabaseHelper(this);
          textView = findViewById(R.id.textView);
          lanList = findViewById(R.id.listView);
          button = findViewById(R.id.subscribeBtn);
  
  
  
          showLang();
  
      }
  
  
  
      public void showLang(){
          final Cursor cursor = translateDB.retriveLang();
          System.out.println("1");
          if (cursor.getCount() == 0){
              Toast.makeText(Subscribe.this, "No data", Toast.LENGTH_LONG).show();
              return;
          }
  
          System.out.println("2");
          while (cursor.moveToNext()) {
              list.add(cursor.getString(1));
          }
  
  
          ArrayAdapter&lt;String&gt; arrayAdapter = new ArrayAdapter&lt;&gt;(this,android.R.layout.simple_list_item_multiple_choice,list);
  
          lanList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
          lanList.setAdapter(arrayAdapter);
  
  
          lanList.setItemChecked(1,true);     &lt;--- used this to check the functioning 
  
          lanList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView&lt;?&gt; adapterView, View view, int i, long l) {
                  clicked = adapterView.getItemAtPosition(i).toString();
                  Toast.makeText(Subscribe.this,"You selected '"+ clicked+"' " , Toast.LENGTH_LONG).show();
              }
          });
  
  
          button.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  SparseBooleanArray checkedItems =lanList.getCheckedItemPositions();
                  System.out.println("1 - " +checkedItems);
  
  
  
  
                  if (checkedItems != null) {
                      for (int i=0; i&lt;checkedItems.size(); i++) {
                          if (checkedItems.valueAt(i)) {
                              String item = lanList.getAdapter().getItem(
                                      checkedItems.keyAt(i)).toString();
                              System.out.println("2 - " +item);
                              translateDB.subscribedLang(item);
                          }   
                      }       
                  }
  
  
  
              }
          });
  
      }
  </code></pre>
  
  <p>*</p>
  
  <p>SO what I'm planning to do is pass the checked positions to db and get the positions and lanList.setItemChecked(x,true); and pass position values to this code.</p>
  
  <p>Help me with this.</p>