简体   繁体   中英

I want to pass the string to another activity

I want to pass a string to another activity through click on the Listview , but it says I have something wrong.The purpose is to modify the data change when the user wants just to change some words and then update to the data.

Here is the data structure. https://ppt.cc/f02tWx

It says : https://ppt.cc/f8ZYKx

This is the listview on click part

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

               }
           });
          }
      });

This is the modifying page.

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

        }
    }

You can achieve that by just few lines of code

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

Why are you passing Bundle with intent extra? just use one method.

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

and from the receiver.

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

why you are receiving these student_id and student_name . you did not pass them.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM