ADD file via upload

master
pybple5kv 3 years ago
parent 9466dfd6a1
commit 34994574e7

@ -0,0 +1,402 @@
package com.example.activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.example.Config;
import com.example.JsonParse;
import com.example.LoveDao;
import com.example.R;
import com.example.bean.JsonQuestBean;
import com.example.bean.QuestBean;
import com.example.fragment.AnswerFragment;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class AnswerActivity extends BaseActivity implements Chronometer.OnChronometerTickListener, View.OnClickListener {
private Chronometer chronometer;
private ViewPager vp_answer;
private ArrayList<Fragment> fragmentlists;
private int minute = 0;
private int second = 0;
private AlertDialog.Builder builder;
private ArrayList<String> a;
private Button btn_previous;
private Button btn_submit;
private Button btn_next;
private int nowpager = 0;
private List<QuestBean> messages;
private String type;
@Override
int getLayoutId() {
return R.layout.activity_answer;
}
@Override
void initView() {
chronometer = (Chronometer) findViewById(R.id._chro_exam);
vp_answer = (ViewPager) findViewById(R.id.vp_answer);
btn_previous = (Button) findViewById(R.id._btn_previous);
btn_submit = (Button) findViewById(R.id._btn_submit);
btn_next = (Button) findViewById(R.id._btn_next);
// 获取传递来的变量
// 联网获取数据
initNet();
btn_previous.setOnClickListener(this);
btn_submit.setOnClickListener(this);
btn_next.setOnClickListener(this);
vp_answer.setOnPageChangeListener(new MyOnPageChangeListener());
setChronometer();
}
/**
*
*/
private void setChronometer() {
chronometer.setText(nowtime());
chronometer.start();
chronometer.setOnChronometerTickListener(this);
}
/**
*
*
* @param chronometer
*/
@Override
public void onChronometerTick(Chronometer chronometer) {
second++;
if (second == 59) {
minute++;
second = 00;
}
}
/**
*
*
* @return
*/
private String nowtime() {
if (second < 10) {
return (minute + ":0" + second);
} else {
return (minute + ":" + second);
}
}
/*
*
*
* @param type
*/
private void initNet() {
a = new ArrayList<>();
fragmentlists = new ArrayList<>();
//进度条对话框
final ProgressDialog progressDialog = new ProgressDialog(AnswerActivity.this, R.style.AppTheme_Dark_Dialog);
progressDialog.setIndeterminate(true);
progressDialog.setMessage("获取题目中...");
progressDialog.show();
// 联网
RequestBody requestBody = new FormBody.Builder()
.build();
// OkHttpClient okHttp=new OkHttpClient();
// Request request=new Request
// .Builder()
// .url(Config.URL_GET_QUESTION)
// .build();
// Call call=okHttp.newCall(request);
//
okhttp3.Callback callback = new okhttp3.Callback(){
@Override
public void onFailure(Call call, IOException e) {
String responseData = "Error!";
Log.e("LoginActivity","Failure");
Log.e("LoginActivity",responseData);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String str=response.body().string();
Message msg=new Message();
msg.what=0;
msg.obj=str;
mhandler.sendMessage(msg);
}
};
progressDialog.dismiss();
/*OkGo.get(Config.URL_GET_QUESTION)
.params("type", type)
.execute(new StringCallback() {
@Override
public void onSuccess(String s, Call call, Response response) {
//gson解析
Gson gson = new Gson();
JsonQuestBean jsonQuestBean = gson.fromJson(s, JsonQuestBean.class);
messages = jsonQuestBean.getMessages();
for (int i = 0; i < messages.size(); i++) {
QuestBean questBeanQ = messages.get(i);
questBeanQ.setId(i);
fragmentlists.add(new AnswerFragment(questBeanQ));
LoveDao.insertLove(questBeanQ);
a.add(questBeanQ.getId() + "");
LogUtils.e(i + " onSuccess : " + questBeanQ.getId() + questBeanQ.getTitle());
}
// 设置适配器
vp_answer.setAdapter(new MainAdapter(getSupportFragmentManager()));
progressDialog.dismiss();
}
@Override
public void onError(Call call, Response response, Exception e) {
}
});*/
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(Config.URL_GET_QUESTION)
.post(requestBody)
.build();
//发送请求
client.newCall(request).enqueue(callback);
}
Handler mhandler=new Handler(){
@Override
public void dispatchMessage(@NonNull Message msg) {
super.dispatchMessage(msg);
switch (msg.what){
case 0:
if (msg.obj!=null){
String vlResult = (String) msg.obj;
Log.e("ShopActivity", vlResult);
//解析获取的JSON数据
Gson gson=new Gson();
//
// List<QuestBean> quests = new ArrayList<QuestBean>();
// for(int i = 0;i<10;i++){
// QuestBean q = new QuestBean();
// q.setId(Long.valueOf("id"));
// q.setQ_type("q_type");
// q.setTitle("title");
// q.setOptionA("optionA");
// q.setOptionB("optionB");
// q.setOptionC("optionC");
// q.setOptionD("optionD");
// quests.add(q);
// }
//
JsonQuestBean infoBean=gson.fromJson(vlResult,JsonQuestBean.class);//json文件解析错误
messages = infoBean.getMessages();
for (int i = 0; i < messages.size(); i++) {
QuestBean questBeanQ = messages.get(i);
questBeanQ.setId((long) i);
fragmentlists.add(new AnswerFragment(questBeanQ));
LoveDao.insertLove(questBeanQ);
a.add(questBeanQ.getId() + "");
}
// 设置适配器
vp_answer.setAdapter(new MainAdapter(getSupportFragmentManager()));
}
break;
}
}
};
// @Override
// void processClick(View v) {
// switch (v.getId()) {
//// 点击上一题按钮
// case R.id._btn_previous:
//// 如果是第一题,则谈吐司提醒,否则上移一道题
// if (nowpager == 0) {
// Toast.makeText(AnswerActivity.this, "已经到头啦!", Toast.LENGTH_SHORT).show();
// } else {
// vp_answer.setCurrentItem(--nowpager);
// }
// break;
//// 点击提交按钮
// case R.id._btn_submit:
//// 简答题不进行提交评分
// if (type.equals("3")) {
// Toast.makeText(this, "简答题目前暂不支持评分", Toast.LENGTH_SHORT).show();
// return;
// }
//// 否则初始化并展示提交对话框
// initAlertDialog();
// builder.show();
// break;
//// 点击下一题按钮
// case R.id._btn_next:
//// 如果是最后一题,则谈吐司提醒,否则下移一道题
// if (nowpager == fragmentlists.size()) {
// Toast.makeText(AnswerActivity.this, "已经是最后一题了!", Toast.LENGTH_SHORT).show();
// } else {
// vp_answer.setCurrentItem(++nowpager);
// }
// break;
// default:
// break;
// }
// }
@Override
public void onClick(View view) {
switch (view.getId()) {
// 点击上一题按钮
case R.id._btn_previous:
// 如果是第一题,则谈吐司提醒,否则上移一道题
if (nowpager == 0) {
Toast.makeText(AnswerActivity.this, "已经到头啦!", Toast.LENGTH_SHORT).show();
} else {
vp_answer.setCurrentItem(--nowpager);
}
break;
// 点击提交按钮
case R.id._btn_submit:
// 简答题不进行提交评分
if (type.equals("3")) {
Toast.makeText(this, "简答题目前暂不支持评分", Toast.LENGTH_SHORT).show();
return;
}
// 否则初始化并展示提交对话框
initAlertDialog();
builder.show();
break;
// 点击下一题按钮
case R.id._btn_next:
// 如果是最后一题,则谈吐司提醒,否则下移一道题
if (nowpager == fragmentlists.size()) {
Toast.makeText(AnswerActivity.this, "已经是最后一题了!", Toast.LENGTH_SHORT).show();
} else {
vp_answer.setCurrentItem(++nowpager);
}
break;
default:
break;
}
}
/**
* viewpager
*/
class MainAdapter extends FragmentPagerAdapter {
public MainAdapter(FragmentManager fm) {
super(fm);
}
//获取条目
@Override
public Fragment getItem(int position) {
return fragmentlists.get(position);
}
//数目
@Override
public int getCount() {
return fragmentlists.size();
}
}
// 弹出是否确认交卷的对话框
private void initAlertDialog() {
//新建对话框
builder = new AlertDialog.Builder(AnswerActivity.this);
builder.setTitle("提示");
builder.setMessage("是否确定交卷?");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 计算分数
int grade = 0;
// 判断题
// if (type.equals("2")) {
// for (int i = 0; i < messages.size(); i++) {
//// 查询
// QuestBean questBeenA = LoveDao.queryLove(Integer.toString(Integer.parseInt(a.get(i))));
//// 判断
// if (questBeenA.getAnswer().equals("对") && questBeenA.getMyanswer().equals("A") || questBeenA.getAnswer().equals("错") && questBeenA.getMyanswer().equals("B")) {
// grade += 20;
// }
// }
// }
//// 选择题
// else {
for (int i = 0; i < messages.size(); i++) {
QuestBean questBeenA = LoveDao.queryLove(Integer.toString(Integer.parseInt(a.get(i))));
if (questBeenA.getAnswer().equals(questBeenA.getMyanswer())) {
grade += 20;
}
}
//}
// 传递分数
Intent intent = new Intent(AnswerActivity.this, GradeActivity.class);
intent.putExtra("grade", "" + grade);
// 传递题目列表
intent.putStringArrayListExtra("timu", a);
startActivity(intent);
finish();
}
});
builder.setNegativeButton("取消", null);
}
private void parseJsonDate(String obj) {
}
/**
* viewpager
*/
private class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
nowpager = position;
}
@Override
public void onPageScrollStateChanged(int state) {
}
}
}
Loading…
Cancel
Save