我试图通过点击按钮传递意图的链接列表组件按钮从第一个活动到第二个活动,并显示它在textview的第二个活动但是下面的代码不能正常工作
firstActivity.java
package com.example.intentsdemo;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
Button btn;
List<Button> array=new LinkedList<Button>();
EditText txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (EditText)findViewById(R.id.editText1);
btn = (Button)findViewById(R.id.btn1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:123456"));
//startActivity(intent);
boolean msg= array.add(btn);
// String msg = btn.getText().toString();
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
Bundle b = new Bundle();
b.putSerializable("data",(Serializable) array);
intent.putExtras(b);
startActivityForResult(intent, 111);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
secondActivity.java
package com.example.intentsdemo;
import java.io.Serializable;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
Bundle b = intent.getExtras();
Serializable x = b.getSerializable("data");
//txt.sett
TextView tt = (TextView)findViewById(R.id.txt);
tt.setText("bundle "+x);
/*Intent i = new Intent(SecondActivity.this,MainActivity.class);
b.putString("newMsg",x);
i.putExtras(b);
setResult(Activity.RESULT_OK, i);
finish();*/
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_second, menu);
return true;
}
}
帮助将是感激!!!!
你可以把你的LinkedList包装在一个实现了Parceleable
的Java对象中: