在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