我想将字符串传递给另一个活动

时间:2018-02-05 02:47:18

标签: java android firebase firebase-realtime-database

我想通过点击Listview将字符串传递给另一个活动,但它说我有问题。目的是在用户只想更改某些单词然后更新时修改数据更改对数据。

这是数据结构。 https://ppt.cc/f02tWx

它说:https://ppt.cc/f8ZYKx

这是点击部分的列表视图

    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {

           hom.addValueEventListener(new ValueEventListener() {
               @Override
               public void onDataChange(DataSnapshot dataSnapshot) {
                   adapter.getItem(position);
                   String title = dataSnapshot.child(String.valueOf(position)).getValue().toString();
                   Intent intent = new Intent(MeTodolist.this, MeTodolistModify.class);
                   Bundle bundle = new Bundle();
                   bundle.putString("title", title);
                   intent.putExtras(bundle);
                   startActivity(intent);
                   Log.e("CHANGE",title);
               }

               @Override
               public void onCancelled(DatabaseError databaseError) {

               }
           });
          }
      });

这是修改页面。

  public class MeTodolistModify extends AppCompatActivity implements View.OnClickListener {


        private String student_id,student_name,title;
        private EditText addtext;
        private Button sure;

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_me_todolist_modify);
            Intent intent = getIntent();
            Bundle bundle = getIntent().getExtras();
            String title = bundle.getString("title");
            student_id = intent.getStringExtra("student_id");
            student_name = intent.getStringExtra("student_name");
            createDetail();
        }

        private void createDetail(){

            final FirebaseDatabase db = FirebaseDatabase.getInstance();
            final DatabaseReference ref = db.getReference("Student").child(student_id).child("event");


            sure = findViewById(R.id.sure);

            sure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent intent;
                    switch (v.getId()) {
                        case R.id.sure:
                            intent = new Intent(MeTodolistModify.this, MeTodolist.class);
                            intent.putExtra("student_id", student_id );
                            intent.putExtra("student_name",student_name);
                            startActivity(intent);
                            break;
                    }

                }
            });

        }

        @Override
        public void onClick(View v) {

        }
    }

2 个答案:

答案 0 :(得分:1)

只需几行代码即可实现这一目标

Intent intent = new Intent(MeTodolist.this, MeTodolistModify.class);
String title = dataSnapshot.child(String.valueOf(position)).getValue().toString();
intent.putExtra("title", title);
startActivity(intent);
Log.e("CHANGE",title);

<强> MeTodolistModify

String title = getIntent().getStringExtra("title");

答案 1 :(得分:0)

你为什么要额外传递Bundle?只需使用一种方法。

           Bundle bundle=new Bundle();
           bundle.putString("title", title);
           intent.putExtras(bundle);
           startActivity(intent);

来自接收者。

 Intent intent = getIntent();
 Bundle bundle = getIntent().getExtras();
 if(bundle !=null)
 String title = bundle.getString("title");

您收到这些student_idstudent_name的原因。你没有通过它们。

    student_id = intent.getStringExtra("student_id");
    student_name = intent.getStringExtra("student_name");