Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a variable from another class in Java?

Tags:

java

android

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.

like image 762
Tan Nguyen Avatar asked Jan 09 '23 06:01

Tan Nguyen


1 Answers

  1. 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);
    
    1. Open it in the ListFood class, whenever you need it.

    Bundle b = getIntent().getExtras(); int id_categories = bundle.getInt("id_categories");

like image 118
Joseph Graddy Avatar answered Jan 22 '23 13:01

Joseph Graddy