Anthraxff
Anthraxff

Reputation: 172

Cant get an int value from on class to another class : android studio

I'm kind of new to android. I want to get an int value id(id of dynamically generated buttons) from class MainPage into class Note when a button is clicked. but the value of id always turns to zero in Note class.

here's the summerized code:

MainPage class:

public class MainPage extends AppCompatActivity {
    public int id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        for (int j=0; j<allnotes.size(); j++) {
            //generating buutons          
            final Button preview_text = new Button(this);
            preview_text.setId(j)         
            preview_text.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    //getting id of the clicked button
                    id=v.getId();
                    startActivity(new Intent(MainPage.this, Note.class));
                  
                }
            });
        }
    }
}

Notes class:

public class Note extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.note);

        MainPage obj=new MainPage();
        int id=obj.id

        View parentLayout = findViewById(android.R.id.content);
        Snackbar mySnackbar = Snackbar.make(parentLayout,Integer.toString(id) , 10000);
        mySnackbar.show();
    }

in snackbar message, id is always zero.

Upvotes: 2

Views: 224

Answers (3)

Guhanmuthu Selvaraj
Guhanmuthu Selvaraj

Reputation: 253

we can do this via intent Because Android Intent is the message that is passed between components such as activities, content providers, broadcast receivers, services, etc.

In Our case, we can send the v.getId() to Note.java, viaputExtra

intent.putExtra("view_id",v.getId());

and receive the value in Note.java by using

getIntent().getIntExtra("view_id", 0);

Upvotes: 1

Shoaib Kakal
Shoaib Kakal

Reputation: 1336

Here I implemented @Riccully Answer in your code.

MainPage.java

preview_text.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        //getting id of the clicked button
        id = v.getId();
        Intent intent = new Intent(MainPage.this, Note.class);
        intent.putExtra("id_value", id);
        startActivity(intent);
    }
});

Note.java

@Override
protected void onCreate (Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.note);

    int id = getIntent().getIntExtra("id_value", 0);

    View parentLayout = findViewById(android.R.id.content);
    Snackbar mySnackbar = Snackbar.make(parentLayout, Integer.toString(id), 10000);
    mySnackbar.show();
}

Upvotes: 2

Ridcully
Ridcully

Reputation: 23665

You need to add the id to the Intent you use to start the Note activity. You do this by using Intent.putExtra(...) and in Note you retrieve it via getIntent().getIntExtra(...)

Upvotes: 3

Related Questions