Android:来自视图类的值来回传递给另一个活动

时间:2014-03-21 05:35:30

标签: android android-intent android-activity

我已设法从我的视图类切换到新活动。它通过对话框启动新活动的开始,并将特定圆圈的名称传递给新活动。但是,我无法将名称成功传递给新活动,并显示在新活动中的文本视图中。有关新活动的文本视图仍为空。请就此问题提出建议。谢谢。

从我的视图类中修改代码:

    @Override
        public boolean onTouchEvent(MotionEvent event) {

            x = event.getX();
            y = event.getY();


            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:


                for (int i = 0; i < circles.size(); i++) {


                    if (circles.get(i).contains(x, y)) {
                        circleID = i;

        Handler handler = new Handler();
                            handler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    AlertDialog.Builder builder = new Builder(
                                            getContext());
                                    final EditText text = new EditText(getContext());

                                    builder.setTitle("Adding colors to circles").setMessage(
                                            "Proceed to Enter color");
                                    builder.setPositiveButton("Yes",
                                            new DialogInterface.OnClickListener() {

                                                public void onClick(DialogInterface di,
                                                        int i) {

                                                    Intent intent = new Intent(
                                                            getContext(),
                                                            colorActivity.class);

                                                    getContext().startActivity(intent);
                                                     intent.putExtra("circlename", circleNameList.get(circleID));
                                                }

                                            });
                                    builder.setNegativeButton("No",
                                            new DialogInterface.OnClickListener() {

                                                public void onClick(DialogInterface di,
                                                        int i) {
                                                }

                                            });

                                    builder.create().show();
                                }
                            }, 3000);
break;

}

在新活动中:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_ecolor);


        circlenametextview = (TextView)findViewById(R.id.circlenametextview);


        String circlename = super.getIntent().getStringExtra("circlename");
          circlenametextview.setText(circlename);
        }

1 个答案:

答案 0 :(得分:1)

更改此

  Intent intent = new Intent(getContext(),colorActivity.class);
  getContext().startActivity(intent); // you start activity
  intent.putExtra("circlename", circleNameList.get(circleID)); // then you have putExtra

  Intent intent = new Intent(getContext(),colorActivity.class); 
  intent.putExtra("circlename", circleNameList.get(circleID)); //putExtra first
  getContext().startActivity(intent); // then startactivity