[英]I am creating an android app and I want multiple people to have access to the same number and be able to change that number with two buttons
我正在创建一个应用程序,人们可以在该应用程序中报告当周是否患有流感。 我已经有了代码,可以通过按按钮将确实患有流感的人数增加到没有流感的人数。 然后根据该数据创建一定比例的流感患者。 但是,每当我关闭应用程序时,所有数据都会消失。 使用该应用程序的其他人也将无法访问相同的数据。 这是该应用程序的代码。
public void fluButton()
{
Button hasFluButton = (Button)findViewById(R.id.fluButton);
hasFluButton.setOnClickListener(
new View.OnClickListener()
{
@Override
public void onClick(View v)
{
TextView t1 = (TextView)findViewById(R.id.influenzaPercent);
NumberFormat defaultFormat = NumberFormat.getPercentInstance();
defaultFormat.setMinimumFractionDigits(2);
numPeopleWFlu += 1;
percentFlu = ((double)numPeopleWFlu) / (numPeopleWOFlu + numPeopleWFlu);
String percent = defaultFormat.format(percentFlu);
t1.setText(percent + " of people have had the flu this week.");
}
}
);
}
public void noFluButton()
{
Button hasNoFluButton = (Button)findViewById(R.id.noFluButton);
hasNoFluButton.setOnClickListener(
new View.OnClickListener()
{
@Override
public void onClick(View v)
{
TextView t1 = (TextView)findViewById(R.id.influenzaPercent);
NumberFormat defaultFormat = NumberFormat.getPercentInstance();
defaultFormat.setMinimumFractionDigits(2);
numPeopleWOFlu += 1;
percentFlu = ((double)numPeopleWFlu) / (numPeopleWOFlu + numPeopleWFlu);
String percent = defaultFormat.format(percentFlu);
t1.setText(percent + " of people have had the flu in missouri this year.");
}
}
);
}`.
由于您的问题很广泛,此答案只会为您提供一个非常模糊的解决方案。
这是您需要做的事情,需要租用服务器并将所有数据放入该服务器。
为什么需要服务器? 因为您希望很多人同时访问数据。 如果您将数据保存到应用程序的SharedPreferences
或本地文件中,则其他人将无法获取它。
因此,现在您在应用程序中有了一个服务器,您可以在应用程序启动时从服务器检索数据。 那里有很多外部库可以帮助您从Internet上获取内容。
检索数据后,可以在某些视图和BOOM中显示它! 您做了第一部分。 第二部分是将数据保存到服务器。 当用户点击按钮或其他内容时,您将保存数据。
听起来很容易,对吧?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.