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();
}*/
}