[英]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.