Summary of problem: The adapter and spinner are all in the Spinner Activity. Once user picks a name, it sends them to the Main Activity. I want to call an ArrayAdapter from another activity and delete an item from that list but can't seem to figure out how to do so. I use an ArrayAdapter and attach a list to it from strings.xml
and using string-array name="horizon_names"
Call custom ArrayAdapter method from another Activity doesn't answer my problem.
What I have tried: I have tried making ArrayAdapter
public and trying to call it in the other activity but nothing comes up.
Spinner Selection Activity:
public class SpinnerSelection extends AppCompatActivity implements
AdapterView.OnItemSelectedListener {
Spinner spinner;
TextView tvPackage;
String stringSelectionPackage;
Button bSaveSelection;
Context context;
public ArrayAdapter<CharSequence> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner_selection);
context = this;
//---------------------------------------------------
// Name Selection (1-24)
spinner = findViewById(R.id.spinner);
tvPackage= findViewById(R.id.tvPackage);
//Array Adapter for creating list
adapter = ArrayAdapter.createFromResource(this, R.array.names,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
//Save Selection Button Code
bSaveSelection = findViewById(R.id.bSaveSelection);
bSaveSelection.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(),
MainActivity.class);
stringSelectionPackage=
tvPackage.getText().toString();
myIntent.putExtra("name", stringSelectionPackage);
startActivity(myIntent);
finish();
}
});
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i,
long l) {
//Put user selection into String text
String text = adapterView.getItemAtPosition(i).toString();
//Store text in tvPackage to send it to MainActivity and
place in @id/textViewName TextView
tvPackage.setText(text);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
Main Activity:
Within onCreate
//GETTING NAME FROM SPINNER SELECTION
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("name");
textViewName.setText(value);
**THIS IS WHERE I WANT TO DELETE ITEM FROM LIST**
adapter.remove(value);
}
strings.xml
<string-array name="names">
<item>Name 01</item>
<item>Name 02</item>
<item>Name 03</item>
<item>Name 04</item>
<item>Name 05</item>
</string-array>
Expected and actual results I expect to be able to call the ArrayAdapter from the other activity, and delete the item that the user picked in the main activity but I am not able to call the ArrayAdapter in the MainActivity.