0

Let say I have this String array "{Unclaimed", "Lost/Stolen", "Damaged/Defective", "Pawned", "Not Turned Over", "Others"} and I want to remove the first one which is Unclaimed , It will remove if aat_distribution_status is being clicked is there anyway to remove it? I tried Reasons = Reasons.Skip(1).ToArray() but it doesn't work

My sample code

AutoCompleteTextView aat_reason_not_presented;
aat_reason_not_presented = findViewById(R.id.aat_reason_not_presented);

String[] Reasons = new String[]{"Unclaimed", "Lost/Stolen", "Damaged/Defective", "Pawned", "Not Turned Over", "Others"};
ArrayAdapter<String> adapterYesNo = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, Reasons);

adapterYesNo.setDropDownViewResource(simple_spinner_dropdown_item);
aat_reason_not_presented.setAdapter(adapterYesNo);

 aat_distribution_status.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View arg1, int pos, long id) {

                 Reasons = Reasons.Skip(1).ToArray() //Trying to remove first element of reasons but it didn't work

                }
            });
1
  • 1
    Arrays have fixed size so you can't "remove* some element since that would affect its size. You can replace that element with something else. So for instance if you have {a, b, c} and you want to get rid of a you can't modify that array into {b, c} (since now its size is 2 not 3), but what you can do is shifting elements and filling blanks with null like {b, c, null} (or use other values representing no element). To have array with reduced size you will need to create new array of that size and fill it with data you want. Alternatively you can use List which is resizable. Commented Oct 25, 2022 at 12:37

1 Answer 1

2

You can use Arrays.copyOfRange(T[], int, int) like

Reasons = Arrays.copyOfRange(Reasons, 1, Reasons.length);

Also, Java variable names start with a lowercase letter by convention. Finally, I suspect that Reasons must be effectively final to use it in the inner class like that. So I doubt that will work as is.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.