如何在没有数据库的情况下将调查结果保存到texfile中?

时间:2016-02-10 14:27:09

标签: android

我尝试使用此调查示例https://github.com/AndreiD/surveylib,但如何将调查结果保存到texfile中?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Nothing fancy here. Plain old simple buttons....

    Button button_survey_example_1 = (Button) findViewById(R.id.button_survey_example_1);
    Button button_survey_example_2 = (Button) findViewById(R.id.button_survey_example_2);
    Button button_survey_example_3 = (Button) findViewById(R.id.button_survey_example_3);

    button_survey_example_1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent i_survey = new Intent(MainActivity.this, SurveyActivity.class);
            //you have to pass as an extra the json string.
            i_survey.putExtra("json_survey", loadSurveyJson("example_survey_1.json"));
            startActivityForResult(i_survey, SURVEY_REQUEST);
        }
    });

    button_survey_example_2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i_survey = new Intent(MainActivity.this, SurveyActivity.class);
            i_survey.putExtra("json_survey", loadSurveyJson("example_survey_2.json"));
            startActivityForResult(i_survey, SURVEY_REQUEST);
        }
    });

    button_survey_example_3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i_survey = new Intent(MainActivity.this, SurveyActivity.class);
            i_survey.putExtra("json_survey", loadSurveyJson("example_survey_3.json"));
            startActivityForResult(i_survey, SURVEY_REQUEST);
        }
    });

}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == SURVEY_REQUEST) {
        if (resultCode == RESULT_OK) {

            String answers_json = data.getExtras().getString("answers");
            Log.d("****", "****************** WE HAVE ANSWERS ******************");
            Log.v("ANSWERS JSON", answers_json);
            Log.d("****", "*****************************************************");

            //do whatever you want with them...
        }
    }
}


//json stored in the assets folder. but you can get it from wherever you like.
private String loadSurveyJson(String filename) {
    try {
        InputStream is = getAssets().open(filename);
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        return new String(buffer, "UTF-8");
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
}

}

是否可以添加一些内容来保存带有调查结果的文件,然后在"随意做任何事情......" ?它看起来像:)(是的,我知道谷歌的入门页面)

0 个答案:

没有答案