anonymous23
anonymous23

Reputation: 55

How to send data by using putExtra from an Activity to a Java Class?

I want to send a username after login is successful to another Java Class.

Here is a snippet from my MainActivity.java:

    public class CheckLogin extends AsyncTask<String, String, String> {
        String msg ="";
        Boolean isSuccess = false;

        protected void onPreExecute() {

        }

        @Override
        protected String doInBackground(String... strings) {
            String username = loginUsername.getText().toString();
            String password = loginPassword.getText().toString();

            UserDetails ud = new UserDetails(username,password);

            if (DbConnectionClass.connection == null) {
                new DbConnectionClass().connectDB();
            }
            if (DbConnectionClass.connection != null) {
                Statement statement = null;
                try {
                    statement = DbConnectionClass.connection.createStatement();
                    ResultSet resultSet = statement.executeQuery("SELECT * FROM USERLOGINDETAILS_T WHERE user_name='"+ ud.getUsername() +"' AND user_password='"+ud.getPassword()+"'");
                    if(resultSet.next())
                    {

                        msg = "Login Successful";
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();
                            }
                        });

                        isSuccess = true;
                        Intent startIntent = new Intent(getApplicationContext(), TabbedActivity.class);

                        //PROBLEM HERE
                        Intent i = new Intent(MainActivity.this, taobaoListAdapter.class);
                        i.putExtra("Key", username);
                        //***

                        startIntent.putExtra("UserLoginDetails", username); //send user details to tabbedactivity
                        startActivity(startIntent);
                    }
                    else if (username.trim().equals("") || password.trim().equals("")) {
                        msg = "Please Enter Username and Password";
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();
                            }
                        });

                    }
                    else
                    {
                        msg = "Username or Password Incorrect";
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();
                            }
                        });

                        isSuccess = false;
                    }
                }
                catch(SQLException e)
                {

                }

            }
            return msg;
        }
    }

But in my receiving class, taobaoListAdapter.java, when I use String s = getIntent().getExtras("UserLoginDetails"); the getIntent() shows an error. I believe this is due to the Java Class not extending an Activity.

How do I get around this? I've looked at other questions similar to mine but could not get a solution.

Upvotes: 1

Views: 35

Answers (1)

Rishabh Saraswat
Rishabh Saraswat

Reputation: 26

You need to extend the class to appcompat activity. You are sending your data to adapter. Try getting data in appcompat activity and transferring it to adapter using constructor.

Upvotes: 1

Related Questions