我如何从另一个 activity 调用 ArrayAdapter 以从具有 strings.xml 数组的 ArrayAdapter 中删除项目?

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

问题总结:adapter和spinner都在Spinner中Activity。一旦用户选择了一个名字,它就会将它们发送到 Main Activity。我想从另一个 activity 调用 ArrayAdapter 并从该列表中删除一个项目,但似乎无法弄清楚该怎么做。我使用 ArrayAdapter 并从 strings.xml 中附加一个列表并使用 string-array name="horizon_names"

Call custom ArrayAdapter method from another Activity 没有回答我的问题。

我试过的方法: 我试过制作 ArrayAdapter public 并尝试在另一个 activity 中调用它,但什么也没有向上。

微调器选择 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) {

}


}

主要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>

预期和实际结果我希望能够从另一个activity调用ArrayAdapter,并删除用户在main中选择的项目activity 但我无法在 MainActivity.

中调用 ArrayAdapter

您要完成的任务需要不同的方法。

问题总结:

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.

解决方案:

您需要将 Adapter 的值存储在其他地方,例如 SQLiteSharedPreferences(如果它是少量数据)而不是 string-array。这将允许您从保存它们的地方删除值,一旦从那里删除值,启动 SpinnerSelectionSpinner 将不会有它。