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

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

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&lt;?&gt; 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&lt;CityScreenItem&gt; city = new ArrayList&lt;&gt;();
switch (position) {
case 0:
city.add(new CityScreenItem(&quot;Giza&quot;, &quot;Giza is good&quot;,
R.drawable.dubai));
break;
case 1:
city.add(new CityScreenItem(&quot;Dubai&quot;, &quot;Dubai is good&quot;,
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;

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:

确定