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

Asked

Viewed 162 times

-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:

inserir a descrição da imagem aqui

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?

2 answers

4


The way to start the ColetarActivity is wrong. You should not instantiate a class Activity manually. The correct would be something like:

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

Or, by being on 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 the other Activity, to take the past value:

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

This article gives a full explanation of the subject.

  • When you put your code @Leonardo it brings some errors that I posted in the question.

  • 1

    Just a complement to Leonardo’s answer. When you use this within a new class, you are telling the system to take that - new - class. That is, the new Intent(this, ColetarActivity.class) is taking the class OnCheckedChangeListener and moving on to the intent. You can use the context (as mentioned above in the reply), or view.getContext(); in place of this.

  • 1

    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.

  • @Leonardo Lima now I’m with this other error, updated in question.

  • @Carlosdiego my mistake, updated the answer

  • @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?

  • @Carlosdiego as the error itself indicates, you must include the indicated flag. I updated the response.

  • @Leonardolima I add this tag, only when changing the switch button side (on/off) it changes to a white Activity.

  • Is there any information on logcat?

  • @Carlosdiego good, there is already another problem outside the scope of this question. You should implement properly the ColetarActivity and your onCreate to display something in it.

  • @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

  • It really sends the ON/OFF switch to class, as checked in LOG, but has this other pro of changing Activity blank

Show 7 more comments

0

Create a class within the class where Voce manipulates 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 pick list:

private List<Palheta> palhetas;
private ListView lvPalheta;

Type usuariosListType = new TypeToken<ArrayList<Palheta>>(){}.getType();
CustomAdapter customAdapter = new CustomAdapter(palhetas);
lvPalheta.setAdapter(customAdapter);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.