[英]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
I am creating an app where people report whether or not they have the flu that week. 我正在创建一个应用程序,人们可以在该应用程序中报告当周是否患有流感。 I already have the code that allows me to be able to add to the number of people who do have the flu and the number of people who don't have the flu by pressing buttons. 我已经有了代码,可以通过按按钮将确实患有流感的人数增加到没有流感的人数。 It then creates a percentage of people who have the flu based on that data. 然后根据该数据创建一定比例的流感患者。 But whenever I close out of the app, all of the data goes away. 但是,每当我关闭应用程序时,所有数据都会消失。 The same data also won't be able to be accessed by the other people with the app. 使用该应用程序的其他人也将无法访问相同的数据。 Here is the code for the app. 这是该应用程序的代码。
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.");
}
}
);
}`.
This answer will only provide you with a very vague solution as your question is broad. 由于您的问题很广泛,此答案只会为您提供一个非常模糊的解决方案。
Here's what you need to do, you need to rent a server and put all the data to that server. 这是您需要做的事情,需要租用服务器并将所有数据放入该服务器。
Why do you need a server? 为什么需要服务器? Because you want lots of people to access the data as the same time. 因为您希望很多人同时访问数据。 If you save the data to your app's SharedPreferences
or something local, other people won't be able to get it. 如果您将数据保存到应用程序的SharedPreferences
或本地文件中,则其他人将无法获取它。
So now you have a server, in your app, you retrieve the data from the server at the start of your app. 因此,现在您在应用程序中有了一个服务器,您可以在应用程序启动时从服务器检索数据。 There are lots of external libraries out there that can help you fetch something from the internet. 那里有很多外部库可以帮助您从Internet上获取内容。
After you retrieved the data, you display it in some views and BOOM! 检索数据后,可以在某些视图和BOOM中显示它! You did the first part. 您做了第一部分。 The second part is to save the data to the server. 第二部分是将数据保存到服务器。 When the user taps on a button or something, you save the data. 当用户点击按钮或其他内容时,您将保存数据。
Sounds easy, huh? 听起来很容易,对吧?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.