怎样从一个类中获取整型值并在另一个类中使用它

huangapple go评论111阅读模式
英文:

how to take int value from a class and use it in another one

问题

  1. public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
  2. switch (position) {
  3. case 0:
  4. Intent i = new Intent(MainActivity.this, TabActivity.class);
  5. startActivity(i);
  6. Position = 0;
  7. break;
  8. case 1:
  9. i = new Intent(MainActivity.this, TabActivity.class);
  10. startActivity(i);
  11. Position = 1;
  12. break;
  13. case 2:
  14. i = new Intent(MainActivity.this, TabActivity.class);
  15. startActivity(i);
  16. Position = 2;
  17. break;
  18. case 3:
  19. i = new Intent(MainActivity.this, TabActivity.class);
  20. startActivity(i);
  21. Position = 3;
  22. break;
  23. case 4:
  24. i = new Intent(MainActivity.this, TabActivity.class);
  25. startActivity(i);
  26. Position = 4;
  27. break;
  28. }
  29. }
  30. });
  31. and use it in a different Fragment class in a switch loop
  32. @Override
  33. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  34. Bundle savedInstanceState) {
  35. View rootView = inflater.inflate(R.layout.activity_city, container, false);
  36. int position = new MainActivity().Position;
  37. ArrayList<CityScreenItem> city = new ArrayList<>();
  38. switch (position) {
  39. case 0:
  40. city.add(new CityScreenItem("Giza", "Giza is good",
  41. R.drawable.dubai));
  42. break;
  43. case 1:
  44. city.add(new CityScreenItem("Dubai", "Dubai is good",
  45. R.drawable.egypt));
  46. break;
  47. }
  48. CityScreenAdapter adapter = new CityScreenAdapter(getActivity(), city);
  49. ListView listView = rootView.findViewById(R.id.list);
  50. listView.setAdapter(adapter);
  51. return rootView;
  52. }

但是每次都显示第一个 case,经过检查发现是由于 int position = new MainActivity().Position; 这行代码导致的,但我找不到答案。
注意
我在类开始时确实声明了一个名为 Position 的 public int 变量。

英文:

I need to get the position of the grid view in the onItemClick

  1. public void onItemClick(AdapterView&lt;?&gt; parent, View v,
  2. int position, long id) {
  3. switch (position) {
  4. case 0:
  5. Intent i = new Intent(MainActivity.this, TabActivity.class);
  6. startActivity(i);
  7. Position = 0;
  8. break;
  9. case 1:
  10. i = new Intent(MainActivity.this, TabActivity.class);
  11. startActivity(i);
  12. Position = 1;
  13. break;
  14. case 2:
  15. i = new Intent(MainActivity.this, TabActivity.class);
  16. startActivity(i);
  17. Position = 2;
  18. break;
  19. case 3:
  20. i = new Intent(MainActivity.this, TabActivity.class);
  21. startActivity(i);
  22. Position = 3;
  23. break;
  24. case 4:
  25. i = new Intent(MainActivity.this, TabActivity.class);
  26. startActivity(i);
  27. Position = 4;
  28. break;
  29. }
  30. }
  31. });

and use it in a different Fragment class in a switch loop

  1. @Override
  2. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  3. Bundle savedInstanceState) {
  4. View rootView = inflater.inflate(R.layout.activity_city, container, false);
  5. int position = new MainActivity().Position;
  6. ArrayList&lt;CityScreenItem&gt; city = new ArrayList&lt;&gt;();
  7. switch (position) {
  8. case 0:
  9. city.add(new CityScreenItem(&quot;Giza&quot;, &quot;Giza is good&quot;,
  10. R.drawable.dubai));
  11. break;
  12. case 1:
  13. city.add(new CityScreenItem(&quot;Dubai&quot;, &quot;Dubai is good&quot;,
  14. R.drawable.egypt));
  15. break;
  16. }
  17. CityScreenAdapter adapter = new CityScreenAdapter(getActivity(), city);
  18. ListView listView = rootView.findViewById(R.id.list);
  19. listView.setAdapter(adapter);
  20. return rootView;
  21. }

but it always displays the first case when I check this was due to the int position = new MainActivity().Position;code but I could not find the answer.
Note
I did make a public int for Position at the beginning of the class

答案1

得分: 0

你每次都在创建一个新的对象:

  1. int position = new MainActivity().Position;

这将会将位置重置为你所设置的值。将Position设置为静态。

  1. public static int POSITION = 0;

这应该能解决你的问题。像下面这样访问它:

  1. int position = MainActivity.POSITION;
英文:

You are creating a new object every time:

  1. int position = new MainActivity().Position;

This will reset the position to what you are setting it to. Make Position static.

  1. public static int POSITION = 0;

This should fix your problem. Access it like below:

  1. int position = MainActivity.POSITION;

huangapple
  • 本文由 发表于 2020年7月26日 09:52:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63095349.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定