2 回答
TA贡献1843条经验 获得超7个赞
每次单击按钮时,都会创建一个带有新空 chunkSize ArrayList 的新活动,仅使用单个值创建。
您可以在每次单击按钮时添加到 MainActivity 中的chunkSizeArray,然后在各个 intentExtras 中发送一个计数器和数组的值。
然后从 bestFitActivity 中的所有 Intent Extra 中检索并构建 ArrayList。
这就是你可以做到的
public class MainActivity extends AppCompatActivity {
public static final String INTENTKEY_ARRAYELEMENTS = "arrayelemets";
public static final String INTENYKEY_ELEMENTX = "element_";
private static ArrayList<String> chunkSizeArray = new ArrayList<>();
Button bestFitDirector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bestFitDirector = (Button)findViewById(R.id.bestFitdirector);
bestFitDirector.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chunkSizeArray.add("Test" + String.valueOf(chunkSizeArray.size() + 1));
Intent myitent = new Intent(MainActivity.this,bestFitActivity.class);
myitent.putExtra(INTENTKEY_ARRAYELEMENTS, chunkSizeArray.size());
for (int i=0; i < chunkSizeArray.size(); i++) {
myitent.putExtra(INTENYKEY_ELEMENTX + String.valueOf(i),chunkSizeArray.get(i));
}
startActivity(myitent);
}
});
}
}
和
public class bestFitActivity extends AppCompatActivity {
ArrayList<String> chunkSizeArray = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_best_fit);
Intent mypassedIntent = getIntent();
int elements = mypassedIntent.getIntExtra(MainActivity.INTENTKEY_ARRAYELEMENTS,0);
for (int i = 0; i < elements; i++) {
chunkSizeArray.add(mypassedIntent.getStringExtra(MainActivity.INTENYKEY_ELEMENTX + String.valueOf(i)));
}
Toast.makeText(this,"The number of strings in the chukSizeArray is " + String.valueOf(chunkSizeArray.size()),Toast.LENGTH_SHORT).show();
for (String s: chunkSizeArray) {
Log.d("chunkSizeArray Value",s);
}
}
}
点击 5 次后(点击返回返回 MainActivity)日志为
2019-03-10 17:19:51.687 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test1
2019-03-10 17:19:56.362 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test1
2019-03-10 17:19:56.362 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test2
2019-03-10 17:19:59.877 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test1
2019-03-10 17:19:59.877 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test2
2019-03-10 17:19:59.877 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test3
2019-03-10 17:20:03.007 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test1
2019-03-10 17:20:03.007 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test2
2019-03-10 17:20:03.007 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test3
2019-03-10 17:20:03.007 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test4
2019-03-10 17:20:06.266 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test1
2019-03-10 17:20:06.266 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test2
2019-03-10 17:20:06.266 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test3
2019-03-10 17:20:06.266 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test4
2019-03-10 17:20:06.266 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test5
添加间隙以将点击分开
一种更简单的方法是使用putStringArrayListExtra和getStringArrayListExtra方法,它们基本上代表您执行循环。
所以主要活动有
public static final String INTENTKEY_ARRAY = "bestfitarray";
在 onClick 侦听器中,您可以:-
myitent.putStringArrayListExtra(INTENTKEY_ARRAY,chunkSizeArray);
随着
myitent.putStringArrayListExtra(INTENTKEY_ARRAY,chunkSizeArray);
在 bestFitActivity 中。
TA贡献1868条经验 获得超4个赞
你可以做一个简单的黑客攻击
改变ArrayList<String> chunkSizeArray = new ArrayList<>();
静态喜欢private static ArrayList<String> chunkSizeArray = new ArrayList<>();
添加回答
举报