英文:
how to take int value from a class and use it in another one
问题
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
switch (position) {
case 0:
Intent i = new Intent(MainActivity.this, TabActivity.class);
startActivity(i);
Position = 0;
break;
case 1:
i = new Intent(MainActivity.this, TabActivity.class);
startActivity(i);
Position = 1;
break;
case 2:
i = new Intent(MainActivity.this, TabActivity.class);
startActivity(i);
Position = 2;
break;
case 3:
i = new Intent(MainActivity.this, TabActivity.class);
startActivity(i);
Position = 3;
break;
case 4:
i = new Intent(MainActivity.this, TabActivity.class);
startActivity(i);
Position = 4;
break;
}
}
});
and use it in a different Fragment class in a switch loop
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_city, container, false);
int position = new MainActivity().Position;
ArrayList<CityScreenItem> city = new ArrayList<>();
switch (position) {
case 0:
city.add(new CityScreenItem("Giza", "Giza is good",
R.drawable.dubai));
break;
case 1:
city.add(new CityScreenItem("Dubai", "Dubai is good",
R.drawable.egypt));
break;
}
CityScreenAdapter adapter = new CityScreenAdapter(getActivity(), city);
ListView listView = rootView.findViewById(R.id.list);
listView.setAdapter(adapter);
return rootView;
}
但是每次都显示第一个 case,经过检查发现是由于 int position = new MainActivity().Position;
这行代码导致的,但我找不到答案。
注意
我在类开始时确实声明了一个名为 Position 的 public int 变量。
英文:
I need to get the position of the grid view in the onItemClick
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
switch (position) {
case 0:
Intent i = new Intent(MainActivity.this, TabActivity.class);
startActivity(i);
Position = 0;
break;
case 1:
i = new Intent(MainActivity.this, TabActivity.class);
startActivity(i);
Position = 1;
break;
case 2:
i = new Intent(MainActivity.this, TabActivity.class);
startActivity(i);
Position = 2;
break;
case 3:
i = new Intent(MainActivity.this, TabActivity.class);
startActivity(i);
Position = 3;
break;
case 4:
i = new Intent(MainActivity.this, TabActivity.class);
startActivity(i);
Position = 4;
break;
}
}
});
and use it in a different Fragment class in a switch loop
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_city, container, false);
int position = new MainActivity().Position;
ArrayList<CityScreenItem> city = new ArrayList<>();
switch (position) {
case 0:
city.add(new CityScreenItem("Giza", "Giza is good",
R.drawable.dubai));
break;
case 1:
city.add(new CityScreenItem("Dubai", "Dubai is good",
R.drawable.egypt));
break;
}
CityScreenAdapter adapter = new CityScreenAdapter(getActivity(), city);
ListView listView = rootView.findViewById(R.id.list);
listView.setAdapter(adapter);
return rootView;
}
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
你每次都在创建一个新的对象:
int position = new MainActivity().Position;
这将会将位置重置为你所设置的值。将Position设置为静态。
public static int POSITION = 0;
这应该能解决你的问题。像下面这样访问它:
int position = MainActivity.POSITION;
英文:
You are creating a new object every time:
int position = new MainActivity().Position;
This will reset the position to what you are setting it to. Make Position static.
public static int POSITION = 0;
This should fix your problem. Access it like below:
int position = MainActivity.POSITION;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论