简体   繁体   English

如何从另一个活动调用 ArrayAdapter 以使用 strings.xml 数组从 ArrayAdapter 中删除项目?

[英]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.问题总结: adapter 和 spinner 都在 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.我想从另一个活动调用一个 ArrayAdapter 并从该列表中删除一个项目,但似乎无法弄清楚如何这样做。 I use an ArrayAdapter and attach a list to it from strings.xml and using string-array name="horizon_names"我使用 ArrayAdapter 并从strings.xml并使用string-array name="horizon_names"将列表附加到它

Call custom ArrayAdapter method from another Activity doesn't answer my problem. 从另一个 Activity 调用自定义 ArrayAdapter 方法不能解决我的问题。

What I have tried: I have tried making ArrayAdapter public and trying to call it in the other activity but nothing comes up.我尝试过的:我尝试将ArrayAdapter设为公开并尝试在其他活动中调用它,但没有任何结果。

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字符串.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.预期和实际结果我希望能够从其他活动调用 ArrayAdapter,并删除用户在主活动中选择的项目,但我无法在 MainActivity 中调用 ArrayAdapter。

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 .您的adapter位于SpinnerSelection ,在用户从Spinner选择值finished立即finished ,这意味着每次启动SpinnerSelection时,您将在Spinner再次拥有相同的值,因为SpinneronCreatere-created并填充来自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 .您需要将Adapter值存储在其他地方,例如在SQLiteSharedPreferences (如果它是少量数据)而不是在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.这将允许您从保存它们的位置删除值,一旦从那里删除值,启动SpinnerSelectionSpinner将没有它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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