简体   繁体   中英

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

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.

  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");

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;
}

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM