1. 程式人生 > >SQLite—homework

SQLite—homework

ont pan override prot res led XML this text

主要的界面布局:

 <EditText
android:id="@+id/edt"
android:textSize="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/add"
android:textSize="10sp"
android:layout_width="0dp"
android:layout_weight="1"
android:onClick="onClick"
android:layout_height="wrap_content"
android:text="增加一條新記錄"/>
<Button
android:id="@+id/delete"
android:textSize="10sp"
android:layout_width="0dp"
android:layout_weight="1"
android:onClick="onClick"
android:layout_height="wrap_content"
android:text="刪除第一條記錄"/>
</LinearLayout>
<ListView
android:id="@+id/list_item"
android:layout_width="match_parent"
android:layout_height="wrap_content">

技術分享

第二個布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_list"/>

</LinearLayout>
首先要進行數據庫表的創建:

跟之前的一樣 只是沒那麽復雜
public class MyDBHelper extends SQLiteOpenHelper {
private String sql = "create table student(_id integer primary key autoincrement," + "name text not null," +
"classmate text not null,"+"age integer)";


public MyDBHelper(Context context) {
super(context, "studentInfo", null, 1);

}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(sql);
}


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists student");
onCreate(db);
}
}



DAO部分
public class DAO {
private MyDBHelper dbHelper;
private SQLiteDatabase sqLiteDatabase;

public DAO(Context context){
dbHelper = new MyDBHelper(context);
}
//向數據庫中插入數據
public void insert(String name){
sqLiteDatabase = dbHelper.getWritableDatabase();
String sql = "insert into person(name) values(‘"+name+"‘)";
sqLiteDatabase.execSQL(sql);
}

public Cursor selectAll() {
sqLiteDatabase = dbHelper.getReadableDatabase();
Cursor cursor = sqLiteDatabase.query("person", null, null, null, null, null, null);
return cursor;
}

public void delete(String id){
sqLiteDatabase = dbHelper.getWritableDatabase();
String sql = "delete from person where _id="+String.valueOf(id);//"delete from student where _id="+id
sqLiteDatabase.execSQL(sql);
}
}
Adapter部分:
public class MyCursorAdapter extends CursorAdapter {


public MyCursorAdapter(Context context, Cursor cursor) {
super(context, cursor, 0);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
return LayoutInflater.from(context).inflate(R.layout.listview_mian,viewGroup,false);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView name = (TextView) view.findViewById(R.id.tv_list);
name.setText(cursor.getString(cursor.getColumnIndex("name")));
}
}
MainActivity Java代碼:
public class MainActivity extends AppCompatActivity {
private EditText edt;
private ListView listView;
private MyCursorAdapter adapter;
private DAO DAO;
private Cursor cursor;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

edt = (EditText)findViewById(R.id.edt);

listView = (ListView)findViewById(R.id.list_item);
DAO = new DAO(this);
cursor = DAO.selectAll();
if(cursor!=null){
adapter = new MyCursorAdapter(MainActivity.this,cursor);
listView.setAdapter(adapter);
}


}

public void onClick(View view) {
switch(view.getId()){
case R.id.add:

String name = edt.getText().toString();
DAO.insert(name);
cursor = DAO.selectAll();
if(cursor!=null){
adapter = new MyCursorAdapter(this,cursor);
listView.setAdapter(adapter);
}
break;
case R.id.delete:

if(cursor!=null){
if(cursor.moveToFirst()){
DAO.delete(cursor.getString(cursor.getColumnIndex("_id")));
cursor = DAO.selectAll();
}
}
adapter = new MyCursorAdapter(this,cursor);
listView.setAdapter(adapter);
break;
}

}
}

技術分享

但不幸的是 運行起來依然存在問題 。。。




 
 

SQLite—homework