我有两个活动MainActivity和MenuActivity。我需要将Menu Activity中的ArrayList传递给Main Activity。我是用意图做的。
MenuActivity.java
public class MenuActivity extends ActionBarActivity {
Button add,save;
EditText subject;
ArrayList list = new ArrayList();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.subject_main);
add = (Button) findViewById(R.id.button1);
subject = (EditText) findViewById(R.id.editText1);
save= (Button) findViewById(R.id.button2);
add.setOnClickListener(new OnClickListener() {
Context context;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String content;
content = subject.getText().toString();
MainActivity.myStringList.add(content);
ArrayAdapter dataAdapter = new ArrayAdapter(MenuActivity.this,android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//spinner1.setAdapter(dataAdapter);
// Showing selected spinner item
Toast.makeText(getApplicationContext(),
content +"Added", Toast.LENGTH_LONG).show();
}
});
save.setOnClickListener(new OnClickListener() {
Context context;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i1 = new Intent(MenuActivity.this, MainActivity.class);
i1.putStringArrayListExtra("LIST",list);
startActivity(i1);
}
});
}
}
Arraylist列表需要在MainActivity中使用
MainActivity.java
public class MainActivity extends ActionBarActivity {
Spinner spinner1,spinner2,spinner3;
public static List myStringList = new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(myStringList.isEmpty())
{
Intent myIntent = new Intent(MainActivity.this, MenuActivity.class);
MainActivity.this.startActivity(myIntent);
}
// Spinner element
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner3 = (Spinner) findViewById(R.id.spinner3);
ArrayAdapter dataAdapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_spinner_item, myStringList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter);
// Spinner click listener
//spinner2.setOnItemSelectedListener(this);
//spinner3.setOnItemSelectedListener(this);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapter, View v,
int position, long id) {
// On selecting a spinner item
String item = adapter.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
//do something here
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
//optionally do something here
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId())
{
case R.id.settings:
// Single menu item is selected do something
// Ex: launching new activity/screen or show alert message
return true;
case R.id.help:
return true;
case R.id.subjects:
Intent myIntent = new Intent(MainActivity.this, MenuActivity.class);
MainActivity.this.startActivity(myIntent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
如何在MainActivty中显示ArrayList?
答案 0 :(得分:0)
MainActivity是您启动应用时发生的第一项活动吗?如果是,当onCreate第一次运行时,将不会填充list1。我假设您的MainActivity在某个时刻调用了MenuActivity,然后您的MenuActivity通过使用intent返回MainActivity,此时MainActivity会更新spinners?您可以尝试的其他内容是在MainActivity的onCreate之前输入:
public static List<String> myStringList = new ArrayList<String>();
当您添加到MenuActivity中的列表时,请使用MainActivity.myStringList.add(content);
然后,当您完成添加到MenuActivity中的列表时,您可以调用finish();
返回MainActivity,您可以通过覆盖onResume方法来更新微调器。示例 - 在onCreate方法下面的MainActivity中:
@Override
public void onResume(){
super.onResume();
try{
spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, myStringList.toArray(), android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
} catch (RuntimeException e){
Log.d("error",e+"");
}
}
这是来自记忆,所以如果它有一些问题我很抱歉。 runtimexception是这样的,它不会在第一次调用onResume时崩溃(在初始化其他任何东西之前)。