在android开发中,当我使用
时,pointView是一个TextViewfinal CommunityFragment CommunityFragment = new CommunityFragment();
pointView.setText(String.valueOf(CommunityFragment.points));
它在社区片段类中带来了整数的整数值。更改该值后,如何获取更新值? 下面的完整代码:
class TotalPointsMenu extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.total_points_menu);
// TODO Auto-generated method stub
final CommunityFragment CommunityFragment = new CommunityFragment();
TextView rankView = (TextView) findViewById(R.id.Ranks);
final TextView pointView = (TextView) findViewById(R.id.Points);
Button refreshButton = (Button) findViewById(R.id.refreshButton);
pointView.setText(String.valueOf(CommunityFragment.points));
refreshButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
pointView.setText(String.valueOf(CommunityFragment.points));
Toast.makeText(getApplicationContext(), String.valueOf(CommunityFragment.points), Toast.LENGTH_LONG).show();
}
});
int points = CommunityFragment.points;
pointView.setText(String.valueOf(points));
if(points >= 20 && points < 45){
rankView.setText(R.string.pos2);
}
if(points >= 45 && points < 65){
rankView.setText(R.string.pos3);
}
if(points >= 65 && points < 80){
rankView.setText(R.string.pos4);
}
if(points >= 80 && points < 110){
rankView.setText(R.string.pos5);
}
if(points >= 110){
rankView.setText(R.string.pos6);
}
}
}
CommunityFragment:
class CommunityFragment extends Fragment{
public int points = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.community_fragment, container, false);
Button Tgiven = (Button) view.findViewById(R.id.Tgiven);
Tgiven.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
points += 20;
Toast.makeText(getActivity(), "You gained 20 points!", Toast.LENGTH_LONG).show();
}
});
Button fbPage = (Button) view.findViewById(R.id.fbPage);
fbPage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent openURL = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/pages/FBLA-Intro-to-It/294112347403691"));
startActivity(openURL);
}
});
Button Qgiven = (Button) view.findViewById(R.id.Qgiven);
Qgiven.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
points += 10;
Toast.makeText(getActivity(), "You gained 10 points!", Toast.LENGTH_LONG).show();
}
});
Button Qhelp = (Button) view.findViewById(R.id.Qhelp);
Qhelp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
points += 5;
Toast.makeText(getActivity(), "You gained 5 points!", Toast.LENGTH_LONG).show();
Intent openURL = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/pages/FBLA-Intro-to-It/294112347403691"));
startActivity(openURL);
}
});
Button QhelpedEnter = (Button) view.findViewById(R.id.QhelpedEnter);
final TextView Qhelped = (TextView) view.findViewById(R.id.Qhelped);
QhelpedEnter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
points += 5;
Toast.makeText(getActivity(), "You gained 5 points!", Toast.LENGTH_LONG).show();
Qhelped.setText("");
}
});
Button Thelp = (Button) view.findViewById(R.id.Thelp);
Thelp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
points += 10;
Toast.makeText(getActivity(), "You gained 10 points!", Toast.LENGTH_LONG).show();
Intent openURL = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/pages/FBLA-Intro-to-It/294112347403691"));
startActivity(openURL);
}
});
return view;
}
}
MainFragment(转到活动适配器)
class MainFragment extends FragmentActivity{
ViewPager ViewPager = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager= (ViewPager) findViewById(R.id.pager);
FragmentManager fragmentManager=getSupportFragmentManager();
ViewPager.setAdapter(new AdapterActivity(fragmentManager));
}
活动适配器(然后就像社区一样有家庭和资源片段,但它们是分开的)
public class AdapterActivity extends FragmentStatePagerAdapter{
public AdapterActivity(FragmentManager fm){
super(fm);
}
@Override
public Fragment getItem(int i){
Fragment fragment=null;
switch(i){
case 0:
fragment=new HomeFragment();
break;
case 1:
fragment=new CommunityFragment();
break;
case 2:
fragment=new ResourcesFragment();
break;
}
return fragment;
}
@Override
public int getCount(){
return 3;
}
@Override
public CharSequence getPageTitle(int position){
switch(position){
case 0:
return "Home Page";
case 1:
return "Community";
case 2:
return "Resources";
}
return null;
}
}
答案 0 :(得分:0)
首先,我建议您更改CommunityFragment
变量的名称。
final CommunityFragment CommunityFragment = new CommunityFragment();
将变量命名为类名是一个坏习惯。 (这就是我要求发布其余代码的原因,因为如果您的point
被声明为static
,则可能会发生冲突。
我看到的问题是CommunityFragment
变量是指您使用new
运算符创建的实例,但它永远不会添加到FragmentManager
的屏幕中。我认为您在屏幕上看到的片段是{strong> CommunityFragment
类的另一个实例,可能是在扩充XML时创建的。
此外,在rankView
视图中设置文字的代码仅出现在onCreate()
上,因此您的rankView
将不会更新。只要确保你知道这一点。