我正在尝试制作一个应用,我想要在其中将值从editText(MainActivity.java)
传递到放置在另一个活动中的listView(bestFitActivity.java)
。我的代码的问题在于,它仅将一个值传递给listView
。输入新值时,它将替换先前的值。
MainActivity.java
public class MainActivity extends AppCompatActivity {
int i = 0;
String getChunkSize;
ArrayList<String> chunkSizeArray = new ArrayList<>();
EditText chunkSize;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button chunkSave = (Button)findViewById(R.id.chunkSave);
Button processSave = (Button)findViewById(R.id.processSave);
Button bestFitDirector = (Button)findViewById(R.id.bestFitdirector);
Button worstFitDirector = (Button)findViewById(R.id.worstFitdirector);
Button firstFitDirector = (Button)findViewById(R.id.firstFitDirector);
EditText processSize = (EditText)findViewById(R.id.processSize);
chunkSize = (EditText)findViewById(R.id.chunkSize);
chunkSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i++;
Log.d("Manager" , "%d" +i);
getChunkSize = chunkSize.getText().toString();
//Log.d("Manager" , "" +getChunkSize);
if(getChunkSize == null ) {
Toast.makeText(getBaseContext(), "Empty Input", Toast.LENGTH_SHORT).show();
}
}
});
bestFitDirector.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this , bestFitActivity.class);
myIntent.putExtra("chunkSizeArray" , getChunkSize );
startActivity(myIntent);
}
});
/*worstFitDirector.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent2 = new Intent (MainActivity.this , worstFitActivity.class);
startActivity(myIntent2);
}
});
firstFitDirector.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent3 = new Intent (MainActivity.this , firstFitActivity.class);
startActivity(myIntent3);
}
});*/
}
}
bestFitActivity.java
public class bestFitActivity extends AppCompatActivity {
String chunkSIZE;
ArrayList<String> chunkSizeArray = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_best_fit);
ListView blockSize = (ListView)findViewById(R.id.blockSize);
ListView finalProcessList = (ListView)findViewById(R.id.finalprocesslist);
chunkSIZE = getIntent().getExtras().getString("chunkSizeArray");
Log.d("Manager" , " "+chunkSIZE);
chunkSizeArray.add(chunkSIZE);
Log.d("Manager" , ""+chunkSIZE);
ArrayAdapter<String> adapter = new ArrayAdapter<>(bestFitActivity.this , android.R.layout.simple_expandable_list_item_1 , chunkSizeArray);
blockSize.setAdapter(adapter);
adapter.notifyDataSetChanged();
//((EditText)findViewById(R.id.chunkSize)).setText(" ");
//EditText tv = (EditText) findviewbyID(R.id.)
}
}
PS:该代码不完整
答案 0 :(得分:5)
每次单击按钮时,都会使用一个值创建一个新的活动,其中包含一个新的空chunkSize ArrayList。
您可以在每次单击按钮时将其添加到MainActivity中的 chunkSizeArray ,然后在单个intentExtras中发送一个计数器和该数组的值。
然后从bestFitActivity中的所有意向中检索并构建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中。