-1
I’m developing a mobile app on AS that has a list of cardviews. In these cards I have a switch button that clicking on will open an Alert dialog to confirm the status change. However I’m having trouble passing the information, I believe, switch button, so much so that returns me the error below:
The layout of my cardview where the switch button is:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:id="@+id/cv">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="8dp">
        <ImageView
            android:src="@drawable/board"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:id="@+id/tv_qtd"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="16dp"/>
        <TextView
            android:text=""
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_nome"
            android:layout_toRightOf="@+id/tv_qtd"
            android:layout_alignParentTop="true"
            android:textSize="20dp"/>
        <TextView
            android:text=""
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_endereco"
            android:layout_toRightOf="@+id/tv_qtd"
            android:layout_below="@+id/tv_nome"
            />
        <TextView
            android:text=""
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_bairro"
            android:layout_marginTop="20dp"
            android:layout_toRightOf="@+id/tv_qtd"
            android:layout_below="@+id/tv_nome"
            />
        <Switch
            android:id="@+id/simpleSwitch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="false"
            android:layout_marginTop="60dp"
            android:layout_alignParentRight="true"
            android:text="Coletada"
            android:textOff="Não"
            android:textOn="Sim"/>
    </RelativeLayout>
</android.support.v7.widget.CardView>
This is my class Palhetascoled responsible by cardview:
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    View v = View.inflate(context,R.layout.card_view_palheta, null);
    TextView tv_nome = (TextView) v.findViewById(R.id.tv_nome);
    tv_nome.setText(palhetas.get(i).getCodigo());
    TextView tv_endereco = (TextView) v.findViewById(R.id.tv_endereco);
    tv_endereco.setText("Endereço: " + palhetas.get(i).getEndereco().getRua());
    TextView tv_bairro = (TextView) v.findViewById(R.id.tv_bairro);
    tv_bairro.setText("Bairro: " + palhetas.get(i).getEndereco().getBairro());
    v.setTag(palhetas.get(i).getId());
    Switch simpleSwitch = (Switch) v.findViewById(R.id.simpleSwitch);
    simpleSwitch.setTextOn("Sim"); // displayed text of the Switch whenever it is in checked or on state
    simpleSwitch.setTextOff("Não");
    simpleSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
            ColetarActivity coletarActivity = new ColetarActivity();
            coletarActivity.cekstatus (isChecked);
        }
    });
    return v;
}
In my collection class is that I check if the swicth button has been checked and depending on this check I make a command.
 private AlertDialog alertDialog() {
    // Use the Builder class for convenient dialog construction
    final AlertDialog.Builder builder = new AlertDialog.Builder(getApplication());
    builder.setMessage("Confirmar a coleta da palheta")
            .setPositiveButton("Sim", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    confirmarPedidos();
                }
            })
            .setNegativeButton("Não", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    finish();
                }
            });
    // Create the AlertDialog object and return it
    return builder.create();
}
The problem is in changing the classes, where I am not able to pass the information on the switch, someone can help me?

When you put your code @Leonardo it brings some errors that I posted in the question.
– Carlos Diego
Just a complement to Leonardo’s answer. When you use
thiswithin a new class, you are telling the system to take that - new - class. That is, thenew Intent(this, ColetarActivity.class)is taking the classOnCheckedChangeListenerand moving on to theintent. You can use thecontext(as mentioned above in the reply), orview.getContext();in place ofthis.– Valdeir Psr
Only another complement, the correct one is to use Contextcompat. In some cases taking Context from the view, or passing an Activity, can generate a Memory Leak from the context, and this in Contextcompat has been fixed.
– Grupo CDS Informática
@Leonardo Lima now I’m with this other error, updated in question.
– Carlos Diego
@Carlosdiego my mistake, updated the answer
– Leonardo Lima
@Leonardolima I had already tested with Intent, and it returns me the error: Calling startActivity() from Outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
– Carlos Diego
@Carlosdiego as the error itself indicates, you must include the indicated flag. I updated the response.
– Leonardo Lima
@Leonardolima I add this tag, only when changing the switch button side (on/off) it changes to a white Activity.
– Carlos Diego
Is there any information on
logcat?– Valdeir Psr
@Carlosdiego good, there is already another problem outside the scope of this question. You should implement properly the
ColetarActivityand youronCreateto display something in it.– Leonardo Lima
@Valdeirpsr as I am passing via putExtra the id of the guy logged in, he gives an error by clicking on the switch he opens a new Intent q on this, there is the id. So strange, because it opens a new input when you click on the switch. Even if you put a log, the blank screen opens
– Carlos Diego
It really sends the ON/OFF switch to class, as checked in LOG, but has this other pro of changing Activity blank
– Carlos Diego