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

时间:2014-12-15 08:34:55

标签: java android

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

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

    }

我尝试通过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 ()), 

但都没有成功。

4 个答案:

答案 0 :(得分:3)

  1. 您将其添加到意图中。

    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类中打开它。
    2. Bundle b = getIntent().getExtras(); int id_categories = bundle.getInt("id_categories");

答案 1 :(得分:2)

在MainActivity类中定义变量id_categories示例:

public class MainActivity {

 private int id_categories;
  ...

更改此行代码

 int id_categories = item.getId();

in

 id_categories = item.getId();

并在MainActivity类中创建一个函数

public int getIdcategories(){
    return id_categories;
}

答案 2 :(得分:2)

将其置于主要活动的意图中,如此

itent.putExtra("id", item.getId());

并在列表食物中检索它

int id=getIntent().getIntExtra("id",0); // 0 is default value if id is not found in intent

答案 3 :(得分:2)

在onCreate之前在id_categories中将public static int id_categories声明为MainActivity。然后,您可以在MainActivity.id_categories的任何课程中访问它。