public class InfoActivity extends AppCompatActivity {
private EditText topic;
private Spinner question_type;
private EditText describe;
private EditText question;
private EditText one;
private EditText two;
private EditText three;
private EditText four;
private Button submit;
private ListView listview;
private List<Answer> mAnswers;
private JSONArray jsonArrayPatientInfo;//JSONObject对象,处理一个集合或者数组
private JSONObject object; //JSONObject对象,处理一个一个的对象
private JSONObject objPatientInfo;
private String jsonString; //保存带集合的json字符串
private TextView content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
initView();
}
private void initView() {
topic = (EditText) findViewById(R.id.topic);
question_type = (Spinner) findViewById(R.id.question_type);
describe = (EditText) findViewById(R.id.describe);
question = (EditText) findViewById(R.id.question);
one = (EditText) findViewById(R.id.one);
two = (EditText) findViewById(R.id.two);
three = (EditText) findViewById(R.id.three);
four = (EditText) findViewById(R.id.four);
submit = (Button) findViewById(R.id.submit);
listview = (ListView) findViewById(R.id.listview);
content = (TextView) findViewById(R.id.content);
object = new JSONObject();
final String[] arr = {"单选", "多选", "文本"};
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, arr);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
question_type.setAdapter(adapter);
question_type.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> pAdapterView, View pView, int pI, long pL) {
String s = arr[pI];
if (s.equals("单选")) {
initSingle();
} else if (s.equals("多选")) {
} else if (s.equals("文本")) {
}
}
@Override
public void onNothingSelected(AdapterView<?> pAdapterView) {
}
});
}
private void initSingle() {
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View pView) {
QuestionAdapter questionAdapter = new QuestionAdapter(InfoActivity.this);
mAnswers = new ArrayList<>();
Answer a_c = new Answer(one.getText().toString(), two.getText().toString(), three.getText().toString(), four.getText().toString(), question.getText().toString());
InfoDatabase database = new InfoDatabase(InfoActivity.this);
database.insert(a_c);
mAnswers = database.select();
Log.d("凉城info", a_c.toString());
questionAdapter.setAnswers(mAnswers);
listview.setAdapter(questionAdapter);
questionAdapter.notifyDataSetChanged();
try {
// object.put("AnswersData", jsonArrayPatientInfo);
object.put("topic", topic.getText().toString()); //这里还可以加入数据,这样json型字符串,就既有集合,又有普通数据
object.put("describe", describe.getText().toString()); //这里还可以加入数据,这样json型字符串,就既有集合,又有普通数据
} catch (JSONException e) {
e.printStackTrace();
}
changeArrayDateToJson();
}
});
}
//把一个集合转换成json格式的字符串
private void changeArrayDateToJson() {
// jsonArrayPatientInfo = null;
// object = null;
jsonArrayPatientInfo = new JSONArray();
//遍历上面初始化的集合数据,把数据加入JSONObject里面
for (int i = 0; i < mAnswers.size(); i++) {
//一个user对象,使用一个JSONObject对象来装
objPatientInfo = new JSONObject();
try {
//从集合取出数据,放入JSONObject里面 JSONObject对象和map差不多用法,以键和值形式存储数据
objPatientInfo.put("answer_content", mAnswers.get(i).getAnswer_content());
objPatientInfo.put("answer_content1", mAnswers.get(i).getAnswer_content1());
objPatientInfo.put("answer_content2", mAnswers.get(i).getAnswer_content2());
objPatientInfo.put("answer_content3", mAnswers.get(i).getAnswer_content3());
objPatientInfo.put("question", mAnswers.get(i).getQuestion());
//把JSONObject对象装入jsonArray数组里面
jsonArrayPatientInfo.put(objPatientInfo);
} catch (JSONException e) {
e.printStackTrace();
}
}
try {
//再把JSONArray数据加入JSONObject对象里面(数组也是对象)
object.put("AnswersData", jsonArrayPatientInfo);
// object.put("topic", topic.getText().toString()); //这里还可以加入数据,这样json型字符串,就既有集合,又有普通数据
// object.put("describe", describe.getText().toString()); //这里还可以加入数据,这样json型字符串,就既有集合,又有普通数据
} catch (JSONException e) {
e.printStackTrace();
}
// jsonString = null;
//把JSONObject转换成json格式的字符串
jsonString = object.toString();
content.setText(jsonString);
// submit.setVisibility(View.GONE);
Log.i("输出json字符串", "转换成json字符串: " + jsonString);
}
}
xml布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".question.InfoActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="问卷主题"
android:gravity="center"
android:layout_marginTop="30dp"
android:layout_marginLeft="10dp"
/>
<EditText
android:id="@+id/topic"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="80dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="问卷题型"
android:gravity="center"
android:layout_marginTop="80dp"
android:layout_marginLeft="10dp"
/>
<Spinner
android:id="@+id/question_type"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="70dp"
android:layout_marginLeft="80dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="问卷描述"
android:gravity="center"
android:layout_marginTop="130dp"
android:layout_marginLeft="10dp"
/>
<EditText
android:id="@+id/describe"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="110dp"
android:layout_marginLeft="80dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_below="@id/describe">
<EditText
android:id="@+id/question"
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint="请输入问题"
android:layout_gravity="center_vertical"/>
<TextView
android:id="@+id/tv_question"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:visibility="gone"/>
<LinearLayout
android:id="@+id/ll_answer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/one"
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint="答案一" />
<EditText
android:id="@+id/two"
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint="答案二" />
<EditText
android:id="@+id/three"
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint="答案三" />
<EditText
android:id="@+id/four"
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint="答案四" />
</LinearLayout>
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="提交" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/content"
/>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
封装类:
public class Answer {
private String answerId;
private String answer_content;
private String answer_content1;
private String answer_content2;
private String answer_content3;
private String question;
public String getQuestion() {
return question;
}
public void setQuestion(String pQuestion) {
question = pQuestion;
}
public String getAnswer_content1() {
return answer_content1;
}
public void setAnswer_content1(String pAnswer_content1) {
answer_content1 = pAnswer_content1;
}
public String getAnswer_content2() {
return answer_content2;
}
public void setAnswer_content2(String pAnswer_content2) {
answer_content2 = pAnswer_content2;
}
public String getAnswer_content3() {
return answer_content3;
}
public void setAnswer_content3(String pAnswer_content3) {
answer_content3 = pAnswer_content3;
}
private int ans_state;
public int getAns_state() {
return ans_state;
}
public void setAns_state(int ans_state) {
this.ans_state = ans_state;
}
public String getAnswerId() {
return answerId;
}
public void setAnswerId(String answerId) {
this.answerId = answerId;
}
public String getAnswer_content() {
return answer_content;
}
public void setAnswer_content(String answer_content) {
this.answer_content = answer_content;
}
public Answer(String pAnswer_content, String pAnswer_content1, String pAnswer_content2, String pAnswer_content3, String pQuestion) {
answer_content = pAnswer_content;
answer_content1 = pAnswer_content1;
answer_content2 = pAnswer_content2;
answer_content3 = pAnswer_content3;
question = pQuestion;
}
@Override
public String toString() {
return "Answer{" +
"answerId='" + answerId + '\'' +
", answer_content='" + answer_content + '\'' +
", answer_content1='" + answer_content1 + '\'' +
", answer_content2='" + answer_content2 + '\'' +
", answer_content3='" + answer_content3 + '\'' +
", ans_state=" + ans_state +
'}';
}
}