0

I still am a beginner in Android development and will try to make my question as clear as possible with a schema of what I have in my mind.

Purpose of this application:
- I want the user to have the choice between a few buttons and when clicking on any of them, it would open a list view with different content according to the button. ex : if you click on "Category_1" button, only elements with a fitting id will appear in the listview.

So far, I have :
- defined my "handler" class (extends SQLiteOpenHelper) : name/path of DB, definition of CRUD, .getReadableDatabase, etc.
- define a class for my table, in my case "Restaurants.java" with getters/setters and constructor. - defined my MainActivity with empty listeners for my button.
- defined my "DatabaseAdapter.java" in which I want to define the methods/sql requests which will communicate with the database and get the information I want from it.
- defined my ListViewActivity with nothing to display so far.

Here is a schema of what I want with the idea of how to make it to try to optimize my application :
Schema

To sum up:
- I want a listener for each button setting a variable to a certain value (for example: "if you click on 1 then set the value of A to 1") and opening the ListViewActivity.
- There would be a method defined in "...Adapter.java" sending a request to the database and having the variable A defined earlier as an input. - And then, when clicking on the button, the ListViewActivity will open and call the method from "..Adapter.java", and finally display the results.

So, here are my questions :
- First of all, is the design optimized enough to allow my application to run fast? I think it should as all the button open only one activity and there is only one method defined for all buttons.
- I have a hard time defining the method in "...Adapter.java" which will be called from my ListViewAcitivity. The input should be the variable obtained when clicking on the button but I don't really know how to get a variable in one activity, open a second activity where to display results by using the variable in a third activity... :s
Is it fine to set a variable to a certain value when we click on a button and use this variable in another class as an input for a method?

public findNameInTable(int A){ string sql = " select NAME from MY_TABLE where CAT1 = " + A; c = database.rawQuery(sql, null); } 

Thanks in advance for any indications, suggestions or links which could help me to make my application come true, and sorry if some questions really sounds newbie, I am starting !
Have a good day !

1 Answer 1

1

Part 1: The best way I have found to pass variables to other activities is with a putExtra(String, variable);. Say you change the variable name on a button press, you can then call:

YourNewActivityClassName var = new YourNewActivityClassName(); Intent i = new Intent(context, YourNewActivityClassName.class); i.putExtra("name", name); startActivity(i); 

Then in the activity you just created, you can call this in the onCreate method:

Intent i = getIntent(); final String name = i.getStringExtra("name"); 

Of course this is assuming the variable was defined as a String before the putExtra was called.

If you want to use other variable types, there are different get***Extra commands you can call like getIntExtra(int, defaultval) but the putExtra will still be used to send it.

Part 2: For calling a method with a variable assigned in a button click, I have found the best way to do this is with a "holder class" this holder can be defined as a final, and a button press assigns a value to one of it's slots. Here is my holder for Integers:

public class holder { int to; public void setTo(int to){ this.to = to; } public int getTo(){ return to; } } 

I instantiate my class as final within my on create final holder hold = new holder(); then call my hold.setTo(int); within a list click listener. When I want to get the data, I simply call hold.getTo(); and I have my integer.

Heres a similar post: Pass value outside of public void onClick

Hope this helps! Mike

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Mike, it really helped as I could also identify more clearly my problems and I didn't know about putExtra(). I am done with part 1, it's working, I can pass a value from one activity to another by clicking on my button. I'm gonna focus on part2 now! Thanks a lot!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.