【问题标题】:Android- Saving entries in a checklist as a file to be viewed laterAndroid-将清单中的条目保存为文件以供稍后查看
【发布时间】:2012-06-26 13:56:35
【问题描述】:

我创建了一个包含多个活动的应用程序,每个活动都包含一个复选框列表,每个复选框在检查时都包含一个特定的内容。该应用程序通过单击按钮获取布局视图,并添加复选框的值并显示一个值。我想知道是否有一种方法可以在单击按钮后将清单的状态保存为文件,以便以后查看。该文件应显示清单并显示已选中或未选中的框。我已经尝试将其保存为图像,但由于列表包含在 ScrollView 中并且我的手机屏幕非常小,因此仅捕获了屏幕上可见的列表部分。我希望最终能够在计算机上打开文件并将其转换为 excel 电子表格,但这不必在代码中。

这是我的活动:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;

public class InputsActivity extends Activity {
     public void onCreate(Bundle savedInstanceState {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.inputs);
     }

     public void DisplayScore(View view){
          float score= (float) Math.round(CalculateScore(view)*10)/10;  
          Toast.makeText(InputsActivity.this,"Input score is " +score+"%.",Toast.LENGTH_SHORT).show();
     }

     private double CalculateScore(View view) {
          double percent=  (AddChecks(view) + 2)*100/12;
          return percent;
     }

     private double AddChecks(View view) {
          double sum=0;
          for(int i=1; i<=10; i++) {
               String checkId="value" + i;
               int resID= getResources().getIdentifier(checkId,"id","com.example.ARTHChekclist");
               CheckBox check= (CheckBox) findViewById(resID);
               if (check.isChecked()) {
                     sum++;
               }
          }
          return sum;
     }
}

这是我的 xml 布局

?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         android:orientation="vertical" >

        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/checkBox2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/checkBox3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/checkBox4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/checkBox5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/checkBox6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/value1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/checkBox8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/checkBox9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/checkBox10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/checkBox11"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/checkBox12"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/checkBox13"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/checkBox14"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/checkBox15"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/checkBox16"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/value7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/checkBox18"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/checkBox19"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/value2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/checkBox22"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/checkBox23"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/value3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/value4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/checkBox26"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/checkBox27"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/checkBox28"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/checkBox29"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/checkBox30"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/checkBox31"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/value5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/value6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/checkBox34"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
          <CheckBox
            android:id="@+id/checkBox35"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
          <CheckBox
            android:id="@+id/value8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/value9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />

         <CheckBox
            android:id="@+id/checkBox36"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/checkBox37"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/checkBox38"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/value10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />
         <CheckBox
            android:id="@+id/checkBox40"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkbox1" />

         <Button
             android:id="@+id/button1"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="Submit"
             android:onClick="DisplayScore" />

    </LinearLayout>
</ScrollView>

谢谢!

【问题讨论】:

    标签: android list checkbox save scrollview


    【解决方案1】:

    您可以使用 SharedPreferences 将数据保存到文件。 http://developer.android.com/reference/android/content/SharedPreferences.html

    该文件本身是一个 XML,您可以在以后轻松查看它,甚至可以转换为您想要的任何内容。

    【讨论】:

    • 感谢您的回答!使用 SQlite 数据库并将响应保存在表格中会更容易吗?
    • 如果您有未知数量的复选框 - DB 会更好,因为您可以轻松地遍历包含所有值的表。但是,如果您有固定数量的项目,那么只使用 SharedPreferences 可能会更容易,避免了创建数据库并与之交互的整个过程。
    【解决方案2】:

    我肯定会像 Udnic 建议的那样使用共享首选项。像这样的东西应该可以工作......

    SharedPreferences storedPrefs = PreferenceManager
                    .getDefaultSharedPreferences(getActivity().getApplicationContext());
    
            SharedPreferences.Editor editor = storedPrefs.edit();
    
            editor.putBoolean("YOUR_TAG", true);
    
            editor.commit(); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-11
      • 1970-01-01
      • 2015-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多