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.
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With