题库demo

前言
这是一个关于题库的demo
主要实现功能:题目展示,选择答案时答案的显示及判断对错,已答过题目的答案
记录等功能,基本实现了答题所需要的功能。
实现方案及lib:使用RecycleView 和 PagerSnapHelper 模拟ViewPager滑动效果,
使用本地数据库(greeDao3)作为数据源

先来两张截图
题库
选择答案显示

正文

一 引入lib

dependencies {
    compile 'com.android.support:appcompat-v7:25.+'
    compile 'com.android.support:design:25.+'
    compile 'org.greenrobot:greendao:3.1.0'
    compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.15'
}

二 greendao 使用在这里不说了,网上有很多。下面就直接上代码吧
1 题目布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp">
     <ImageButton
            android:id="@+id/iv_back"
            android:src="@mipmap/icon_back_black"
            android:background="@null"
            android:layout_centerVertical="true"
            android:layout_width="50dp"
            android:layout_height="40dp" />
        <TextView
            android:textSize="18sp"
            android:text="题库"
            android:layout_centerInParent="true"
            android:textColor="@color/black"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recy1"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>

        <ProgressBar
            android:id="@+id/loading_progress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:visibility="gone" />
    </RelativeLayout>
</LinearLayout>

2 答案布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/iv_answer"
        android:layout_marginTop="10dp"
        android:layout_marginRight="20dp"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        />
    <TextView
        android:id="@+id/tv_question"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="" />
    <android.support.v7.widget.RecyclerView
        android:layout_below="@+id/tv_question"
        android:id="@+id/recy"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </android.support.v7.widget.RecyclerView>
</RelativeLayout>

3 准备数据,设置RecycleView

//设置RecycleView 横向显示
LinearLayoutManager linearLayoutManager = new LinearLayoutManager
(QuestionActivity.this);
    linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
    recy1.setLayoutManager(linearLayoutManager);
//实现VeiwPager效果
    PagerSnapHelper snapHelper = new PagerSnapHelper();
    snapHelper.attachToRecyclerView(recy1);

//准备数据
new MyAsyncTask() {
        @Override
        public void preTask() {
            loadingProgressDb.setVisibility(View.VISIBLE);
        }

        @Override
        public void doInBack() {
            questionBeanDao.deleteAll();
            answerBeanDao.deleteAll();
            for (int i = 1; i < 101; i++) {
                long num = i;
                int type = 0;
                int curId = (int) num % 4;
                if (num % 2 == 0) {
                    type = 0;
                } else {
                    type = 1;
                }

                String question = i + ".我是但是对方开始都说了付款,就开
始的地方了开滦股份大连控股亏大发了看过老地方看过地方的空间疯狂的肌肤抵抗";
                int aId = i + 1;
                questionBeanDao.insert(new QuestionBean(type, num, 
question, aId, -1, curId));
                for (int k = 0; k < 4; k++) {
                    answerBeanDao.insert(new AnswerBean(aId, k, i + "我是答案" + k));
                }
            }
        }

        @Override
        public void postTask() {
            loadingProgressDb.setVisibility(View.GONE);
        }
    }.execute();
}

4 问题adapter

public class QuestionAdapter extends BaseQuickAdapter<QuestionBean, 
BaseViewHolder> {

    private ImageView iv_answer;
    private int currentPosition = -1;

    public QuestionAdapter(@Nullable List<QuestionBean> data) {
        super(R.layout.adapter_question, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, final QuestionBean item) {
        currentPosition = helper.getLayoutPosition();

        helper.setText(R.id.tv_question,item.getQuerstion());

        Log.e("dddddddd",item.getACurId()+ "///");

        iv_answer = helper.getView(R.id.iv_answer);
        RecyclerView recyclerView = helper.getView(R.id.recy);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(linearLayoutManager);

    List<AnswerBean> answerS = ((QuestionActivity) mContext).getAnswerS
(item.getAId());
    //查询学生答案,
    final int stuanswerId = ((QuestionActivity) mContext).quaryAnswer
(item.getAId());

    final AnswerAdapter answerAdapter = new AnswerAdapter(answerS);
    recyclerView.setAdapter(answerAdapter);
    //判断学生答案对错,这里是求余
    if (stuanswerId != -1) {
        answerAdapter.addFooterView(View.inflate(mContext, 
R.layout.adapter_answer_current, null));
        iv_answer.setVisibility(View.VISIBLE);
        if (stuanswerId/2==0){
            iv_answer.setImageResource(R.drawable.ic_check_black_24dp);
        }else {
            iv_answer.setImageResource(R.drawable.ic_clear_black_24dp);
        }
    } else {
        iv_answer.setVisibility(View.GONE);
    }
    //已选答案,设置选择的item
    answerAdapter.setSelectPosition(stuanswerId);

    answerAdapter.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(BaseQuickAdapter adapter, View view, int 
position) {
            if (stuanswerId==-1){
                answerAdapter.setSelectPosition(position);
                //保存学生答案
                ((QuestionActivity) mContext).updataAnswer(item.getAId(), 
position);
                //查看正确答案
                checkAnswer(position,item.getACurId());
            }else {
                Toast.makeText(mContext,"答案已
选",Toast.LENGTH_SHORT).show();
            }

        }
    });

}

private void checkAnswer(int position,int curId) {
    if (position==curId){
        iv_answer.setImageResource(R.drawable.ic_check_black_24dp);
    }else {
        iv_answer.setImageResource(R.drawable.ic_clear_black_24dp);
    }
    notifyDataSetChanged();
}

//获取当前position
public int getCurrentPosition() {
    return currentPosition;
}

5 对数据库的操作

//根据题获取所有答案
public List<AnswerBean> getAnswerS(int id) {
    List<AnswerBean> list = answerBeanDao.queryBuilder().where(AnswerBeanDao.Properties.AnswerID.eq(id)).build().list();

    return list;

}

//保存用户答案
public void updataAnswer(int id, int answerId) {
    QuestionBean questionBean = questionBeanDao.queryBuilder().where(QuestionBeanDao.Properties.QId.eq(id)).build().unique();
    if (questionBean != null) {
        questionBean.setAStuId(answerId);
        questionBeanDao.update(questionBean);
    }


}

//判断用户是否答题
public int quaryAnswer(int id) {
    int position = -1;
    QuestionBean unique = questionBeanDao.queryBuilder().where(QuestionBeanDao.Properties.QId.eq(id)).build().unique();
    if (unique != null && unique.getAStuId()!=-1) {
        position = unique.getAStuId();
        return position;
    } else {
        return position;
    }

}

以上就是主要内容。还有很多需要优化的地方,欢迎大家指教.最后附上项目源码
https://github.com/swh123118/myRepository

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值