Search code examples
javasqlandroid-studiolistviewandroid-listview

Unable to toggle switch to on mode in List View in android studio


I am Using ListView to fetch the names from the database and while the user clicks on the switch it has to move to next activity using Intent which I have given using onClickListener, but the switch doesn't change to on mode when I click it. I am able to move to next activity and see the textviews which I am sending, is it possible to also toggle the switch using position of the switch button?

Java code:

public ListAdapter GetInspList() {
        ArrayList<Integer> pos = new ArrayList<>();

        ListView listView = (ListView) findViewById(R.id.inspection_listview_1);
        List<Map<String, String>> MyDataList = getinspectionlist();

        String[] f = {"Inspection_Name", "Inspection_Id", "Button"};
       int[] i = {R.id.inspec_list_name, R.id.inspec_list_id, R.id.nok_1};
        SimpleAdapter ad;
        final int[] popo = {0};
        ad = new SimpleAdapter(this, MyDataList, R.layout.inspection_listview, f, i) {

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View v = super.getView(position, convertView, parent);
                nok_1 = v.findViewById(R.id.nok_1);
               // clear.setText("Clear");
                nok_1.setOnClickListener(arg0 -> {
                    // TODO Auto-generated method stub
                    ListAdapter add = listView.getAdapter();
                    listView.setAdapter(add);
                    //Log.e("here", MyDataList.get(position).get("Activity_Name"));
                    
                    String d = (String) MyDataList.get(position).get("Inspection_Name");
                           String vinno = vin.getText().toString().trim();
                            //String inspname = d.getText().toString().trim();
                            String inspname = d.trim();
                            Bundle bundle = new Bundle();
                            bundle.putString("vinno", vinno);
                         bundle.putString("inspname", inspname);
                           Intent intent = new Intent(Inspection.this, NOK_inspection.class);
                           intent.putExtras(bundle);
                           startActivity(intent);
                });

                return v;
            }
        };
        listView.setAdapter(ad);
        ad.notifyDataSetChanged();


        return listView.getAdapter();
    }


    public List<Map<String, String>> getinspectionlist() {

        List<Map<String, String>> data = null;
        data = new ArrayList<Map<String, String>>();
        try {

            ConnectionHelper connectionHelper = new ConnectionHelper();
            connect = connectionHelper.connectionClass();
            if (connect != null) {
                String query = "select * from Config_Inspection";
                Statement st = connect.createStatement();
                ResultSet rs = st.executeQuery(query);
                while (rs.next()) {
                    Map<String, String> dtname = new HashMap<String, String>();
                    dtname.put("Inspection_Name", rs.getString(3));
                    dtname.put("Inspection_Id", rs.getString(2));
                    // dtname.put("Activity_Value", rs.getString(7));

                    data.add(dtname);


                }


            }


        } catch (Exception ex) {

        }
        return data;

    }

XML of list view file:

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

    <TextView
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:textSize="17dp"
        android:layout_weight="0.8"
        android:id="@+id/inspec_list_name"
        />
    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:id="@+id/inspec_list_id"/>

    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.01"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="OK"
        android:textSize="17dp"
        android:layout_marginTop="10dp"
        android:layout_weight="0.02"/>
    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.01"/>
    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/nok_1"
        android:layout_marginTop="10dp"
        android:scaleX="1.2"
        android:scaleY="1.2"
        android:thumbTint="@color/ok_button_color"
        android:trackTint="@color/nok_button_color"

        />
    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.01"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="NOK"
        android:textSize="17dp"
        android:layout_weight="0.02"/>



</LinearLayout>

Solution

  • Try removing this lines:

    ListAdapter add = listView.getAdapter();
    listView.setAdapter(add);
    

    because you don't have to set your adapter on every switch click.