MainActivity.java package com.example.sqlclass;import android.content.ContentValues;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import
package com.example.sqlclass; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private MyDataBase dbHelper; private EditText etname; private EditText etage; private EditText etgrade; private ListView list_view; private ArrayListMyDataBase.javastudentlist; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dbHelper = new MyDataBase(this); list_view= (ListView) findViewById(R.id.list_view); Button show = (Button) findViewById(R.id.show); Button add = (Button) findViewById(R.id.add); etname = (EditText) findViewById(R.id.et_name); etage = (EditText) findViewById(R.id.et_age); etgrade = (EditText) findViewById(R.id.et_grade); dbHelper.getWritableDatabase(); show.setOnClickListener(this); add.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.show: querystudent(); break; case R.id.add: add(); default: break; } } private void querystudent() { studentlist = new ArrayList<>(); SQLiteDatabase db = dbHelper.getWritableDatabase(); Cursor cur = db.query("info", null, null, null, null, null, null); if (cur != null && cur.getCount() > 0){ while (cur.moveToNext()) { String name = cur.getString(cur.getColumnIndex("name")); String age = cur.getString(cur.getColumnIndex("age")); String grade = cur.getString(cur.getColumnIndex("grade")); student_info st = new student_info(name, age, grade); st.setName(name); st.setAge(age); st.setGrade(grade); studentlist.add(st); } list_view.setAdapter(new BaseAdapter() { @Override public int getCount() { return studentlist.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view; if (convertView==null){ view=View.inflate(MainActivity.this,R.layout.student_item,null); } else { view=convertView; } student_info st = studentlist.get(position); TextView tvname = (TextView) view.findViewById(R.id.tvname); TextView tvage = (TextView) view.findViewById(R.id.tvage); TextView tvgrade = (TextView) view.findViewById(R.id.tvgrade); tvname.setText(st.getName()); tvage.setText(st.getAge()); tvgrade.setText(st.getGrade()); return view; } }); } } private void add() { SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); String stuname = etname.getText().toString().trim(); String stuage = etage.getText().toString().trim(); String stugrade = etgrade.getText().toString().trim(); values.put("name", stuname); values.put("age", stuage); values.put("grade", stugrade); db.insert("info",null,values); db.close(); Toast.makeText(MainActivity.this, "success", Toast.LENGTH_SHORT).show(); } }
package com.example.sqlclass; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.widget.Toast; public class MyDataBase extends SQLiteOpenHelper { public MyDataBase(Context context) { super(context, "student.db", null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table info(_id integer primary key autoincrement,name varchar(20),age varchar(20),grade varchar(20))"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }student_info.java
package com.example.sqlclass; /** * Created by Lancer on 2017/11/22. */ public class student_info { private String name; private String age; private String grade; public void setName(String name) { this.name = name; } public void setAge(String age) { this.age = age; } public student_info( String name, String age, String grade) { this.name = name; this.age = age; this.grade = grade; } public void setGrade(String grade) { this.grade = grade; } public String getName() { return name; } public String getAge() { return age; } public String getGrade() { return grade; } }activity_main.xml
student_item.xml