1

I m make NAME and EMAIL equal to the data from other activity in first activity. Whem i m using NAME and EMAIL for toast in onActivityResult it showing the result but beyond this method its not working beside i make NAME and EMAIL a global variable.

public class startActivity extends ActionBarActivity {


       String sell="sell";
    Toolbar mToolbar;
    //////drawable navbar
    LinearLayout mystore;
    int  REQUEST_CODE=10110;
    private static ViewPager mPager;
    private static int currentPage = 0;
    private static int NUM_PAGES = 0;
    LinearLayout buysell;



     int PROFILE = R.drawable.sellit;

    private Toolbar toolbar;  
                            // Declaring the    Toolbar Object

      RecyclerView mRecyclerView;                           // Declaring 
      RecyclerView.Adapter mAdapter;                        // Declaring Adapter 
      RecyclerView.LayoutManager mLayoutManager;            // Declaring Layout 
      DrawerLayout Drawer;                                  // Declaring 

    ActionBarDrawerToggle mDrawerToggle;
    /////
      String name,mail;
    String NAME;
    String EMAIL;
       protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);


      mystore=(LinearLayout)findViewById(R.id.mystore);
        mystore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(startActivity.this,MybookStore.class));
            }
        });
        //toolbar
      init();

        buysell=(LinearLayout)findViewById(R.id.buysell);
        buysell.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(startActivity.this,buySecond.class));
            }
        });

        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        getSupportActionBar().setTitle("MybookStore");




        mRecyclerView = (RecyclerView) findViewById(R.id.RecyclerView); 

        mRecyclerView.setHasFixedSize(true); 

        mAdapter = new MyAdapter(TITLES,ICONS,NAME,EMAIL,PROFILE);   

        // And passing the titles,icons,header view name, header view email,
        // and header view profile picture

        mRecyclerView.setAdapter(mAdapter);    
                           // Setting the adapter to RecyclerView
        //RecyclerItemClickListener//
                mLayoutManager = new LinearLayoutManager(this);   
                // Creating a layout Manager

        mRecyclerView.setLayoutManager(mLayoutManager);  
               // Setting the layout Manager

        mRecyclerView.addOnItemTouchListener(
                new RecyclerItemClickListener(startActivity.this, new 
     RecyclerItemClickListener.OnItemClickListener() {
                    @Override public void onItemClick(View view, int position) {
                        switch(position){
                            case 1:
                                startActivity(new    
      Intent(startActivity.this,publishNewBook.class));
                        }
                    }
                })
        );


        Drawer = (DrawerLayout) findViewById(R.id.DrawerLayout);  
      // Drawer object Assigned to the view
        mDrawerToggle = new
         ActionBar
       DrawerToggle(this,Drawer,toolbar,R.string.drawer_open,R.string.drawer_c
      lose){

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                // code here will execute once the drawer is opened( As I dont 
        want anything happened whe drawer is
                // open I am not going to put anything here)
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                // Code here will execute once drawer is closed
            }



        }; // Drawer Toggle Object Made
        Drawer.setDrawerListener(mDrawerToggle); // Drawer Listener set to the
      Drawer toggle
                     // Finally we set the drawer toggle sync State
        mDrawerToggle.syncState();


    }




    public void login(View view){
    AlertDialog.Builder builder=new AlertDialog.Builder(this);
    builder.setMessage("Please Login To Publish Book ");
    builder.setCancelable(false);
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            Intent i=new Intent(startActivity.this, userLogin.class);
            startActivityForResult(i,REQUEST_CODE);
          //  startActivity(new Intent(startActivity.this, userLogin.class));
        }
    });
    builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
     AlertDialog alertDialog=builder.create();
    alertDialog.show();

     //startActivity(new Intent(startActivity.this,userLogin.class));

    }

    protected void onActivityResult(int requestCode, int resultCode, Intent 
    data) {
        super.onActivityResult(requestCode, resultCode, data);

     if(requestCode==REQUEST_CODE){
        if (resultCode ==110) {
            NAME=new String(data.getStringExtra("name1"));
            EMAIL=new String(data.getStringExtra("mail2"));
            Toast.makeText(getApplicationContext(), "Name " + NAME+" and mailId
      "+EMAIL+" get", Toast.LENGTH_LONG).show();

            //  mAdapter = new MyAdapter(TITLES,ICONS,NAME1,EMAIL1,PROFILE);

        }
   }
}
Nicky Manali
  • 386
  • 3
  • 22
  • 1) Please provide a [mcve] 2) We need to see where you are putting your variables into an Intent when (or if) you call `setResult` in the Activity that sends the `Intent data` back to this Activity – OneCricketeer Jun 04 '16 at 17:26
  • Also, what do you mean by "beyond this method"? Your code doesn't seem to do anything beyond that method, so if the Toast shows your variables, then what is the problem? – OneCricketeer Jun 04 '16 at 17:31

2 Answers2

4

do this

   //make a function updateAdapter() inside your adapter
   public void updateAdapter(/* gets params like TITLES,ICONS,NAME,EMAIL,PROFILE*/){

            // update adapter element like NAME, EMAIL e.t.c. here

            // then in order to refresh the views notify the RecyclerView
            notifyDataSetChanged();
   }

then modify the OnActivityResult method like so

   protected void onActivityResult(int requestCode, int resultCode, Intent 
   data) {
           super.onActivityResult(requestCode, resultCode, data); 

           if(requestCode==REQUEST_CODE){ 
              if (resultCode == RESULT_OK) { 
                NAME=new String(data.getStringExtra("name1")); 
                EMAIL=new String(data.getStringExtra("mail2")); 
                Toast.makeText(getApplicationContext(), "Name " + NAME+" and mailId 
                "+EMAIL+" get", Toast.LENGTH_LONG).show(); 

                //do this
                mAdapter.updateAdapter(TITLES,ICONS,NAME,EMAIL,PROFILE);

    } 
 } 
Muddassir Ahmed
  • 518
  • 4
  • 19
  • thanxx for rply.I think u understand my question.But it still showing null after doing this.plz help me – Nicky Manali Jun 05 '16 at 05:54
  • move the following code code into the `onActivityResult` ***mAdapter = new MyAdapter(TITLES,ICONS,NAME,EMAIL,PROFILE); // And passing the titles,icons,header view name, header view email, // and header view profile picture mRecyclerView.setAdapter(mAdapter); *** – Muddassir Ahmed Jun 05 '16 at 06:21
  • but we can't set Adapter in onAcivityResult I think it only instantiated in onCreate method().. – Nicky Manali Jun 05 '16 at 06:32
  • I have changed the answer even further! – Muddassir Ahmed Jun 05 '16 at 07:06
  • Could you rename the question to how to update `RecyclerView` element from `OnActivityResult` so that it will get more views – Muddassir Ahmed Jun 07 '16 at 05:29
0

The thing you are looking for Is passing data between activities. Firstly you should use fragments instead of activity, if you are still willing to use activities then it is answered here

How to send string from one activity to another?

Community
  • 1
  • 1
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
  • Firstly, this is not what he asked at all. Secondly, I don't know why you insisted he should use fragments instead of activities. Then you post a link instead of just telling him to use something like `intent.putExtra("email", email);` This is not a answer, its a bold statement that you could've posted as a comment. – HB. Jun 30 '17 at 10:31