gpt4 book ai didi

android - 只获取一个项目到微调器

转载 作者:搜寻专家 更新时间:2023-11-01 09:35:22 25 4
gpt4 key购买 nike

public class MainActivity extends AppCompatActivity {
ArrayList al = new ArrayList(20);
public static final String SharedPref = "MyPreferences";
SharedPreferences settings=null;
Spinner sp;
ArrayAdapter aa;
Intent i;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp = (Spinner) findViewById(R.id.spinner);
settings=getSharedPreferences(SharedPref,0);

Toast.makeText(this, al.size()+"", Toast.LENGTH_SHORT).show();

aa = new ArrayAdapter(this, R.layout.support_simple_spinner_dropdown_item, al);
sp.setAdapter(aa);

}

public void onResume()
{
super.onResume();
int j=0;
String s =settings.getString("NewTask","");
try {
for(j=0;j<20;j++) {
aa.insert(s,j);
aa.notifyDataSetChanged();
j++;
Toast.makeText(this, al.size()+"", Toast.LENGTH_SHORT).show();
sp.setAdapter(aa);
}
}
catch(Exception e)
{
Log.e("Array",e+"");
}
}



public void add1(View v)
{
i = new Intent(this,second.class);
startActivityForResult(i,1);

}}

我需要从其他 Activity 获取项目。我正在通过单击添加按钮移动到第二个 Activity。通过 edittext 添加项目并单击添加按钮。然后我只得到一个我添加的项目,但我需要所有之前添加的项目。请帮助我。我的要求是使用两种布局。(我知道将一个布局中的项目添加到微调器)。 arraylist 的大小在这里没有增加。如何修复我的 Arraylist 大小。

最佳答案

You can use startActivityForResult to pass data to activity

For example call startActivityResult from first activity

Intent i = new Intent(FirstActivity.this, SecondActivity.class);
startActivityForResult(i, 1);

And in second activity set data you want to return back like below

Intent returnIntent = new Intent();
returnIntent.putExtra("data",result);
setResult(Activity.RESULT_OK,returnIntent);
finish();

And in your first activity override the onActivityResult() method to get data

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK)
{
String result=data.getStringExtra("data");

}
if (resultCode == Activity.RESULT_CANCELED)
{

}

}
}

Hope this will help

关于android - 只获取一个项目到微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43693237/

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