java - 如何从 Java 中的另一个类获取变量?

标签 java android

我想获取类 MainActivity 类型中的 id_categories 变量,以便在类 ListFood 的方法 LoadListFoodbyId (int id) 中使用。

主 Activity .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();

    }

我尝试通过 ListFood 在类 mainActivity 中调用方法 LoadListFoodbyId (int i):

ListFood lf = new ListFood(); 

lf.LoadListFoodbyId (id_categories)

并尝试另一种方法是获取 MainActivity 类 ListFood 中使用的类 MainActivity id_categories 中的变量:

MainActivity main = new MainActivity(); 
LoadListFoodbyId (main.getId ()), 

但都没有成功。

最佳答案

  1. 您将其添加到 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. 随时在 ListFood 类中打开它。

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

关于java - 如何从 Java 中的另一个类获取变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27480066/

相关文章:

java - 检查字符串在符合条件的字符串中包含字符 'g' 的次数

java - 按照模式指定的顺序在序列下的 XML 中插入元素

android - 如何在通知中添加按钮?

android - 致命异常 : java. lang.StackOverflowError 堆栈大小 8MB android.view.View.hasIdentityMatrix

java - Saxon XQuery 内存管理

java - Android Volley Json 请求 : Parameters not showing

javascript - Ajax将带有两个数组的JSON对象发送到servlet并在没有jQuery的java servlet中解析

Android colorControlNormal xml 属性在 Lollipop 之前不起作用

android - ExoPlayer 在恢复时恢复状态

java - java中的FileNotFoundException