0

in MainActivity.java I am reading a list from a txt file then on a show list button I am able to display the list, with list items, images and checkbox using custom grid view. Now with another button click I am trying to get checked item's position so as I can use these selected items. the code I have is giving me a null pointer exception error on . view.getCheckedItemPositions();

MainActivity.java

        if(v.getId()==R.id.toast)
        {
            Toast.makeText(this,"positions" + adapter.getCheckedItemPositions(view), Toast.LENGTH_SHORT).show();

        }


    }
    public class CustomAdapter extends BaseAdapter {

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            LinearLayout layout = new LinearLayout(getApplicationContext());
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            view = new View(getApplicationContext());
            TextView text=new TextView(getApplicationContext());
            text.setTextColor(Color.BLACK);
            text.setText(content.get(position));
            CheckBox checkBox = new CheckBox(getApplicationContext());
            checkBox.setChecked(!checkBox.isChecked());
            checkBox.setChecked(true);
            layout.addView(checkBox);
            layout.addView(text);
            return layout;
        }

        public List<Integer> getCheckedItemPositions(GridView view){
            SparseBooleanArray checked = new SparseBooleanArray();
            checked = view.getCheckedItemPositions();
            List<Integer> positions = new ArrayList<>();
            int checksize=checked.size();
            for(int i=0; i<checksize; i++) {
                if (checked.valueAt(i)) {
                    positions.add(checked.keyAt(i));

                }
            }
            return positions;
        }
    }

}

Activity_main.xml


    <GridView
        android:layout_width="wrap_content"

        android:layout_height="wrap_content"
        android:id="@+id/gridView"
        android:layout_below="@+id/button"
        android:layout_alignParentStart="true"

        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Toast"
        android:id="@+id/toast"
        android:layout_alignParentTop="true"
        android:layout_alignEnd="@+id/gridView"
        android:layout_marginEnd="49dp" />


</RelativeLayout>

logcat

--------- beginning of crash 09-18 18:31:49.144 31751-31751/com.example.administrator.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.administrator.myapplication, PID: 31751 java.lang.NullPointerException: Attempt to invoke virtual method 'int android.util.SparseBooleanArray.size()' on a null object reference at com.example.administrator.myapplication.MainActivity$CustomAdapter.getCheckedItemPositions(MainActivity.java:158) at com.example.administrator.myapplication.MainActivity.onClick(MainActivity.java:96) at android.view.View.performClick(View.java:4780) at android.view.View$PerformClick.run(View.java:19866) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5257) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 09-18 18:32:26.135 31751-31758/com.example.administrator.myapplication W/art﹕ Suspending all threads took: 102.748ms 09-18 18:32:27.078 31751-31758/com.example.administrator.myapplication W/art﹕ Suspending all threads took: 78.288ms

sant123
  • 1
  • 4

1 Answers1

0

I got the answer..

I created a link hash map with string as position of the check box and value as the boolean value at that position. Like

LinkedHashMap mylist = new LinkedHashMap<>();

Now in order to get the status of the checked item and monitor it in runntime we have to set a Listener on checkbox either using

checkBox.setOnCheckedChangeListener();

or //

checkbox.setOnClickListener()

either of these two works.

Earlier I was just setting up the checkbox but not setting up a listener on the check box as above

checkBox.setChecked(!checkBox.isChecked()); checkBox.setChecked(true);

Now with setOnClickListener all I have to do is store the position and boolean value of the checkbox to the LinkHashmap.

checkBox.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                if (((CheckBox) v).isChecked()) {
                   saveValues("check" + position, true);
                    Toast.makeText(MainActivity.this,"position: "+getValues("check"+position), Toast.LENGTH_SHORT).show();
                }
                else {

                    saveValues("check"+position,false);
                }

            }

        });

Later on I can see the checked items using

  public Boolean getValues(String a){
     return mylist.get(a).booleanValue();
        }

I am really thankful to the contributors of the below post

Android: checkbox listener

Community
  • 1
  • 1
sant123
  • 1
  • 4