title: How to use Multiple Selection with Room Database
body: <p>How can I link the transaction to the member, like link a transaction id to many member ids? </p>
  
  <p>I have an activity to create new transaction, in that activity I enter the name of the transaction and have a button to insert. After I click on that button, I want to see the list of members. </p>
  
  <p>Then I want to choose the members I want to link to the transaction. By clicking on every member, it should show me a tick or something next to the member I chose or it should set the clicked members in a different colour. </p>
  
  <p>Then the transaction should be inserted with all the linked members I chose.</p>
  
  <p>I did it like this for now, that I just make a new transaction, click on it, start a new activity with the chosen transaction, then in the toolbar I click on a button to start another activity where I should be able to choose the members I want to link with the transaction.</p>
  
  <p>Is it possible to choose the members I want to link to the transaction directly with the new transaction activity? Or is it possible to make a new transaction and when I click on the button to save the new transaction, it inserts the record into the database and directly after insert, the activity where I choose the members to link to the transaction starts? </p>
  
  <p>Also regarding my delete method: I have the method implemented in a onSwipe method. How can I do so that I can click on multiple transactions, they have a tick or a different colour, and delete them all at the same time?</p>
  
  <p>I can click on a button (e.g. in the toolbar) to delete, then choose multiple members, click on a "confirm delete" button, let a confirmation screen appear (but not deleting yet), and after accepting the confirmation screen, delete them?</p>
  
  <p>Class Transaction:</p>
  
  <pre><code>@Entity(tableName = "transaction_table")
  public class Transaction {
  
  @PrimaryKey(autoGenerate = true)
  @ColumnInfo(name = "TransactionID")
  private long id;
  
  @ColumnInfo(name = "Transaction Name")
  private String transactionName;
  
  @ColumnInfo(name = "Transaction Balance")
  private double balance;
  
  public double getBalance() {
      return balance;
  }
  
  public void setBalance(double balance) {
      this.balance = balance;
  }
  
  public long getId() {
      return id;
  }
  
  public void setId(long id) {
      this.id = id;
  }
  
  public String getTransactionName() {
      return transactionName;
  }
  
  public void setTransactionName(String transactionName) {
      this.transactionName = transactionName;
  }
  
  public Transaction(String transactionName, double balance) {
      this.transactionName = transactionName;
      this.balance = balance;
  }
  
  }
  </code></pre>
  
  <p>Class Member:</p>
  
  <pre><code>@Entity(tableName = "member_table")
  public class Member {
  
  
  @PrimaryKey(autoGenerate = true)
  @ColumnInfo(name = "MemberID")
  private long id;
  
  @ColumnInfo(name = "First Name")
  private String firstname;
  
  @ColumnInfo(name = "Surname")
  private String surname;
  
  @ColumnInfo(name = "Balance")
  private double balance;
  
  public String getFirstname() {
      return firstname;
  }
  
  public void setFirstname(String firstname) {
      this.firstname = firstname;
  }
  
  public String getSurname() {
      return surname;
  }
  
  public void setSurname(String surname) {
      this.surname = surname;
  }
  
  public double getBalance() {
      return balance;
  }
  
  public void setBalance(double balance) {
      this.balance = balance;
  }
  
  public long getId() {
      return id;
  }
  
  public void setId(long id) {
      this.id = id;
  }
  
  public Member(String firstname, String surname) {
      this.firstname = firstname;
      this.surname = surname;
      this.balance = 0;
  }
  
  }
  </code></pre>
  
  <p>Class Transaction Main Activity:</p>
  
  <pre><code>public class TransactionMainActivity extends AppCompatActivity implements TransactionListAdapter.TransactionClickListener {
  
  private TransactionViewModel mTransactionViewModel;
  private List&lt;Transaction&gt; mTransaction;
  
  public static final int NEW_TRANSACTION_ACTIVITY_REQUEST_CODE = 1;
  
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.transaction_activity);
      Toolbar toolbar = findViewById(R.id.toolbar_TransactionMainActivity);
      setSupportActionBar(toolbar);
      if (getSupportActionBar() != null) {
          getSupportActionBar().setTitle(R.string.Transaction);
      }
  
      FloatingActionButton fab = findViewById(R.id.fab_TransactionMainActivity);
      fab.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              Intent intent = new Intent(TransactionMainActivity.this, NewTransactionActivity.class);
              startActivityForResult(intent, NEW_TRANSACTION_ACTIVITY_REQUEST_CODE);
          }
      });
  
      RecyclerView recyclerView = findViewById(R.id.RecyclerViewCard_Transaction);
      final TransactionListAdapter adapter = new TransactionListAdapter(this);
      recyclerView.setAdapter(adapter);
      recyclerView.setLayoutManager(new LinearLayoutManager(this));
  
      mTransactionViewModel = ViewModelProviders.of(this).get(TransactionViewModel.class);
  
      mTransactionViewModel.getAllTransactions().observe(this, new Observer&lt;List&lt;Transaction&gt;&gt;() {
          @Override
          public void onChanged(@Nullable List&lt;Transaction&gt; transactions) {
              mTransaction = transactions;
              adapter.setTransaction(transactions);
          }
      });
  
      ItemTouchHelper helper = new ItemTouchHelper(
              new ItemTouchHelper.SimpleCallback(0,
                      ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
                  @Override
                  public boolean onMove(RecyclerView recyclerView,
                                        RecyclerView.ViewHolder viewHolder,
                                        RecyclerView.ViewHolder target) {
                      return false;
                  }
  
                  @Override
                  public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
                      int position = viewHolder.getAdapterPosition();
                      Transaction myTransaction = adapter.getTransactionAtPosition(position);
                      Toast.makeText(TransactionMainActivity.this,
                              getString(R.string.TransactionDeleted) + " " +
                                      myTransaction.getTransactionName(), Toast.LENGTH_LONG).show();
  
                      mTransactionViewModel.delete(myTransaction);
                  }
              });
      helper.attachToRecyclerView(recyclerView);
  
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.menu_main, menu);
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
          case R.id.MainMenuToolbarSearch:
  
          case R.id.MainMenuToolbarAdd:
  
          case R.id.MainMenuToolbarDelete:
  
      }
      return super.onOptionsItemSelected(item);
  }
  
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
  
      if (requestCode == NEW_TRANSACTION_ACTIVITY_REQUEST_CODE &amp;&amp; resultCode == RESULT_OK) {
          Transaction transaction = new Transaction(data.getStringExtra(NewTransactionActivity.EXTRA_REPLY), data.getDoubleExtra(NewTransactionActivity.EXTRA_REPLY2, -1));
          mTransactionViewModel.insert(transaction);
      } else
  
      {
          Toast.makeText(
                  getApplicationContext(),
                  R.string.transaction_not_saved,
                  Toast.LENGTH_LONG).show();
      }
  }
  </code></pre>
  
  <p>Class New Transaction Activity:</p>
  
  <pre><code>public class NewTransactionActivity extends AppCompatActivity {
  
  public static final String EXTRA_REPLY = "com.example.android.transactionlistsql.REPLY";
  public static final String EXTRA_REPLY2 = "com.example.android.transactionlistsql.REPLY2";
  
  private EditText mEditTextTransaction;
  private EditText mEditTextTransaction2;
  
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.newtransaction_activity);
      mEditTextTransaction = findViewById(R.id.NewTransactionName);
      mEditTextTransaction2 = findViewById(R.id.NewTransactionBalance);
      mEditTextTransaction2.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
      mEditTextTransaction2.setText("0");
      final Button button = findViewById(R.id.NewTransactionButtonSave);
  
      button.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              Intent replyIntent = new Intent();
              if (TextUtils.isEmpty(mEditTextTransaction.getText())){
                  Toast.makeText(getApplicationContext(), R.string.TransactionNameMissing, Toast.LENGTH_LONG).show();
                  return;
              }
              else if (TextUtils.isEmpty(mEditTextTransaction2.getText())){
                  mEditTextTransaction2.setText("0");
              }
              else {                 
                  String newtransactionname = mEditTextTransaction.getText().toString();
                  double newtransactionbalance = (Double.parseDouble(mEditTextTransaction2.getText().toString()));
                  replyIntent.putExtra(EXTRA_REPLY, newtransactionname);
                  replyIntent.putExtra(EXTRA_REPLY2, newtransactionbalance);
                  setResult(RESULT_OK, replyIntent);
              }
              finish();
          }
      });
  }
  }
  </code></pre>
  
  <p>My AsyncTask for insert method:</p>
  
  <pre><code>    private static class insertAsyncTask extends AsyncTask&lt;Transaction, Void, Void&gt; {
  
      private TransactionDao mAsyncTaskDao;
  
      insertAsyncTask(TransactionDao dao) {
          mAsyncTaskDao = dao;
      }
  
      @Override
      protected Void doInBackground(final Transaction... params) {
          mAsyncTaskDao.insert(params[0]);
          return null;
      }
  }
  </code></pre>
  
  <p>The line marked as error is this one: mAsyncTaskDao.insert(params[0]);</p>
  
  <p>Error:</p>
  
  <pre><code>07-11 03:10:49.003 16307-16508/com.example.mainbuchhaltung E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #4
  
  Process: com.example.mainbuchhaltung, PID: 16307
  
  java.lang.RuntimeException: An error occurred while executing doInBackground()
  
      at android.os.AsyncTask$3.done(AsyncTask.java:309)
      at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
      at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
      at java.util.concurrent.FutureTask.run(FutureTask.java:242)
      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
      at java.lang.Thread.run(Thread.java:818)
   Caused by: android.database.sqlite.SQLiteConstraintException: FOREIGN KEY constraint failed (code 787)
  #################################################################
  Error Code : 787 (SQLITE_CONSTRAINT_FOEIGNKEY)
  Caused By : Abort due to constraint violation.
      (FOREIGN KEY constraint failed (code 787))
  #################################################################
      at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
      at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:915)
      at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
      at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
      at android.arch.persistence.db.framework.FrameworkSQLiteStatement.executeInsert(FrameworkSQLiteStatement.java:50)
      at android.arch.persistence.room.EntityInsertionAdapter.insert(EntityInsertionAdapter.java:64)
      at com.example.mainbuchhaltung.Transaction.TransactionDao_Impl.insert(TransactionDao_Impl.java:96)
      at com.example.mainbuchhaltung.Transaction.TransactionRepository$insertAsyncTask.doInBackground(TransactionRepository.java:64)
      at com.example.mainbuchhaltung.Transaction.TransactionRepository$insertAsyncTask.doInBackground(TransactionRepository.java:54)
      at android.os.AsyncTask$2.call(AsyncTask.java:295)
      at java.util.concurrent.FutureTask.run(FutureTask.java:237)
          ... 4 more
  </code></pre>