简体   繁体   中英

How can I call an ArrayAdapter from another activity to delete item from ArrayAdapter with strings.xml array?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM