0

I created a function with name setRecyclerViewSingleItems so that i can restrict the user to select only one option from the given options ( those options are stored in arrrayList ). I want to create this function such that i can do this multiple times but without creating the function multiple times.

When i do that for a single type of list by defining their variables inside the function it works but not for every list.

So could anyone please explain me how can I solve this issue.

I tried what the IDE suggesting me by making a another final variable but after that app crashes with null pointer exception.

public class employer_after_login_form extends AppCompatActivity{
    RecyclerView typeOfOrgRV, typeOfSectorRV;
    ArrayList<String> typeOfOrgArrList, typeOfSectorArrList;
    Multiple_Choice_Adapter typeOfOrgAdapter;
    Multiple_Choice_Listener typeOfOrgListener;
    Single_Choice_Adapter typeOfSectorAdapter;
    Single_Choice_Listener typeOfSectorListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_employer_after_login_form);

        //[START : type of organization code here]
        typeOfOrgRV = findViewById(R.id.typeOfOrgRV);
        String[] typeOfOrgStr = getResources().getStringArray(R.array.typeOfOrganisation);
        typeOfOrgArrList = setArrayList(typeOfOrgStr);
        setRecyclerViewMultipleItems(typeOfOrgRV, typeOfOrgArrList, typeOfOrgAdapter, typeOfOrgListener);
        //[END]


        //[START : industry OR sector choice]
        typeOfSectorRV = findViewById(R.id.typeOfSectorRV);
        String[] typeOfSectorStr = getResources().getStringArray(R.array.typeOfOrganisation);
        typeOfSectorArrList = setArrayList(typeOfSectorStr);
        setRecyclerViewSingleItems(typeOfSectorRV, typeOfSectorArrList, typeOfSectorAdapter, typeOfSectorListener);
        //[END]
    }

    private void setRecyclerViewSingleItems(RecyclerView recyclerView, ArrayList<String> arrayList, Single_Choice_Adapter singleChoiceAdapter, Single_Choice_Listener singleChoiceListener) {
        singleChoiceListener = new Single_Choice_Listener() {
            @Override
            public void onSingleItemSelected(String s) {
                recyclerView.post(new Runnable() {
                    @Override
                    public void run() {
                        singleChoiceAdapter.notifyDataSetChanged();
                    }
                });
                Toast.makeText(employer_after_login_form.this, s, Toast.LENGTH_SHORT).show();
            }
        };
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        singleChoiceAdapter = new Single_Choice_Adapter(this, arrayList, singleChoiceListener);
        recyclerView.setAdapter(singleChoiceAdapter);
    }

    private void setRecyclerViewMultipleItems(RecyclerView recyclerView, ArrayList<String> arrayList, Multiple_Choice_Adapter multipleChoiceAdapter, Multiple_Choice_Listener multipleChoiceListener) {
        typeOfOrgRV.setHasFixedSize(true);
        typeOfOrgListener = new Multiple_Choice_Listener() {
            @Override
            public void onMultipleItemSelected(ArrayList<String> arrayList) {
                typeOfOrgRV.post(new Runnable() {
                    @Override
                    public void run() {
                        typeOfOrgAdapter.notifyDataSetChanged();
                    }
                });
                Toast.makeText(employer_after_login_form.this, arrayList.toString(), Toast.LENGTH_SHORT).show();
            }
        };
        typeOfOrgRV.setLayoutManager(new LinearLayoutManager(this));
        typeOfOrgAdapter = new Multiple_Choice_Adapter(this, typeOfOrgArrList, typeOfOrgListener);
        typeOfOrgRV.setAdapter(typeOfOrgAdapter);
    }

    private ArrayList<String> setArrayList(String[] stringArray) {
        ArrayList<String> arrayList = new ArrayList<>(Arrays.asList(stringArray));
        return arrayList;
    }

    /*@Override
    public void onMultipleItemSelected(ArrayList<String> arrayList) {
        Toast.makeText(this, arrayList.toString(), Toast.LENGTH_SHORT).show();
    }*/
}
  • To be "effectively final" you must not re-assign the variable in question. In this case it is that simple: don't re-assign to `singleChoiceAdapter` later in the method. Instead, you can pass that `new ...` call to `#setAdapter` directly, make a new variable to hold the new adapter, or even make a "copy" variable before your listener, in order to keep that copy reference as effectively final. – Rogue Apr 04 '23 at 14:03
  • @Rogue Thankyou for replying to my problem. Your explanation solves the problem which I asked firstly but now when I run the application it gives the error "Adapters.Single_Choice_Adapter.notifyDataSetChanged()' on a null object reference". So i researched about it and found that my arraylist could be empty. But I am sure of that it is not empty. Could you help me there? – Akshat Agrawal Apr 04 '23 at 17:28
  • At that point you may want to open a new question, since the topic has fundamentally changed (you could comment it here to me). That said, the full error does matter. If you got a NullPointerException, [refer to here](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it). The stacktrace should point to the line that is in error, but I wouldn't see it happening as a result of what is currently shown. If you find a `null` variable but don't understand how it became `null`, then you need to debug what set its value and from where. – Rogue Apr 04 '23 at 20:19

0 Answers0