1

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.

Treewallie
  • 346
  • 2
  • 13
  • `Within onCreate` - I think here lays your problem. `onCreate` is called only when `Activity` needs to be created. If you start second `Activity` the first one will go in `onPause` after you `finish()` with your second `Activity` first `Activity` will be resumed by calling `onResume` so I would move that code here instead of `onCreate` – Yupi Sep 03 '19 at 15:16
  • I'll clarify; the name selection screen shows up first and after a user selects a name they are sent to the main activity where I want to delete the name from the list the user selected. – Treewallie Sep 03 '19 at 15:18
  • 1
    Your `adapter` is inside `SpinnerSelection` not in `MainActivity` correct? – Yupi Sep 03 '19 at 15:21
  • Correct. The adapter and spinner are all in the Spinner Activity. Once user picks a name, it sends them to the Main Activity. – Treewallie Sep 03 '19 at 15:30

1 Answers1

1

Task which you want to acomplish requires different approach.

Summary of problem:

Your adapter is inside SpinnerSelection which is finished immediately after user selects value from Spinner that means that every next time when SpinnerSelection is started you will have same values again in Spinner because Spinner is re-created inside onCreate and populated with values from string-array. Hardcoded resource files are constant can not be removed on runtime.

Solution:

You need to store values for Adapter somewhere else, for example in SQLite or SharedPreferences (if it is small amount of data) not in string-array. That will allow you to delete the values from place where you have saved them and once value is deleted from there, Spinner will not have it when SpinnerSelection is launched.

Yupi
  • 4,402
  • 3
  • 18
  • 37
  • This sounds promising. Do you have an example code of how to use SharedPreferences to display the list in the spinner? – Treewallie Sep 03 '19 at 15:59
  • 1
    That is the way if you want to delete the value regardless of app state unless user `re-install` the app. If you want that value is removed in that case as well consider using some online database like `Firebase`. Common way to do that is to convert `List` of values in `JSON` and than save that `JSON` as a `String` for example: https://stackoverflow.com/questions/28107647/how-to-save-listobject-to-sharedpreferences – Yupi Sep 03 '19 at 16:04
  • What if I create a List names = new ArrayList(); names.add("Name 01"); ? When i try to populate the list into the adapter like this ``adapter = ArrayAdapter.createFromResource(this, deviceNameList, android.R.layout.simple_spinner_item);`` I get an error "Wrong 2nd Arg type. Required (Int)" – Treewallie Sep 03 '19 at 16:10
  • Figured it out. Changed the adapter to this ``adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, nameList);`` and just use adaper.remove() to remove from the list. Good leads. – Treewallie Sep 03 '19 at 16:29
  • Yes because `Spinner` accepts primitive `array` not `generic List` you need to convert `List` to `array`. You can remove the value in runtime from the `List` but once app starts again you will have same values in `Spinner` so saving somewhere else which will keep the values regardless of app state is necessary in your case. – Yupi Sep 03 '19 at 16:31