jeudi 23 avril 2020

React checkboxes. State is late when toggling the checkboxes

I have a group of 3 checkboxes and the main checkbox for checking those 3 checkboxes.

  • When I select all 3 checkboxes I want for main checkbox to become checked.
  • When I check those 3 checkboxes nothing happens but when I then uncheck one of those trees the main checkbox becomes checked.

Can someone explain to me what actually is happening behind the scenes and help me somehow to solve this mystery of React state? Thanks!

Here is a code snnipet:

state = {
  data: [
    { checked: false, id: 1 },
    { checked: false, id: 2 },
    { checked: false, id: 3 }
  ],
  main: false,
}

onCheckboxChange = id => {
  const data = [...this.state.data];

  data.forEach(item => {
    if (item.id === id) {
      item.checked = !item.checked;
    }
  })

  const everyCheckBoxIsTrue = checkbox.every(item => item === true);

  this.setState({ data: data, main: everyCheckBoxIsTrue });
}

onMainCheckBoxChange = () => {
  let data = [...this.state.data];

  data.forEach(item => {
    !this.state.main ? item.checked = true : item.checked = false
  })

  this.setState({
    this.state.main: !this.state.main,
    this.state.data: data,
  });
}

render () {
  const checkbox = this.state.data.map(item => (
      <input
        type="checkbox"
        checked={item.checked}
        onChange={() => this.onCheckboxChange(item.id)}
      />
    ))
  }

return (
  <input type="checkbox" name="main" checked={this.state.main} onChange={this.onMainCheckBoxChange} />
  {checkbox}
)




Trying to get radio button and checkbox to work together in an if statement

I don't even know if this code is right, I'm still new to java and have no clue if this is even right. It's meant to be 2 radio buttons and a checkbox when male checkbox is selected and checkbox C1 is selected then it opens a new class but nothing happens

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class WordDocumentTest extends JFrame {


    JCheckBox C1 = new JCheckBox("Safety Switch"); 
    JRadioButton male = new JRadioButton("male");
    JRadioButton female = new JRadioButton("Female");
    private JPanel p1=new JPanel();



    public WordDocumentTest(String s){
        super(s);
        Container content=getContentPane();
        content.setLayout(new FlowLayout());
        Font f=new Font("TimesRoman", Font.BOLD,20);
        p1.setLayout(new GridLayout(1,2));

    p1.add(C1);
    p1.add(male);
    p1.add(female);


    content.add(p1);

    this.add(male);
    this.add(female);



    JRadioButton button1 = male;
    JRadioButton button2 = female;
    ButtonGroup group = new ButtonGroup();
    group.add(button1);
    group.add(button2);



    setSize(250,400);    setVisible(true);
    }

public void actionPerformed(ActionEvent e){


        Object target=e.getSource();

        if (C1.isSelected() && male.isSelected()){
            new MyFrame("");
            }

    }
}

Any tips or anything would help a bunch thanks




Trying to save the state of checkbox and the data within that checkbox even when app is closed

I have created a checkbox that can only select one checkbox at a time. I am then storing the data of the checkbox in a string which i then use in a different method. the checkbox is used in a recyclerview and it is displayed using a firebase recycler adapter. my app requires the checkbox to remain checked at all times even when app is closed as I am launching the app with a foreground service and it uses the data that is checked in the checkbox for an intent in a different class. What i want to know is how to get the checkbox and the data that is stored using this checkbox to save even if my app is closed . I have done some digging and seen that you can use a shared preference but I cant seem to work out how to implement it in my application. here is the code I am currently using to implement the checkbox: firebase recycler adapter

 adapter1 = new FirebaseRecyclerAdapter<contact, contactAdapter>(options1) {
            @Override
            protected void onBindViewHolder(@NonNull final contactAdapter holder, final int position, @NonNull contact model) {
                holder.contactName.setText(model.getContactName());
                holder.contactPhone.setText(model.getContactPhone());
                if(lastCheckedPos == position){
                    holder.chk.setChecked(true);
                }
                else{
                    holder.chk.setChecked(false);
                }
                holder.chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (position == lastCheckedPos) {
                            holder.chk.setChecked(false);
                            lastCheckedPos = -1;
                        }else {
                            lastCheckedPos = position;
                            for (int i = 0; i < adapter1.getItemCount(); i++) {
                                if (adapter1.getItem(i).isSelected(true)) {
                                    number = ((TextView) holder.itemView.findViewById(R.id.contactPhone)).getText().toString();
                                    Toast.makeText(getActivity(), "Data Inserted....." + number, Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(getActivity(), "Error", Toast.LENGTH_LONG).show();
                                }
                            }
                            contactsView.post(new Runnable()
                            {
                                @Override
                                public void run() {
                                    adapter1.notifyDataSetChanged();
                                }
                            });

                        }
                    }
                });
            }

            @NonNull
            @Override
            public contactAdapter onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                return new contactAdapter(LayoutInflater.from(getActivity()).inflate(R.layout.item_contacts, parent, false));

            }
        };

Adapter class

public class contactAdapter extends RecyclerView.ViewHolder{

    public TextView contactName, contactPhone;
    CheckBox chk;
    AdapterView.OnItemClickListener itemClickListener;




    public contactAdapter(@NonNull View itemView) {
        super(itemView);
        contactName = itemView.findViewById(R.id.contactName);
        contactPhone = itemView.findViewById(R.id.contactPhone);
        chk = itemView.findViewById(R.id.myCheckBox);
        this.setIsRecyclable(false);

    }
}

Any suggestions?




trying to select one checkbox at a time in a recyclerview but app crashes

I am trying to select one checkbox at a time in a recyclerview as i need data from an item so that i can use it in a different method. Currently I can select an item and the data is then stored in a variable which is then used in a different class but if i try and select a different item so that it becomes the new and only selected item in the recyclerview, it crashes. Variable used:

 private int lastCheckedPos = -1;

I am using a firebase recycler adapter

adapter1 = new FirebaseRecyclerAdapter<contact, contactAdapter>(options1) {
            @Override
            protected void onBindViewHolder(@NonNull final contactAdapter holder, final int position, @NonNull contact model) {
                holder.contactName.setText(model.getContactName());
                holder.contactPhone.setText(model.getContactPhone());
                holder.chk.setChecked(position == lastCheckedPos);
                holder.chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (position == lastCheckedPos) {
                            holder.chk.setChecked(false);
                            lastCheckedPos = -1;
                        }else {
                            lastCheckedPos = position;
                            for (int i = 0; i < adapter1.getItemCount(); i++) {
                                if (adapter1.getItem(i).isSelected(true)) {
                                    number = ((TextView) contactsView.findViewHolderForAdapterPosition(position).itemView.findViewById(R.id.contactPhone)).getText().toString();
                                    Toast.makeText(getActivity(), "Data Inserted....." + number, Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(getActivity(), "Error", Toast.LENGTH_LONG).show();
                                }
                            }
                            notifyDataSetChanged();
                        }
                    }
                });
            }

            @NonNull
            @Override
            public contactAdapter onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                return new contactAdapter(LayoutInflater.from(getActivity()).inflate(R.layout.item_contacts, parent, false));

            }
        };

This is my full stacktrace

2020-04-23 14:48:45.528 10913-10913/com.example.carcrashdetection E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.carcrashdetection, PID: 10913
    java.lang.NullPointerException: Attempt to read from field 'android.view.View androidx.recyclerview.widget.RecyclerView$ViewHolder.itemView' on a null object reference
        at com.example.carcrashdetection.Home$1$1.onCheckedChanged(Home.java:112)
        at android.widget.CompoundButton.setChecked(CompoundButton.java:180)
        at com.example.carcrashdetection.Home$1.onBindViewHolder(Home.java:101)
        at com.example.carcrashdetection.Home$1.onBindViewHolder(Home.java:96)
        at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:149)
        at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
        at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
        at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
        at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
        at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
        at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
        at android.view.View.layout(View.java:22085)
        at android.view.ViewGroup.layout(ViewGroup.java:6290)
        at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
        at android.view.View.layout(View.java:22085)
        at android.view.ViewGroup.layout(ViewGroup.java:6290)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:22085)
        at android.view.ViewGroup.layout(ViewGroup.java:6290)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
        at android.view.View.layout(View.java:22085)
        at android.view.ViewGroup.layout(ViewGroup.java:6290)
        at androidx.drawerlayout.widget.DrawerLayout.onLayout(DrawerLayout.java:1231)
        at android.view.View.layout(View.java:22085)
        at android.view.ViewGroup.layout(ViewGroup.java:6290)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:22085)
        at android.view.ViewGroup.layout(ViewGroup.java:6290)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
        at android.view.View.layout(View.java:22085)
        at android.view.ViewGroup.layout(ViewGroup.java:6290)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:22085)
        at android.view.ViewGroup.layout(ViewGroup.java:6290)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
        at android.view.View.layout(View.java:22085)
        at android.view.ViewGroup.layout(ViewGroup.java:6290)
2020-04-23 14:48:45.528 10913-10913/com.example.carcrashdetection E/AndroidRuntime:     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:780)
        at android.view.View.layout(View.java:22085)
        at android.view.ViewGroup.layout(ViewGroup.java:6290)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3332)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2809)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1929)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7987)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1154)
        at android.view.Choreographer.doCallbacks(Choreographer.java:977)
        at android.view.Choreographer.doFrame(Choreographer.java:893)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1139)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7682)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)

Contacts Adapter

public class contactAdapter extends RecyclerView.ViewHolder{

    public TextView contactName, contactPhone;
    CheckBox chk;
    AdapterView.OnItemClickListener itemClickListener;



    public contactAdapter(@NonNull View itemView) {
        super(itemView);
        contactName = itemView.findViewById(R.id.contactName);
        contactPhone = itemView.findViewById(R.id.contactPhone);
        chk = itemView.findViewById(R.id.myCheckBox);
    }
}

item_contacts.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:id="@+id/outer"
        android:layout_width="match_parent"
        android:layout_height="101dp"
        android:layout_marginBottom="24dp"
        android:paddingBottom="24dp"
        android:paddingTop="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="#ffffff">

        <ImageView
            android:layout_width="80dp"
            android:layout_height="65dp"
            android:layout_marginTop="15dp"
            android:src="@drawable/icon" />

        <LinearLayout
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:orientation="vertical"
            android:id="@+id/inner">

            <TextView
                android:id="@+id/contactName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Name"
                android:textSize="16sp" />

            <TextView
                android:id="@+id/contactPhone"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Phone"
                android:textSize="16sp" />


        </LinearLayout>

        <LinearLayout
            android:id="@+id/hello"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal"
            android:padding="10dp">

            <Button
                android:id="@+id/edit"
                android:layout_width="70dp"
                android:layout_height="wrap_content"


                android:text="Edit"
                android:textSize="14sp" />

            <Button
                android:id="@+id/delete"
                android:layout_width="70dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:text="Delete"
                android:textSize="14sp" />
        </LinearLayout>


        <CheckBox
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="25dp"
            android:height="60dp"
            android:id="@+id/myCheckBox"
            />

    </LinearLayout>

</LinearLayout>

I can see from the stack trace that its giving me a NullPointerException but dont really know why. Any suggestions would be much appreciated.




JS or Jquery: how to add an "option" to a checkbox

The checkbox created in MS Sharepoint form has this html structure;

<div class="fd_field " fd_name="purchase_order" style="">
  <div class="fd_title" style="width: 150px;">purchase_order</div>
  <div class="ms-formbody fd_control" fd_type="MultiChoice">
    <span dir="none">
      <table cellspacing="1" cellpadding="0">                             
        <tbody>
          <tr>
            <td><span class="ms-RadioText" title="Carica dati">
              <input id="ctl00_ctl40_g_2bbe8d3f_1b61_4103_9ca6_2e943eb28860_purchase_orderField_ctl00_ctl00" type="checkbox" name="ctl00$ctl40$g_2bbe8d3f_1b61_4103_9ca6_2e943eb28860$purchase_orderField$ctl00$ctl00">
              <label for="ctl00_ctl40_g_2bbe8d3f_1b61_4103_9ca6_2e943eb28860_purchase_orderField_ctl00_ctl00">Carica dati</label></span>
            </td>
        </tr>
      </tbody>
    </table>
    </span>
  </div>
</div>

with ajax i got other data that i want to add as option in that checkbox. The loop to retrieve data is not a problem but, how can i append those data in a new row of the checkbox? In the past i was able to edit dropdown field but it not works with checkbox.

I was thinking to the a solution like this:

var f = '<tr><td><input type="checkbox" name="xxx" id="ss"><label for="ss">Other</label></td></tr>';
$( ".ms-RadioText" ).parent().append(f);

Thanks




CheckBox issue in DocuSign Tab

When I add checkbox to Tab I see double check box on my pdf.

checkbox= new CheckBox(){TabLable="Large",Selected="true",Locked="true",DocumentId="1",PageNumber="1",XPosition="328",YPosition="362"}; 

enter image description here




mercredi 22 avril 2020

How do I send the check box to the end?

I want to make a check box by making a line called submit at the end of the table. How should I change it?

I'd like to send the check box at the front to the back.

this sample code

https://www.gyrocode.com/articles/jquery-datatables-how-to-add-a-checkbox-column/