Reputation: 65
I want to get the id_categories variable in class MainActivity types for use in methods LoadListFoodbyId (int id) in class ListFood.
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.list_store_View);
LoadCategories();
lv.setOnItemClickListener(new CategoriesClickListener());
}
// The click listener for ListView in Store List
private class CategoriesClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Categories item = cateAdapter.getItem(position);
Intent itent = new Intent(getApplicationContext(), ListFood.class);
itent.putExtra("item", item);
int id_categories = item.getId(); //i want to get this variable
String log = "ID: " + id_categories;
startActivity(itent);
Log.d("ID: ", log);
}
}
ListFood.java
public class ListFood extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_dish);
LoadListFoodbyId(4); //use variable in here
}
public void LoadListFoodbyId(int i) {
DataApdapter mDbHelper = new DataApdapter(this);
mDbHelper.createDatabase();
mDbHelper.open();
ArrayList<Food> listFoodById = mDbHelper.getAllFoodById(i);
listFoodAdapter = new ListFoodArrayAdapter(this, R.layout.list_dish,
listFoodById);
lv.setAdapter(listFoodAdapter);
mDbHelper.close();
}
I tried calling the method LoadListFoodbyId (int i) in the class mainActivity by ListFood:
ListFood lf = new ListFood();
lf.LoadListFoodbyId (id_categories)
and try the other way is to take variables in class MainActivity id_categories used in class ListFood by MainActivity:
MainActivity main = new MainActivity();
LoadListFoodbyId (main.getId ()),
but all were unsuccessful.
Upvotes: 4
Views: 178
Reputation: 71
You add it to the intent.
Categories item = cateAdapter.getItem(position);
Intent itent = new Intent(getApplicationContext(), ListFood.class);
itent.putExtra("item", item);
int id_categories = item.getId(); //i want to get this variable
String log = "ID: " + id_categories;
itent.putExtra("id_categories", id_categories) //you can call your variable whatever you want, I have called it "id_categories" for convenience.
startActivity(itent);
Log.d("ID: ", log);
Bundle b = getIntent().getExtras();
int id_categories = bundle.getInt("id_categories");
Upvotes: 3
Reputation: 1231
In your MainActivity class define the variable id_categories example:
public class MainActivity {
private int id_categories;
...
change this line code
int id_categories = item.getId();
in
id_categories = item.getId();
and create a function in class MainActivity
public int getIdcategories(){
return id_categories;
}
Upvotes: 2
Reputation: 547
put it in an intent in your main activity, like this
itent.putExtra("id", item.getId());
and retrieve it in list food like this
int id=getIntent().getIntExtra("id",0); // 0 is default value if id is not found in intent
Upvotes: 2
Reputation: 4571
Declare id_categories
as public static int id_categories
in MainActivity
before onCreate. Then you can access it in any class as MainActivity.id_categories
.
Upvotes: 2