How to pass the data from one switch button to another acivity?

0

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?

    
asked by anonymous 08.01.2018 / 17:13

2 answers

1

Create a class within the class where you manipulate your listview:

class CustomAdapter extends BaseAdapter{

    private List<Palheta> palhetas;

    public CustomAdapter(List<Palheta> palhetas) {
        this.palhetas = palhetas;
    }

    @Override
    public int getCount() {
        return palhetas.size();
    }

    @Override
    public Object getItem(int i) {
        return palhetas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        view = getLayoutInflater().inflate(R.layout.card_view_palheta, null);
        TextView tv_nome = (TextView) view.findViewById(R.id.tv_nome);
        tv_nome.setText(palhetas.get(i).getCodigo());
        TextView tv_endereco = (TextView) view.findViewById(R.id.tv_endereco);
        tv_endereco.setText("Endereço: " + palhetas.get(i).getEndereco().getRua());
        TextView tv_bairro = (TextView) view.findViewById(R.id.tv_bairro);
        tv_bairro.setText("Bairro: " + palhetas.get(i).getEndereco().getBairro());
        view.setTag(palhetas.get(i).getId());
        Switch simpleSwitch = (Switch) view.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) {
                if (isChecked) {
                    Toast.makeText( getApplicationContext(), "SIM", Toast.LENGTH_LONG ).show();
                } else {
                    Toast.makeText( getApplicationContext(), "NAO", Toast.LENGTH_LONG ).show();
                }
            }
        });
        return view;
    }
}

Then initialize your adapter by passing the list of straws:

private List<Palheta> palhetas;
private ListView lvPalheta;

Type usuariosListType = new TypeToken<ArrayList<Palheta>>(){}.getType();
CustomAdapter customAdapter = new CustomAdapter(palhetas);
lvPalheta.setAdapter(customAdapter);
    
11.01.2018 / 21:20
4

How to start ColetarActivity is wrong. You should not instantiate a Activity class manually. The correct one would look something like:

Intent intent = new Intent(this, ColetarActivity.class);
intent.putExtra("check_status", message);
startActivity(intent);

Or, because it is in an adapter, using a Context :

Intent intent = new Intent(context, ColetarActivity.class);
intent.putExtra("check_status", message);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

And in the other Activity , to get the value passed:

Intent intent = getIntent();
String message = intent.getBooleanExtra("check_status");

This article gives you a full explanation on the subject.

    
10.01.2018 / 19:51