我创建了一个应用程序,它从 sql server 的 2 个表中获取多选数据。我使用了 2 个 edittext 来放置该数据;我正在使用捆绑包从 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);
}
});
}
}
活动列表viewHelper
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);
}
}
活动列表viewHelper2
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);
}
}