kaoshi,主界面,查询界面

package com.hanqi.kaoshi;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView tv_Welcome;
    Button bt_kaoshi,bt_tuichu,bt_chaxun;
    String name;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_Welcome=(TextView)findViewById(R.id.tv_Welcome);
        Intent intent=getIntent();
        name=intent.getStringExtra("name");
        tv_Welcome.setText("欢迎 " + name + " 参加考试");
        bt_kaoshi=(Button)findViewById(R.id.bt_kaoshi);
        bt_tuichu=(Button)findViewById(R.id.bt_tuichu);
        bt_chaxun=(Button)findViewById(R.id.bt_chaxun);
        //开始考试
        bt_kaoshi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final ProgressDialog dialog=new ProgressDialog(MainActivity.this);
                dialog.setTitle("加载考题");
                dialog.setMessage("加载中...,稍候");
                dialog.show();
                new Thread(){
                    public void run() {
                     try {
                         Thread.sleep(2000);
                     }catch (Exception e){}
                         dialog.dismiss();//关闭对话框

                       Intent intent=new Intent(MainActivity.this, ExamActivity.class);
                       intent.putExtra("name",name);
                       startActivity(intent);
                    };
                }.start();
            }
        });
        //查询成绩
        bt_chaxun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final ProgressDialog dialog=new ProgressDialog(MainActivity.this);
                dialog.setTitle("查询成绩");
                dialog.setMessage("查询中...,稍候");
                dialog.show();
                new Thread(){
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(2000);
                        }catch (Exception e) {}
                        dialog.dismiss();
                        Intent intent1=new Intent(MainActivity.this,ChaXunActivity.class);
                        intent1.putExtra("name",name);
                        startActivity(intent1);
                    }
                }.start();

            }
        });


        //退出考试
        bt_tuichu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View layout=View.inflate(MainActivity.this, R.layout.exit, null);
                AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("退出")
                        .setView(layout)
                        .setPositiveButton("退出", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                finish();
                            }
                        })
                        .setNegativeButton("取消", null);
                builder.create().show();
            }
        });


    }



}
package com.hanqi.kaoshi;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.hanqi.kaoshi.exam.ExamDAO;
import com.hanqi.kaoshi.exam.ExamPOJO;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class ExamActivity extends AppCompatActivity {
    Button bt_11;
    CheckBox cb_1,cb_2,cb_3,cb_4;
    EditText ed_name;
    ExamDAO examDAO=new ExamDAO(ExamActivity.this);
    ArrayList<ExamPOJO> arraylist;
    String name;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exam);
        cb_1=(CheckBox)findViewById(R.id.cb_1);
        cb_2=(CheckBox)findViewById(R.id.cb_2);
        cb_3=(CheckBox)findViewById(R.id.cb_3);
        cb_4=(CheckBox)findViewById(R.id.cb_4);
        bt_11=(Button)findViewById(R.id.bt_11);
        ed_name=(EditText)findViewById(R.id.ed_name);
        Intent intent=getIntent();
        name=intent.getStringExtra("name");
        ed_name.setText(name);
    }
    //提交按钮
    public void bt11_OnClick(View v)
    {
        if (!cb_1.isChecked()&&!cb_2.isChecked()&&!cb_3.isChecked()&&!cb_4.isChecked())
        {
            Toast.makeText(ExamActivity.this, "请选择选项再提交", Toast.LENGTH_SHORT).show();
        }else {
            AlertDialog alertDialog=new AlertDialog.Builder(this)
                    .setTitle("确定要提交了么?")
                    .setCancelable(false)
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if (cb_3.isChecked()) {
                                int score = 100;
                                String examname = "java";
                                //得到系统时间
                                SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd   hh:mm:ss");
                                String starttime = sDateFormat.format(new Date());
                                String name = ed_name.getText().toString();
                                ExamPOJO exam = new ExamPOJO(examname, name, score, starttime);
                                //2通过DAO插入数据
                                long l = examDAO.insert(exam);
                                if (l > 0) {
                                    Toast.makeText(ExamActivity.this, "提交成功", Toast.LENGTH_SHORT).show();

                                } else {
                                    Toast.makeText(ExamActivity.this, "提交失败", Toast.LENGTH_SHORT).show();
                                }

                            } else {
                                int score = 0;
                                String examname = "java";
                                //得到系统时间
                                SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd   hh:mm:ss");
                                String starttime = sDateFormat.format(new Date());
                                String name = ed_name.getText().toString();
                                ExamPOJO exam = new ExamPOJO(examname, name, score, starttime);
                                //2通过DAO插入数据
                                long l = examDAO.insert(exam);
                                if (l > 0) {
                                    Toast.makeText(ExamActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(ExamActivity.this, "提交失败", Toast.LENGTH_SHORT).show();
                                }
                            }
                        }
                    })
                    .setNegativeButton("取消", null)
                    .show();

        }
    }

}
package com.hanqi.kaoshi;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.hanqi.kaoshi.exam.ExamDAO;
import com.hanqi.kaoshi.exam.ExamPOJO;

import java.util.ArrayList;

public class ChaXunActivity extends ListActivity {
    ListView lv_1;
    ExamDAO examDAO=new ExamDAO(this);
    ArrayList<ExamPOJO> arraylist;
    MyAdapter myAdapter;
    int index;
    TextView tv_3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cha_xun);
        lv_1=getListView();
        Intent intent=getIntent();
        lv_1.setOnCreateContextMenuListener(this);
        arraylist=examDAO.getAll();
        myAdapter=new MyAdapter();
        lv_1.setAdapter(myAdapter);
        tv_3=(TextView)findViewById(R.id.tv_3);

    }
    //重写上下文菜单
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, 1, 1, "修改");
        menu.add(0, 2, 2, "删除");
        AdapterView.AdapterContextMenuInfo acmi=
                (AdapterView.AdapterContextMenuInfo)menuInfo;
        index=acmi.position;
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId())
        {
            case 1://修改成绩
                final EditText editText=new EditText(this);
                editText.setHint("输入要修改的分数");
                editText.setInputType(InputType.TYPE_CLASS_NUMBER);
                editText.setText(arraylist.get(index).getScore()+"");
                new AlertDialog.Builder(this)
                        .setTitle("修改成绩")
                        .setView(editText)
                        .setNegativeButton("取消",null)
                        .setPositiveButton("保存", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                int i = Integer.parseInt(editText.getText().toString());
                                if (i > 100) {
                                    Toast.makeText(ChaXunActivity.this, "修改的分数不能大于100啊!", Toast.LENGTH_SHORT).show();
                                } else {
                                    ExamPOJO examPOJO = arraylist.get(index);
                                    examPOJO.setScore(i);
                                    if (examDAO.update(examPOJO) > 0) {
                                        Toast.makeText(ChaXunActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                                        myAdapter.notifyDataSetChanged();
                                    } else {
                                        Toast.makeText(ChaXunActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            }
                        })
                        .show();
                break;
            case 2://删除
                View view1=View.inflate(this,R.layout.shanchu,null);
                new AlertDialog.Builder(this)
                        .setTitle("删除")
                        .setView(view1)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (examDAO.delete(arraylist.get(index))>0)
                                {
                                    arraylist.remove(index);
                                    myAdapter.notifyDataSetChanged();
                                    Toast.makeText(ChaXunActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                                }else
                                {
                                    Toast.makeText(ChaXunActivity.this, "删除失败", Toast.LENGTH_SHORT).show();
                                }
                            }
                        })
                        .setNegativeButton("取消",null)
                        .show();
                break;

        }
        return super.onContextItemSelected(item);
    }

    //适配器
    class MyAdapter extends BaseAdapter
    {
        @Override
        public int getCount() {
            return arraylist.size();
        }

        @Override
        public Object getItem(int position) {
            return arraylist.get(position);
        }

        @Override
        public long getItemId(int position) {
            return arraylist.get(position).getId();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ExamPOJO examPOJO=arraylist.get(position);
            if (convertView == null) {
                convertView = View.inflate(ChaXunActivity.this, R.layout.listview, null);
            }
            TextView tv_1 = (TextView) convertView.findViewById(R.id.tv_1);
            tv_1.setText(examPOJO.getExamname());
            TextView tv_2 = (TextView) convertView.findViewById(R.id.tv_2);
            tv_2.setText(examPOJO.getName());
            TextView tv_3 = (TextView) convertView.findViewById(R.id.tv_3);
            tv_3.setText(examPOJO.getScore()+"" + ".");
            TextView tv_4= (TextView) convertView.findViewById(R.id.tv_4);
            tv_4.setText(examPOJO.getStarttime());
            return convertView;
        }
    }
}
package com.hanqi.kaoshi.exam;

/**
 * Created by Administrator on 2016/7/4.
 */
public class ExamPOJO {
    private  long id;
    //考试科目
    private String examname;
    //姓名
    private String name;
    //成绩
    private int score;
    //考试开始时间
    private  String starttime;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getExamname() {
        return examname;
    }

    public void setExamname(String examname) {
        this.examname = examname;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public String getStarttime() {
        return starttime;
    }

    public void setStarttime(String starttime) {
        this.starttime = starttime;
    }
    public ExamPOJO(){}

    public ExamPOJO(long id, String examname, String name, int score, String starttime) {
        this.id = id;
        this.examname = examname;
        this.name = name;
        this.score = score;
        this.starttime = starttime;
    }

    public ExamPOJO(String examname, String name, int score, String starttime) {
        this.examname = examname;
        this.name = name;
        this.score = score;
        this.starttime = starttime;
    }
}
View Code
package com.hanqi.kaoshi.exam;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.hanqi.kaoshi.Zhuce.DBHelper;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/7/4.
 */
public class ExamDAO {
    private DBHelper dh;
    private final String TABLE_NAME="t_exam";
    public ExamDAO(Context context)
    {
        dh=new DBHelper(context);
    }
    //
    public long insert(ExamPOJO exam)
    {
        long rtn=0;
        SQLiteDatabase sd=dh.getReadableDatabase();
        ContentValues contentValues=new ContentValues();
        contentValues.put("examname",exam.getExamname());
        contentValues.put("name",exam.getName());
        contentValues.put("score",exam.getScore());
        contentValues.put("starttime",exam.getStarttime());
        rtn=sd.insert(TABLE_NAME, null, contentValues);
        sd.close();
        return rtn;
    }
    //
    public long delete(ExamPOJO exam)
    {
        SQLiteDatabase sqLiteDatabase=dh.getWritableDatabase();
        long rtn=sqLiteDatabase.delete(TABLE_NAME, "_id=?", new String[]{exam.getId() + ""});
        sqLiteDatabase.close();
        return rtn;
    }
    //
    public long update(ExamPOJO exam)
    {
        SQLiteDatabase  sqLiteDatabase=dh.getWritableDatabase();
        ContentValues contentValues=new ContentValues();
        contentValues.put("examname",exam.getExamname());
        contentValues.put("name",exam.getName());
        contentValues.put("score",exam.getScore());
        contentValues.put("starttime",exam.getStarttime());
        long rtn=sqLiteDatabase.update(TABLE_NAME, contentValues, "_id=?"
                , new String[]{exam.getId() + ""});
        sqLiteDatabase.close();
        return rtn;
    }
    //
    public ArrayList<ExamPOJO> getAll()
    {
        ArrayList<ExamPOJO> arraylist=new ArrayList<>();
        SQLiteDatabase sqlitedatabase=dh.getWritableDatabase();
        Cursor cursor=sqlitedatabase.query(TABLE_NAME,null,null,null,null,null,null);
        while (cursor.moveToNext())
        {
            ExamPOJO exam=new ExamPOJO(cursor.getLong(0),cursor.getString(1),cursor.getString(2),
                    cursor.getInt(3),cursor.getString(4));
            arraylist.add(0,exam);
        }
        sqlitedatabase.close();
        return arraylist;
    }
}
View Code

 

posted @ 2016-07-17 13:44  什么玩楞啊,我叫旺仔  阅读(259)  评论(0编辑  收藏  举报