Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using android studios and MongoDB atlas as database. But I cannot take output at the "usernameval" string. Please check my code and help me.




The output for " Log.v("msg","name"+usernameval);" in page 1 is

V/msg: name

thus value of usernameval is not shown.
please help

What I have tried:

my code:

page1=
package com.example.messanger;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import io.realm.Realm;
import io.realm.mongodb.App;
import io.realm.mongodb.AppConfiguration;

public class USERNAME extends AppCompatActivity {
String Appid="application-2-meqlj";
EditText usernamegiven;
Button name;
String usernameval;
public static final String USERNAME1="com.example.messanger.extra.Username";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_username);
Intent intent=new Intent(this,Login.class);
App app=new App(new AppConfiguration.Builder(Appid).build());




usernamegiven=findViewById(R.id.UserNameInput);
name=findViewById(R.id.NameContinue);
Realm.init(this);
usernameval=usernamegiven.getText().toString();
Log.v("msg","name"+usernameval);
intent.putExtra(USERNAME1,usernameval);

name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(intent);
}
});



}
}

page2=
package com.example.messanger;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.bson.Document;

import io.realm.Realm;
import io.realm.mongodb.App;
import io.realm.mongodb.AppConfiguration;
import io.realm.mongodb.Credentials;
import io.realm.mongodb.User;
import io.realm.mongodb.mongo.MongoClient;
import io.realm.mongodb.mongo.MongoCollection;
import io.realm.mongodb.mongo.MongoDatabase;

public class Login extends AppCompatActivity {
String Appid="application-2-meqlj";
EditText email;
EditText password;
MongoDatabase mongoDatabase;
MongoClient mongoClient;
MongoCollection mongoCollection;
User user;
public static final String EMAIL="com.example.messanger.extra.Name";
public static final String PASSWORD="com.example.messanger.extra.Name";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
email=findViewById(R.id.EmailAddressLogin);
password=findViewById(R.id.PasswordLogin);
Realm.init(this);

}
public void onStart(View v){
App app=new App(new AppConfiguration.Builder(Appid).build());

Intent intent=new Intent(this,MainPage.class);
Intent intentlog=getIntent();
String usernamelog=intentlog.getStringExtra(USERNAME.USERNAME1);

String emailval=email.getText().toString();
String passwordval=password.getText().toString();

Credentials credentials=Credentials.emailPassword(emailval,passwordval);
app.loginAsync(credentials, new App.Callback<user>() {
@Override
public void onResult(App.Result<user> result) {
if(result.isSuccess()){
startActivity(intent);
}
else {
Toast.makeText(Login.this, "incorrect val", Toast.LENGTH_SHORT).show();
}
}
});


intent.putExtra(EMAIL,emailval);
intent.putExtra(PASSWORD,passwordval);

if (usernamelog!=null) {


user = app.currentUser();
mongoClient = user.getMongoClient("mongodb-atlas");
mongoDatabase = mongoClient.getDatabase("MessageUsers");
mongoCollection = mongoDatabase.getCollection("UserName");
Document document = new Document("userId", user.getId()).append("Username", usernamelog);
mongoCollection.insertOne(document).getAsync(result -> {
if (result.isSuccess()) {
Log.v("Data", "done");
Log.v("msg","name"+usernamelog);

} else {
Log.v("Data", "Failed" + result.getError().toString());
}
;

});
}




}
}
Posted
Updated 24-Oct-21 7:02am
Comments
Richard MacCutchan 24-Oct-21 8:57am    
You need to check what happens at the line:
usernameval=usernamegiven.getText().toString();

Also, why are you calling toString on a field that is already a string?
Prathmesh Upadhyay 24-Oct-21 10:57am    
how do I check??
and it gives an error when toString is not called
Richard MacCutchan 24-Oct-21 11:48am    
Use the debugger to see what is happening. And if you get an error you need to tell us exactly what it says. We cannot see your screen or read your mind.
Prathmesh Upadhyay 24-Oct-21 13:00pm    
thanks mate , I got the answer

1 solution

just put usernameval=usernamegiven.getText().toString();
in the onclock listner
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_username);
        Intent intent=new Intent(this,Login.class);
        App app=new App(new AppConfiguration.Builder(Appid).build());




        usernamegiven=findViewById(R.id.UserNameInput);
        name=findViewById(R.id.NameContinue);
        Realm.init(this);


        name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                usernameval=usernamegiven.getText().toString();
                Log.v("msg","name"+usernameval);
                intent.putExtra(USERNAME1,usernameval);
                startActivity(intent);
            }
        });



    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900