SQLite数据库的作业

SQLite数据库的作业

首先是XML的代码

 <EditText
   android:id="@+id/et_edit"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:hint=""
   android:textSize="30sp"/>
 <LinearLayout
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:orientation="horizontal">
   <Button
       android:id="@+id/btn_add"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_weight="1"
       android:text="增加一条新纪录"
       android:textSize="25sp"/>
   <Button
       android:id="@+id/btn_del"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_weight="1"
       android:text="删除第一条记录"
       android:textSize="25sp"/>

</LinearLayout>
<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listview">

</ListView>


然后就是listview界面
    <TextView
        android:id="@+id/dddd"
        android:textSize="20sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

接下来就是几个的JAVA代码



  public class MainActivity extends AppCompatActivity {
    private EditText et_edit;
    private ListView listview;
    private MyCursorAdapter myCursorAdapter;
    private PersonDAO personDAO;
    private Cursor cursor;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

    listview = (ListView)findViewById(R.id.listview);
    
    personDAO = new PersonDAO(this);
    cursor = personDAO.selectAll();
    if(cursor!=null){
        myCursorAdapter = new MyCursorAdapter(MainActivity.this,cursor);
        listview.setAdapter(myCursorAdapter);
    }


}

public void onClick(View view) {
    switch(view.getId()){
        case R.id.btn_add:
           
            String name = et_edit.getText().toString();
            personDAO.insert(name);
            cursor = personDAO.selectAll();
            if(cursor!=null){
                myCursorAdapter = new MyCursorAdapter(this,cursor);
                listview.setAdapter(myCursorAdapter);
            }
            break;
        case R.id.btn_del:
           
            if(cursor!=null){
                if(cursor.moveToFirst()){
                    personDAO.btn_del(cursor.getString(cursor.getColumnIndex("_id")));
                    cursor = personDAO.selectAll();
                }
            }
            myCursorAdapter = new MyCursorAdapter(this,cursor);
            listview.setAdapter(myCursorAdapter);
            break;
    }

 }
}









public class PersonDAO {

    private MyDBHelper dbHelper;
    private SQLiteDatabase sqLiteDatabase;

public PersonDAO(Context context){
    dbHelper = new MyDBHelper(context);
}

public void insert(String name){
    sqLiteDatabase = dbHelper.getWritableDatabase();
    String sql = "insert into person(name) values('"+n+"')";
    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);
   }
}






 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.item,viewGroup,false);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    TextView dddd = (TextView) view.findViewById(R.id.dddd);
    dddd.setText(cursor.getString(cursor.getColumnIndex("dddd")));
    }
}

posted on 2017-05-16 22:31  敬芯铜  阅读(81)  评论(0)    收藏  举报

导航