我创建了一个应用程序,它从sql server的2个表中获取多个选定的数据。 我用2个edittext来放置那些数据;我正在使用bundle从2个活动中获取数据,但我没有得到数据。 我的代码是:
try{
Bundle bn = getIntent().getExtras();
ArrayList<String> data= bn.getStringArrayList("selectedItems");
ArrayList<String> data2= bn.getStringArrayList("selectedItems2");
// String[] resultArr = b.getStringArray("selectedItems");
StringBuilder string = new StringBuilder();
for(int i=0;i<data.size();i++){
string.append(data.get(i).toString()+"\n");
}
for(int i=0;i<data2.size();i++){
string2.append(data2.get(i).toString()+"\n");
}
area.setText(string2);
wwith.setText(string);
} catch(Exception e){
e.printStackTrace();
}
有人可以建议我如何使用bundle从多个活动中获取数据吗?
主要活动的完整代码
public class DcrOpen extends Activity {
StringBuilder string=null;
StringBuilder string2=null;
EditText date,wwith,area;
Spinner with;
Button save,back,get,get1;
String paid5;
String dcrdate;
MyConnection mycon;
String mydate;
public void onCreate(Bundle b){
super.onCreate(b);
setContentView(R.layout.dcropen);
date=(EditText)findViewById(R.id.date);
wwith=(EditText)findViewById(R.id.workwith);
area=(EditText)findViewById(R.id.area);
with=(Spinner)findViewById(R.id.worktype);
save=(Button)findViewById(R.id.save);
back=(Button)findViewById(R.id.bkdays);
get=(Button)findViewById(R.id.get);
get1=(Button)findViewById(R.id.get1);
mycon=new MyConnection(DcrOpen.this);
int paid=MyConnection.PA_ID;
try{
Bundle bn = getIntent().getExtras();
ArrayList<String> data= bn.getStringArrayList("selectedItems");
ArrayList<String> data2= bn.getStringArrayList("selectedItems2");
// String[] resultArr = b.getStringArray("selectedItems");
StringBuilder string = new StringBuilder();
for(int i=0;i<data.size();i++){
string.append(data.get(i).toString()+"\n");
}
for(int i=0;i<data2.size();i++){
string2.append(data2.get(i).toString()+"\n");
}
area.setText(string2);
wwith.setText(string);
} catch(Exception e){
e.printStackTrace();
}
ResultSet rs2;
try{
Statement smt=mycon.connection().createStatement();
rs2=smt.executeQuery("DCRDATEDDL "+paid+"");
if(rs2.next()==false){
}
String mydate3=rs2.getString("DATE_NAME");
date.setText(mydate3);
rs2=smt.executeQuery("WORKINGTYPE");
List<String>data1=new ArrayList<String>();
while(rs2.next()){
data1.add(rs2.getString("FIELD_NAME"));
}
ArrayAdapter<String>dataadapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,data1);
dataadapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
with.setAdapter(dataadapter);
}catch(Exception e){
}
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(getApplicationContext(),DcrMenu.class);
startActivity(i);
}
});
get.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(getApplicationContext(),ListviewHelper.class);
startActivity(i);
}
});
get1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in=new Intent(getApplicationContext(),ListviewHelper2.class);
startActivity(in);
}
});
}
}
活动ListviewHelper
public class ListviewHelper extends Activity implements OnClickListener {
Button button;
ListView listView;
ArrayAdapter<String> adapter;
MyConnection mycon;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.data_list);
mycon=new MyConnection(ListviewHelper.this);
int pa_id=mycon.PA_ID;
ResultSet rs;
List<String>data=new ArrayList<String>();
try{
Statement smt=mycon.connection().createStatement();
rs=smt.executeQuery("WORKWITH "+pa_id+",'','','','','','',''");
while(rs.next())
{
data.add(rs.getString("PA_NAME"));
}
}catch(Exception e){
e.printStackTrace();
}
findViewsById();
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, data);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setAdapter(adapter);
button.setOnClickListener(this);
}
private void findViewsById() {
listView = (ListView) findViewById(R.id.list);
button = (Button) findViewById(R.id.testbutton);
}
public void onClick(View v) {
SparseBooleanArray checked = listView.getCheckedItemPositions();
ArrayList<String> selectedItems = new ArrayList<String>();
for (int i = 0; i < checked.size(); i++) {
// Item position in adapter
int position = checked.keyAt(i);
// Add sport if it is checked i.e.) == TRUE!
if (checked.valueAt(i))
selectedItems.add(adapter.getItem(position));
}
String[] outputStrArr = new String[selectedItems.size()];
List<String>data=new ArrayList<String>(selectedItems.size());
for (int i = 0; i < selectedItems.size(); i++) {
// outputStrArr[i] = selectedItems.get(i);
}
Intent intent = new Intent(getApplicationContext(),
DcrOpen.class);
// Create a bundle object
Bundle b = new Bundle();
b.putStringArrayList("selectedItems", selectedItems);
// Add the bundle to the intent.
intent.putExtras(b);
// start the ResultActivity
startActivity(intent);
}
}
活动ListviewHelper2
public class ListviewHelper2 extends Activity implements OnClickListener {
Button button;
ListView listView;
ArrayAdapter<String> adapter;
MyConnection mycon;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.data_list2);
mycon=new MyConnection(ListviewHelper2.this);
int pa_id=mycon.PA_ID;
ResultSet rs;
List<String>data=new ArrayList<String>();
try{
Statement smt=mycon.connection().createStatement();
rs=smt.executeQuery("DCRAREADDL "+pa_id+",'','','',''");
while(rs.next())
{
data.add(rs.getString("AREA"));
}
}catch(Exception e){
e.printStackTrace();
}
findViewsById();
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, data);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setAdapter(adapter);
button.setOnClickListener(this);
}
private void findViewsById() {
listView = (ListView) findViewById(R.id.list2);
button = (Button) findViewById(R.id.testbutton2);
}
public void onClick(View v) {
SparseBooleanArray checked = listView.getCheckedItemPositions();
ArrayList<String> selectedItems = new ArrayList<String>();
for (int i = 0; i < checked.size(); i++) {
// Item position in adapter
int position = checked.keyAt(i);
// Add sport if it is checked i.e.) == TRUE!
if (checked.valueAt(i))
selectedItems.add(adapter.getItem(position));
}
String[] outputStrArr = new String[selectedItems.size()];
List<String>data=new ArrayList<String>(selectedItems.size());
for (int i = 0; i < selectedItems.size(); i++) {
// outputStrArr[i] = selectedItems.get(i);
}
Intent intent = new Intent(getApplicationContext(),
DcrOpen.class);
// Create a bundle object
Bundle b = new Bundle();
b.putStringArrayList("selectedItems2", selectedItems);
// Add the bundle to the intent.
intent.putExtras(b);
// start the ResultActivity
startActivity(intent);
}
}
答案 0 :(得分:0)
您可以在填充引导您参加此活动的意图时使用getIntent.getExtras()
。例如 :
在活动1:
Intent intent = new Intent(activity1.this, activity2.class);
intent.putExtra("test", test);
startActivity(intent);
然后在第二个活动(活动2)中,你可以使用它:
String test = getIntent().getExtras().get("test");
但OnCreate()中的Bundle是您在当前活动中使用onSaveInstanceState(Bundle)
设置数据时可以使用的,用于保存数据,例如onStop()调用时。