1、建立一个MyDBOpenHelper.java的类用来创建数据表
MyDBOpenHelper.java
package com.example.schedule;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyDBOpenHelper extends SQLiteOpenHelper {
public MyDBOpenHelper(Context context){
super(context,"DB_planList",null,1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table tb_plan(id integer primary key autoincrement,date text, mark text,headline text,detail text);"; //建数据表
db.execSQL(sql); //执行SQL语句
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
2、add.xml
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/rijihua_beijing">
android:layout_width="fill_parent"
android:layout_height="50dip"
android:gravity="center">
android:layout_height="45dip"
android:layout_marginBottom="6dip"
android:layout_marginTop="10dip"
android:layout_gravity="center"
android:paddingTop="6dip">
android:layout_height="45px"
android:text="日期:"
android:textSize="18sp"/>
android:layout_width="180px"
android:layout_height="40px"
android:hint="选择结束日期"
/>
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="设置日期"
android:background="#F8F8FF"/>
android:layout_width="306dip"
android:layout_height="40dip"
android:layout_marginBottom="20dip"
android:layout_marginTop="10dip"
android:layout_gravity="center"
android:paddingTop="6dip"
>
android:layout_height="45px"
android:text="标志:"
android:textSize="18sp"
/>
android:layout_width="fill_parent"
android:layout_height="40px"
android:hint="日d;周w;月m,年y"
/>
android:layout_width="306dip"
android:layout_height="40dip"
android:layout_marginBottom="20dip"
android:layout_marginTop="10dip"
android:layout_gravity="center"
android:paddingTop="6dip">
android:layout_height="45px"
android:text="标题:"
android:textSize="18sp"
/>
android:layout_width="fill_parent"
android:layout_height="40px"
/>
android:layout_height="45px"
android:text="详细内容:"
android:textSize="18sp"/>
android:layout_height="40dip"
android:layout_marginBottom="6dip"
android:layout_marginTop="10dip"
android:layout_gravity="center"
android:paddingTop="6dip">
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
android:layout_height="fill_parent"
android:gravity="center"
>
android:layout_width="150dip"
android:layout_height="45dip"
android:text="取消"
android:gravity="center"
android:background="#F8F8FF"
/>
android:layout_width="150dip"
android:layout_height="45dip"
android:text="确定"
android:gravity="center"
android:layout_marginLeft="40px"
android:background="#F8F8FF"/>
3、AddActivity.java类用于创建数据库,并将EditText和TextView里获取的数据存入SQLite数据库里。
AddActivity.java
package com.example.schedule;
import java.util.Calendar;
import java.util.Locale;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class AddActivity extends Activity{
Button btn_shezhi,btn_quxiao,btn_queding;
EditText et_biaoti,et_xiangxineirong,et_biaozhi;
TextView tv_riqi;
String date , mark ,headline,detail ;
private Cursor result;
private SQLiteDatabase dbWrite;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add);
btn_shezhi=(Button)findViewById(R.id.btn_shezhi);
btn_quxiao=(Button)findViewById(R.id.btn_quxiao);
btn_queding=(Button)findViewById(R.id.btn_queding);
tv_riqi=(TextView)findViewById(R.id.tv_riqi);
et_biaoti=(EditText)findViewById(R.id.et_biaoti);
et_biaozhi=(EditText)findViewById(R.id.et_biaozhi);
et_xiangxineirong=(EditText)findViewById(R.id.et_xiangxineirong);
//数据库创建
MyDBOpenHelper dbHelper=new MyDBOpenHelper(this);
dbWrite=dbHelper.getWritableDatabase();
btn_shezhi.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Calendar calendar = Calendar.getInstance(Locale.CHINA);
DatePickerDialog datepickerdialog=new DatePickerDialog (
AddActivity.this, myOnDateCaptureListener,calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH) );
datepickerdialog.show();
}
DatePickerDialog.OnDateSetListener myOnDateCaptureListener=
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
tv_riqi.setText(year+"年"+(monthOfYear+1)+"月"+dayOfMonth+"日");
date=tv_riqi.getText().toString();
}
};
});
et_biaozhi.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
mark=et_biaozhi.getText().toString();
return false;
}
});
//date text, mark text,headline text,detail text
btn_queding.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//将数据存入数据库
if(tv_riqi.getText().toString().length()!=0){
date=tv_riqi.getText().toString();
}else{
Toast.makeText(getApplication(), "日期不能为空", Toast.LENGTH_SHORT).show();
return;
}
if(et_biaozhi.getText().toString().length()!=0){
mark=et_biaozhi.getText().toString();
}else{
Toast.makeText(getApplication(), "标志不能为空", Toast.LENGTH_SHORT).show();
return;
}
if(et_biaoti.getText().toString().length()!=0){
headline=et_biaoti.getText().toString();
}else{
Toast.makeText(getApplication(), "标题不能为空", Toast.LENGTH_SHORT).show();
return;
}
if(et_xiangxineirong.getText().toString().length()!=0){
detail=et_xiangxineirong.getText().toString();
}else{
Toast.makeText(getApplication(), "详细内容不能为空", Toast.LENGTH_SHORT).show();
return;
}
ContentValues cv=new ContentValues();
cv.put("date", date);
cv.put("mark", mark);
cv.put("headline", headline);
cv.put("detail", detail);
Toast.makeText(getApplication(), "添加成功", Toast.LENGTH_SHORT).show();
dbWrite.insert("tb_plan", "date", cv);
tv_riqi.setText("");
et_biaozhi.setText("");
et_biaoti.setText("");
et_xiangxineirong.setText("");
if(mark.equals("d")){
Intent intent = new Intent();
setResult(00, intent);
finish();
}
if(mark.equals("w")){
Intent intent = new Intent();
setResult(11, intent);
finish();
}
if(mark.equals("m")){
Intent intent = new Intent();
setResult(22, intent);
finish();
}
if(mark.equals("y")){
Intent intent = new Intent();
setResult(33, intent);
finish();
}
}
});
btn_quxiao.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(mark.equals("d")){
Intent intent1 = new Intent();
setResult(00, intent1);
finish();
}
if(mark.equals("w")){
Intent intent1 = new Intent();
setResult(11, intent1);
finish();
}
if(mark.equals("m")){
Intent intent1 = new Intent();
setResult(22, intent1);
finish();
}
if(mark.equals("y")){
Intent intent1 = new Intent();
setResult(33, intent1);
finish();
}
}
});
}
}
4、在Fragmnet用ListView将SQLite里的数据查询并显示出来
FragmentDay.java
package com.example.schedule;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class FragmentDay extends Fragment{
ListView r_listview;
private SQLiteDatabase dbRead;
MyDBOpenHelper dbHelper;
String idStr,dateStr , markStr ,headlineStr,detailStr ;
private Cursor result;
private ArrayList
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_day, container, false);
r_listview=(ListView)view.findViewById(R.id.r_listview); //注意:ListView的实体化
dbHelper=new MyDBOpenHelper(view.getContext()); //注意:dbHelper的实体化
//查询数据库
dbRead=dbHelper.getReadableDatabase();
String sql="select date,mark,headline, detail from tb_plan where mark='d' order by date";
result=dbRead.rawQuery(sql,null);
int resultCounts=result.getCount();
if(resultCounts==0||!result.moveToFirst()){
Toast.makeText(getActivity()
, "数据库中无数据", Toast.LENGTH_SHORT).show();
}else{
while(!result.isAfterLast()){
// idStr=String.valueOf(result.getInt(result.getColumnIndex("id")))+"\t";
dateStr=result.getString(result.getColumnIndex("date"))+":\t";
markStr=result.getString(result.getColumnIndex("mark"))+"\t";
headlineStr=result.getString(result.getColumnIndex("headline"))+"\t";
detailStr=result.getString(result.getColumnIndex("detail"));
plan.add(dateStr+markStr+headlineStr+detailStr);
result.moveToNext();
}
}
r_listview.setAdapter(new ArrayAdapter
dbRead.close();
return view;
}
}
注意标红的地方,希望能够帮助到有需要的人。