如何使用 bundle 一次从多个活动中获取数据

Posted

技术标签:

【中文标题】如何使用 bundle 一次从多个活动中获取数据【英文标题】:how to use bundle to get data from multiple activities at a time 【发布时间】:2013-08-13 11:17:08 【问题描述】:

我创建了一个应用程序,它从 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 从多个活动中获取数据?

主 Activity 的完整代码

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);
                        

【问题讨论】:

您从 bundle 中获取数据的趋势是正确的,但您能告诉我们您将数据放入您的意图的位置吗? 我写了完整的代码...... 但首先你需要用你的数据填充你的意图,例如:intent.putExtra("example", example);然后启动意图并在创建活动中使用您的代码获取数据:) 【参考方案1】:

当您填充引导您进行此活动的意图时,您可以使用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() 时)时使用的包。

【讨论】:

以上是关于如何使用 bundle 一次从多个活动中获取数据的主要内容,如果未能解决你的问题,请参考以下文章

一次从多个分区中选择

ef core 如何一次从具有外键的不同表中获取数据? (数据库优先)

有没有办法一次从 iPod 库中搜索多个艺术家的曲目。

一次从 numpy 数组中选择多个切片

一次从平衡二叉树中删除多个项目

如何一次从excel文件中读取一行数据