gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 22:52:18 26 4
gpt4 key购买 nike

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

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

我的尝试:我尝试将 ArrayAdapter 公开并尝试在其他 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,并删除用户在主 Activity 中选择的项目,但我无法在中调用 ArrayAdapter MainActivity。

最佳答案

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

问题总结:

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 中。这将允许您从保存它们的地方删除值,一旦从那里删除值,Spinner 将不会在 SpinnerSelection 启动时拥有它。

关于java - 如何从另一个 Activity 调用 ArrayAdapter 以使用 strings.xml 数组从 ArrayAdapter 中删除项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57774404/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com