0

当我单击提交按钮时,接下来是活动启动,但在文本视图中,它会在以下代码上生成空结果。

我想将用户详细信息存储到一个 ArrayList 中,它应该传递给另一个活动并单独显示,甚至作为一个组显示。

甲级

ArrayList<String> fieldText = new ArrayList<String>();
   Intent i1; Bundle bnd1;

btSubmit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                i1 = new Intent(WidgetDemo1Activity.this, Welcome.class);

                getDetails();

                startActivityForResult(i1,0);




            }
        });

public void getDetails(){
        fieldText.add(0, etuname.getText().toString());
        fieldText.add(1, etpass.getText().toString());
        //i1.putExtra("uname", etuname.getText().toString());
        //i1.putExtra("pass", etpass.getText().toString());
        bnd1 = new Bundle();
        //----- adding values of different field to bundle----- 
        //bnd1.putString("uname", etuname.getText().toString());
        //bnd1.putString("pass", etpass.getText().toString());
        bnd1.putStringArrayList("data", fieldText);
        i1.putExtras(bnd1);

        }

B类

tv1= (TextView) findViewById(R.id.txtView);
tv1.setText("Welcome "+ getIntent().getExtras().getStringArrayList("uname"));
4

3 回答 3

0

利用

 ArrayList<String> arry=new ArrayList<String>();
 arry= getIntent().getExtras().getStringArrayList("data");

代替

tv1.setText("Welcome "+ getIntent().getExtras().getStringArrayList("uname"));

因为您从第一个活动中data作为键传递并uname从第二个活动 中提取


编辑:

您也可以尝试在 Second Activity 中提取 ArrayList :

Bundle bundle = this.getIntent().getExtras();
ArrayList<String> fieldText = new ArrayList<String>();

if(bundle !=null)
{
  fieldText = bundle.getStringArrayList("data"); 
}
else
{

}

因为你正在传递ArrayListBundle不是Intent

于 2012-07-09T19:48:30.197 回答
0

您用于从包中查找的密钥不同。使用类似下面的东西。

tv1.setText("Welcome "+ getIntent().getExtras().getStringArrayList("data"));
于 2012-07-09T19:50:02.050 回答
0

A 类........

ArrayList<String> fieldText = new ArrayList<String>();
   Intent i1; Bundle bnd1;

btSubmit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                i1 = new Intent(WidgetDemo1Activity.this, Welcome.class);

                getDetails();

                startActivityForResult(i1,0);




            }
        });

public void getDetails(){
        fieldText.add(0, etuname.getText().toString());
        fieldText.add(1, etpass.getText().toString());
        //i1.putExtra("uname", etuname.getText().toString());
        //i1.putExtra("pass", etpass.getText().toString());
        bnd1 = new Bundle();
        //----- adding values of different field to bundle----- 
        //bnd1.putString("uname", etuname.getText().toString());
        //bnd1.putString("pass", etpass.getText().toString());
        bnd1.putStringArrayList("data", fieldText);
        i1.putExtras(bnd1);

        }

B类.......

TextView tv1;
protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.welcome);

        ArrayList<String> al= new ArrayList<String>();
        al = getIntent().getExtras().getStringArrayList("data");
        tv1= (TextView) findViewById(R.id.txtView);

        tv1.setText("Welcome "+ al.get(0)); // get string at 0 index 


    }
于 2012-07-09T20:21:48.510 回答