当前位置:Gxlcms > 数据库问题 > SQLite—homework

SQLite—homework

时间:2021-07-01 10:21:17 帮助过:21人阅读

 <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

标签:inf   exe   ble   ppc   weight   textview   width   ima   ati   

人气教程排行