简单的android studio调查问卷
时间: 2023-11-30 16:43:14 浏览: 168
android studio实现简单考试应用程序
5星 · 资源好评率100%
以下是一个简单的Android Studio调查问卷的实现方法:
1.首先,在布局文件中设计调查问卷的UI界面,可以使用LinearLayout、RelativeLayout等布局方式,添加TextView、EditText、CheckBox、RadioButton等控件,设置好它们的属性。
2.在代码文件中,定义一个按钮的点击事件,当用户点击提交按钮时,获取用户输入的调查问卷信息,并将其保存到数据库中。
3.在代码文件中,使用SQLiteOpenHelper类创建一个数据库,定义一个表来存储调查问卷信息。
4.在代码文件中,定义一个数据模型类,用于封装调查问卷信息。
5.在代码文件中,定义一个数据访问类,用于对数据库进行增删改查操作。
6.在代码文件中,定义一个适配器类,用于将数据模型类中的数据绑定到UI界面上。
以下是一个简单的实现代码:
```java
// 布局文件中的控件
EditText etName, etAge, etGender;
CheckBox cb1, cb2, cb3;
RadioButton rb1, rb2, rb3;
Button btnSubmit;
// 数据库操作类
DatabaseHelper dbHelper;
// 数据模型类
public class Survey {
public String name;
public int age;
public String gender;
public boolean cb1;
public boolean cb2;
public boolean cb3;
public String rb;
}
// 数据访问类
public class SurveyDAO {
SQLiteDatabase db;
public SurveyDAO(Context context) {
dbHelper = new DatabaseHelper(context);
db = dbHelper.getWritableDatabase();
}
public void addSurvey(Survey survey) {
ContentValues values = new ContentValues();
values.put("name", survey.name);
values.put("age", survey.age);
values.put("gender", survey.gender);
values.put("cb1", survey.cb1);
values.put("cb2", survey.cb2);
values.put("cb3", survey.cb3);
values.put("rb", survey.rb);
db.insert("survey", null, values);
}
public List<Survey> getAllSurvey() {
List<Survey> surveyList = new ArrayList<>();
Cursor cursor = db.query("survey", null, null, null, null, null, null);
if (cursor.moveToFirst()) {
do {
Survey survey = new Survey();
survey.name = cursor.getString(cursor.getColumnIndex("name"));
survey.age = cursor.getInt(cursor.getColumnIndex("age"));
survey.gender = cursor.getString(cursor.getColumnIndex("gender"));
survey.cb1 = cursor.getInt(cursor.getColumnIndex("cb1")) == 1;
survey.cb2 = cursor.getInt(cursor.getColumnIndex("cb2")) == 1;
survey.cb3 = cursor.getInt(cursor.getColumnIndex("cb3")) == 1;
survey.rb = cursor.getString(cursor.getColumnIndex("rb"));
surveyList.add(survey);
} while (cursor.moveToNext());
}
cursor.close();
return surveyList;
}
}
// 适配器类
public class SurveyAdapter extends BaseAdapter {
private List<Survey> surveyList;
private LayoutInflater inflater;
public SurveyAdapter(Context context, List<Survey> surveyList) {
this.surveyList = surveyList;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return surveyList.size();
}
@Override
public Object getItem(int position) {
return surveyList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_survey, null);
holder = new ViewHolder();
holder.tvName = convertView.findViewById(R.id.tv_name);
holder.tvAge = convertView.findViewById(R.id.tv_age);
holder.tvGender = convertView.findViewById(R.id.tv_gender);
holder.tvCb = convertView.findViewById(R.id.tv_cb);
holder.tvRb = convertView.findViewById(R.id.tv_rb);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Survey survey = surveyList.get(position);
holder.tvName.setText(survey.name);
holder.tvAge.setText(String.valueOf(survey.age));
holder.tvGender.setText(survey.gender);
holder.tvCb.setText(survey.cb1 + " " + survey.cb2 + " " + survey.cb3);
holder.tvRb.setText(survey.rb);
return convertView;
}
static class ViewHolder {
TextView tvName;
TextView tvAge;
TextView tvGender;
TextView tvCb;
TextView tvRb;
}
}
// 布局文件中的控件初始化
etName = findViewById(R.id.et_name);
etAge = findViewById(R.id.et_age);
etGender = findViewById(R.id.et_gender);
cb1 = findViewById(R.id.cb1);
cb2 = findViewById(R.id.cb2);
cb3 = findViewById(R.id.cb3);
rb1 = findViewById(R.id.rb1);
rb2 = findViewById(R.id.rb2);
rb3 = findViewById(R.id.rb3);
btnSubmit = findViewById(R.id.btn_submit);
// 数据库操作类初始化
dbHelper = new DatabaseHelper(this);
db = dbHelper.getWritableDatabase();
// 提交按钮的点击事件
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Survey survey = new Survey();
survey.name = etName.getText().toString();
survey.age = Integer.parseInt(etAge.getText().toString());
survey.gender = etGender.getText().toString();
survey.cb1 = cb1.isChecked();
survey.cb2 = cb2.isChecked();
survey.cb3 = cb3.isChecked();
if (rb1.isChecked()) {
survey.rb = rb1.getText().toString();
} else if (rb2.isChecked()) {
survey.rb = rb2.getText().toString();
} else if (rb3.isChecked()) {
survey.rb = rb3.getText().toString();
}
SurveyDAO surveyDAO = new SurveyDAO(MainActivity.this);
surveyDAO.addSurvey(survey);
Toast.makeText(MainActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
}
});
// 显示调查问卷列表
SurveyDAO surveyDAO = new SurveyDAO(MainActivity.this);
List<Survey> surveyList = surveyDAO.getAllSurvey();
SurveyAdapter surveyAdapter = new SurveyAdapter(MainActivity.this, surveyList);
ListView listView = findViewById(R.id.list_view);
listView.setAdapter(surveyAdapter);
```
阅读全文