Fragment里面使用Button按钮添加、删除数据(SQL)

在android平台上一直没用过sql数据存储方式。最近根据http://www.vogella.com/tutorials/android.html上的教程,在之前练习项目中的一个fragment内添加了两个Button按钮(添加、删除),并在button下部放置了一个ListView。布局如下:

<fragmentOne.xml>

    <Button android:layout_width="wrap_content"
            android:layout_height="48dp"
            android:id="@+id/add_data_btn"
            android:text="ADD NEW"
            android:layout_marginLeft="8dp"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp"
            android:onClick="onClick" />

    <Button android:layout_width="wrap_content"
            android:layout_height="48dp"
            android:id="@+id/delete_btn"
            android:layout_toRightOf ="@id/add_data_btn"
            android:text="DELETE DATA"
            android:layout_marginTop="8dp"
            android:layout_marginLeft="8dp"
            android:onClick="onClick" />

    <ListView android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:id="@android:id/list"
              android:text="hello sql"
              android:layout_below= "@id/add_data_btn" />

<FragmentOne.java>

public class YoudaoFragmentOne extends SherlockListFragment implements View.OnClickListener {

    private Button mAddDataBtn;
    private Button mDeleteBtn;
    ......
}

Fragment类需要implemente View.OnClickListenter,不然当触发button监听事件时出错。

public void onClick(View view) {
        switch (view.getId()) {
            case R.id.add_data_btn:
                // save the new comment to the database
                break;
            case R.id.delete_btn:
                break;
        }
}

源码为IntlliJ IDEA工程文件,下载地址:https://s.yunio.com/QHNRhm


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值