使用我的投票应用程序(android studio)的firebase保存和更新值



我一直在尝试更新我的应用程序的值,但似乎我错过了一些东西。所以我创建了一个投票应用程序,用户可以投票,但每次我点击后退按钮的值回到零。使用这个

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_second_app );
A10 = findViewById ( R.id.tvA10 );

public void A10clicked (View v){
String A10count =A10.getText ().toString ().trim ();
int count =Integer.parseInt ( A10count );
count++;
A10.setText ( String.valueOf ( count ) );
}

我想在我的应用程序中,当用户投票值必须保持不变,让我们说明天的值应该是零,意味着我想从firebase更新这些值。

注意-我在每10个活动字段中有10个textview,我很困惑该怎么做,我尝试了解决方案,但已经5天了,我什么都做不了。

///activity.java文件

public class second_app extends AppCompatActivity {
TextView A10;
TextView A11;
TextView A12;
TextView A13;
TextView A14;
TextView A15;
TextView A16;
TextView A17;
TextView A18;
TextView A19;
Button Abtn10;
Button Abtn11;
Button Abtn12;
Button Abtn13;
Button Abtn14;
Button Abtn15;
Button Abtn16;
Button Abtn17;
Button Abtn18;
Button Abtn19;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_second_app );
A10 = findViewById ( R.id.tvA10 );
A11 = findViewById ( R.id.tvA11 );
A12 = findViewById ( R.id.tvA12 );
A13 = findViewById ( R.id.tvA13 );
A14 = findViewById ( R.id.tvA14 );
A15 = findViewById ( R.id.tvA15);
A16 = findViewById ( R.id.tvA16 );
A17 = findViewById ( R.id.tvA17 );
A18 = findViewById ( R.id.tvA18 );
A19 = findViewById ( R.id.tvA19 );
Abtn10 = findViewById ( R.id.btnA10 );
Abtn11 = findViewById ( R.id.btnA11 );
Abtn12 = findViewById ( R.id.btnA12 );
Abtn13 = findViewById ( R.id.btnA13 );
Abtn14 = findViewById ( R.id.btnA14 );
Abtn15 = findViewById ( R.id.btnA15 );
Abtn16 = findViewById ( R.id.btnA16 );
Abtn17 = findViewById ( R.id.btnA17 );
Abtn18 = findViewById ( R.id.btnA18 );
Abtn19 = findViewById ( R.id.btnA19 );


}

public void A10clicked (View v){
String A10count =A10.getText ().toString ().trim ();
int count =Integer.parseInt ( A10count );
count++;
A10.setText ( String.valueOf ( count ) );
}
public void A11clicked (View v){
String A11count =A11.getText ().toString ().trim ();
int count =Integer.parseInt ( A11count );
count++;
A11.setText ( String.valueOf ( count ) );
}
public void A12clicked (View v){
String A12count =A12.getText ().toString ().trim ();
int count =Integer.parseInt ( A12count );
count++;
A12.setText ( String.valueOf ( count ) );
}
public void A13clicked (View v){
String A13count =A13.getText ().toString ().trim ();
int count =Integer.parseInt ( A13count );
count++;
A13.setText ( String.valueOf ( count ) );
}
public void A14clicked (View v){
String A14count =A14.getText ().toString ().trim ();
int count =Integer.parseInt ( A14count );
count++;
A14.setText ( String.valueOf ( count ) );
}
public void A15clicked (View v){
String A15count =A15.getText ().toString ().trim ();
int count =Integer.parseInt ( A15count );
count++;
A15.setText ( String.valueOf ( count ) );
}
public void A16clicked (View v){
String A16count =A16.getText ().toString ().trim ();
int count =Integer.parseInt ( A16count );
count++;
A16.setText ( String.valueOf ( count ) );
}
public void A17clicked (View v){
String A17count =A17.getText ().toString ().trim ();
int count =Integer.parseInt ( A17count );
count++;
A17.setText ( String.valueOf ( count ) );
}
public void A18clicked (View v){
String A18count =A18.getText ().toString ().trim ();
int count =Integer.parseInt ( A18count );
count++;
A18.setText ( String.valueOf ( count ) );
}

public void A19clicked (View v){
String A19count =A19.getText ().toString ().trim ();
int count =Integer.parseInt ( A19count );
count++;
A19.setText ( String.valueOf ( count ) );
}

}

////XML文件

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".second_app">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:layout_width="200dp"
android:id="@+id/tvA10"
android:gravity="center"
android:layout_height="90dp"
android:layout_marginStart="75dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="80dp"
android:text="0"
android:background="@drawable/yellow_border"
android:textSize="50sp"
android:textColor="@color/black"
/>
<Button
android:layout_width="90dp"
android:id="@+id/btnA10"
android:layout_height="100dp"
android:onClick="A10clicked"
android:gravity="center"
android:layout_marginLeft="133dp"
android:layout_marginTop="180dp"
android:text="10"
android:textSize="30sp"/>
<TextView
android:layout_width="200dp"
android:layout_height="90dp"
android:id="@+id/tvA11"
android:background="@drawable/yellow_border"
android:text="0"
android:textSize="50sp"
android:textColor="@color/black"
android:gravity="center"
android:layout_marginRight="40dp"
android:layout_marginStart="75dp"
android:layout_marginTop="300dp"
android:layout_marginLeft="75dp" />
<Button
android:layout_width="90dp"
android:id="@+id/btnA11"
android:layout_height="100dp"
android:onClick="A11clicked"
android:gravity="center"
android:layout_marginLeft="133dp"
android:layout_marginTop="400dp"
android:text="11"
android:textSize="30sp"/>
<TextView
android:layout_width="200dp"
android:id="@+id/tvA12"
android:background="@drawable/yellow_border"
android:layout_height="90dp"
android:layout_marginStart="75dp"
android:layout_marginLeft="70dp"
android:layout_marginTop="510dp"
android:layout_marginRight="50dp"
android:gravity="center"
android:text="0"
android:textColor="@color/black"
android:textSize="50sp" />
<Button
android:layout_width="90dp"
android:id="@+id/btnA12"
android:layout_height="100dp"
android:onClick="A12clicked"
android:gravity="center"
android:layout_marginLeft="133dp"
android:layout_marginTop="610dp"
android:text="12"
android:textSize="30sp"/>
<TextView
android:layout_width="200dp"
android:id="@+id/tvA13"
android:background="@drawable/yellow_border"
android:layout_height="90dp"
android:gravity="center"
android:layout_marginLeft="75dp"
android:layout_marginTop="730dp"
android:text="0"
android:textSize="50sp"
android:textColor="@color/black"/>
<Button
android:layout_width="90dp"
android:id="@+id/btnA13"
android:onClick="A13clicked"
android:layout_height="100dp"
android:gravity="center"
android:text="13"
android:textSize="30sp"
android:layout_marginLeft="133dp"
android:layout_marginTop="830dp"/>
<TextView
android:layout_width="200dp"
android:id="@+id/tvA14"
android:background="@drawable/yellow_border"
android:layout_height="90dp"
android:gravity="center"
android:text="0"
android:textSize="50sp"
android:textColor="@color/black"
android:layout_marginLeft="75dp"
android:layout_marginTop="950dp"/>
<Button
android:layout_width="90dp"
android:id="@+id/btnA14"
android:layout_height="100dp"
android:gravity="center"
android:onClick="A14clicked"
android:text="14"
android:textSize="30sp"
android:layout_marginLeft="133dp"
android:layout_marginTop="1050dp"/>

<TextView
android:layout_width="200dp"
android:id="@+id/tvA15"
android:layout_height="90dp"
android:background="@drawable/yellow_border"
android:gravity="center"
android:text="0"
android:textSize="50sp"
android:textColor="@color/black"
android:layout_marginLeft="75dp"
android:layout_marginTop="1170dp"/>
<Button
android:layout_width="90dp"
android:layout_height="100dp"
android:id="@+id/btnA15"
android:onClick="A15clicked"
android:gravity="center"
android:text="15"
android:textSize="30sp"
android:layout_marginLeft="133dp"
android:layout_marginTop="1260dp"/>
<TextView
android:layout_width="200dp"
android:layout_height="90dp"
android:id="@+id/tvA16"
android:background="@drawable/yellow_border"
android:gravity="center"
android:text="0"
android:textSize="50sp"
android:textColor="@color/black"
android:layout_marginLeft="75dp"
android:layout_marginTop="1370dp"/>
<Button
android:layout_width="90dp"
android:id="@+id/btnA16"
android:layout_height="100dp"
android:gravity="center"
android:text="16"
android:onClick="A16clicked"
android:textSize="30sp"
android:layout_marginLeft="133dp"
android:layout_marginTop="1470dp"/>
<TextView
android:layout_width="200dp"
android:layout_height="90dp"
android:id="@+id/tvA17"
android:background="@drawable/yellow_border"
android:gravity="center"
android:text="0"
android:textSize="50sp"
android:textColor="@color/black"
android:layout_marginLeft="75dp"
android:layout_marginTop="1590dp"/>
<Button
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/btnA17"
android:gravity="center"
android:text="17"
android:onClick="A17clicked"
android:textSize="30sp"
android:layout_marginLeft="133dp"
android:layout_marginTop="1700dp"/>
<TextView
android:layout_width="200dp"
android:layout_height="90dp"
android:id="@+id/tvA18"
android:background="@drawable/yellow_border"
android:gravity="center"
android:text="0"
android:textSize="50sp"
android:textColor="@color/black"
android:layout_marginLeft="75dp"
android:layout_marginTop="1810dp"/>
<Button
android:layout_width="90dp"
android:layout_height="100dp"
android:id="@+id/btnA18"
android:gravity="center"
android:text="18"
android:onClick="A18clicked"
android:textSize="30sp"
android:layout_marginLeft="133dp"
android:layout_marginTop="1920dp"/>
<TextView
android:layout_width="200dp"
android:id="@+id/tvA19"
android:layout_height="90dp"
android:background="@drawable/yellow_border"
android:gravity="center"
android:text="0"
android:textSize="50sp"
android:textColor="@color/black"
android:layout_marginLeft="75dp"
android:layout_marginTop="2050dp"/>
<Button
android:layout_width="90dp"
android:layout_height="100dp"
android:id="@+id/btnA19"
android:gravity="center"
android:text="19"
android:textSize="30sp"
android:onClick="A19clicked"
android:layout_marginLeft="133dp"
android:layout_marginTop="2160dp"/>


</RelativeLayout>
我愿意听到你的任何消息。这个项目是我个人的兴趣,因为我在没有任何编码知识的情况下工作过。非常感谢你们。数据库结构是空的,因为我没有任何具体的想法,我应该做什么,因此我留下它为空。

如果您想将投票保存在单个设备上。当用户下次打开它时,之前的投票就会自动加载。这可以使用保存的实例来实现。你可以保存活动的实例,只要用户backpress即关闭应用程序。如果用户再次打开活动,那么你可以重新加载数据。例如,如果您想在用户后退时立即将值保存在文本字段A10中,那么您可以这样做。

@Override
public void onBackPressed()
{
PreferenceManager.getDefaultSharedPreferences(getContext()).edit().putString("value", A10.getString()).apply();
super.onBackPressed();  // optional depending on your needs
}

之后,如果用户再次打开应用程序,那么你可以使用

检索以前的值
Context context=this.getApplicationContext();
SharedPreferences settings=context.getSharedPreferences("PREFERENCES", 0);
String value=settings.getString("value", "");
public class MainActivity extends AppCompatActivity {
TextView A10;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
A10 = findViewById ( R.id.tvA10 );
update();
@Override
public void onBackPressed() {

PreferenceManager.getDefaultSharedPreferences(getContext()).edit().putString("value", A10.getString()).apply();
super.onBackPressed();  
}
}
private void update(){
Context context=this.getApplicationContext();
SharedPreferences settings=context.getSharedPreferences("PREFERENCES", 0);
String value=settings.getString("value", "");
if(value!="")A10.setText(value);
}

}

你可以从这里做一个演示

最新更新