I'm developing a mobile application in AS that has a list of cardviews. On these cards I have a switch button that when clicking ON will open an alert dialog to confirm the change of status. But I'm having trouble passing the information, I believe, from the switch button, so much so that it returns the error below:
Thelayoutofmycardviewwheretheswitchbuttonis:
<?xmlversion="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 Replaced reeds 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 I will check if the swicth button has been checked and depending on that 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 the class changes, where I can not pass the switch information, can anyone help me?