Java 检测加载程序的SQLite数据库上的更改

Java 检测加载程序的SQLite数据库上的更改,java,android,sqlite,broadcastreceiver,loader,Java,Android,Sqlite,Broadcastreceiver,Loader,我一直在构建一个简单的note应用程序,并成功地使用SQlite数据库和加载程序在listview上加载数据。下一步我要做的是,当我在数据库中添加、更改或删除注释时,让加载程序自动重新加载,但我不知道如何加载。我已经搜索过,但主要是针对内容提供商的教程,我阅读了以下内容: 但不太了解,因为他们使用广播接收器在sd卡上取零钱。 我会倾听任何建议,并提前感谢您的帮助 这是我的代码:WhiteNote.java public class WhiteNote extends Fragment imple

我一直在构建一个简单的note应用程序,并成功地使用SQlite数据库和加载程序在listview上加载数据。下一步我要做的是,当我在数据库中添加、更改或删除注释时,让加载程序自动重新加载,但我不知道如何加载。我已经搜索过,但主要是针对内容提供商的教程,我阅读了以下内容: 但不太了解,因为他们使用广播接收器在sd卡上取零钱。 我会倾听任何建议,并提前感谢您的帮助

这是我的代码:WhiteNote.java

public class WhiteNote extends Fragment implements LoaderManager.LoaderCallbacks<ArrayList<NoteItems>> {
private NoteDatabase note_database;
private int i=0;
public Context context;
public NoteListAdapter noteListAdapter;
public ListView note_listview_container;
public SQLiteDatabase note_sqldb;
public Cursor c;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.white_note, container, false);

    context=getActivity();
    note_database = new NoteDatabase(context);

    final EditText text_ed_1;
    final EditText text_ed_2;
    Button button_addNote;
    Button button_listallNote;
    Button button_delallNote;

    text_ed_1 = (EditText)rootView.findViewById(R.id.textedit1);
    text_ed_2 = (EditText)rootView.findViewById(R.id.textedit2);
    button_addNote = (Button)rootView.findViewById(R.id.button1);
    button_listallNote = (Button)rootView.findViewById(R.id.button2);
    button_delallNote = (Button)rootView.findViewById(R.id.button3);
    note_listview_container=(ListView)rootView.findViewById(R.id.note_listview);
    noteListAdapter=new NoteListAdapter(context, new ArrayList<NoteItems>());

    note_database.open();
    getLoaderManager().initLoader(0, null,WhiteNote.this);  
    note_database.close();

    button_addNote.setOnClickListener(new OnClickListener() {     
        @Override
        public void onClick(View v) {
            note_database.open();

            note_database.createData(text_ed_1.getText().toString(),text_ed_2.getText().toString());
            //i++;
            note_database.close();
        }
    });

    button_listallNote.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            note_database.open();

            note_database.get_NoteListAdapter();
            note_listview_container.setAdapter(note_database.dbnoteListAdapter);

            note_database.close();
            //note_list.setText(ds);
        }
    });

    button_delallNote.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            note_database.open();
            note_database.deleteAllNote();
            note_database.close();
        }
    });
    return rootView;
}

@Override
public Loader<ArrayList<NoteItems>> onCreateLoader(int id, Bundle args) {
    return new NoteItemsLoader(context,note_database);
}

@Override
public void onLoadFinished(Loader<ArrayList<NoteItems>> loader,
                       ArrayList<NoteItems> data) {
    note_listview_container.setAdapter(new NoteListAdapter(context, data));
}

@Override
public void onLoaderReset(Loader<ArrayList<NoteItems>> loader) {
    note_listview_container.setAdapter(null);
}
}

class NoteItemsLoader extends AsyncTaskLoader<ArrayList<NoteItems>> {
    private ArrayList<NoteItems> loader_noteitems= new ArrayList<NoteItems>();
    private NoteDatabase loader_db;

    public NoteItemsLoader(Context context, NoteDatabase db) {
        super(context);
        loader_db = db;
        loader_noteitems=loader_db.get_NoteListArray(loader_noteitems,loader_db);
    }

    @Override
    protected void onStartLoading() {
        if (loader_noteitems != null) {
            deliverResult(loader_noteitems); // Use the cache
        }
        else
            forceLoad();
    }

    @Override
    protected void onStopLoading() {
        cancelLoad();
    }

    @Override
    public ArrayList<NoteItems> loadInBackground() {              
        loader_db.open(); 

        ArrayList<NoteItems> note_items = new ArrayList<NoteItems>();
        loader_db.get_NoteListArray(note_items,loader_db);

        loader_db.close();
        return note_items;
    }

    @Override
    public void deliverResult(ArrayList<NoteItems> data) {
        if (isReset()) {
            if (data != null) {
                onReleaseResources(data);
            }
        }
        ArrayList<NoteItems> oldNotes = loader_noteitems;
        loader_noteitems = data;

        if (isStarted()) {
            super.deliverResult(data);
        }

        if (oldNotes != null) {
            onReleaseResources(oldNotes);
        }
    }

    @Override
    protected void onReset() {
        super.onReset();
        onStopLoading();
        loader_noteitems = null;
    }

    @Override
    public void onCanceled(ArrayList<NoteItems> data) {
         super.onCanceled(data);
         loader_noteitems = null;
    }

    protected void onReleaseResources(ArrayList<NoteItems> data) {}

}
公共类WhiteNote扩展片段实现LoaderManager.LoaderCallbacks{
私有NoteDatabase note_数据库;
私有整数i=0;
公共语境;
公共NoteListAdapter NoteListAdapter;
公共列表视图注释\u列表视图\u容器;
公共SQLITE数据库注释\u sqldb;
公共图书馆c;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
最终视图根视图=充气机。充气(右布局。白色注释,容器,假);
context=getActivity();
note_database=新NoteDatabase(上下文);
最终编辑文本文本第1版;
最终编辑文本文本第2版;
按钮添加注释;
按钮列表所有注释;
按钮按钮说明;
text_ed_1=(EditText)rootView.findViewById(R.id.textedit1);
text_ed_2=(EditText)rootView.findViewById(R.id.textedit2);
按钮\添加注释=(按钮)rootView.findViewById(R.id.button1);
button_listalnote=(button)rootView.findviewbyd(R.id.button2);
button_delallNote=(button)rootView.findviewbyd(R.id.button3);
note_listview_container=(listview)rootView.findviewbyd(R.id.note_listview);
noteListAdapter=newNoteListAdapter(上下文,newArrayList());
注意_database.open();
getLoaderManager().initLoader(0,null,WhiteNote.this);
注意_database.close();
按钮\u addNote.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
注意_database.open();
注意_database.createData(text_ed_1.getText().toString(),text_ed_2.getText().toString());
//i++;
注意_database.close();
}
});
按钮\u listalnote.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
注意_database.open();
note_database.get_NoteListAdapter();
note_listview_container.setAdapter(note_database.dbnoteListAdapter);
注意_database.close();
//注释\u list.setText(ds);
}
});
button_delallNote.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
注意_database.open();
note_database.deleteAllNote();
注意_database.close();
}
});
返回rootView;
}
@凌驾
公共加载器onCreateLoader(int-id,Bundle-args){
返回新的NoteItemsLoader(上下文,note_数据库);
}
@凌驾
public void onLoadFinished(加载程序,
ArrayList数据){
note_listview_container.setAdapter(新的NoteListAdapter(上下文,数据));
}
@凌驾
公共void onLoaderReset(加载器){
注意\u listview\u container.setAdapter(null);
}
}
类NoteItemsLoader扩展了AsyncTaskLoader{
私有ArrayList loader_noteitems=新ArrayList();
私有NoteDatabase loader_db;
公共NoteItemsLoader(上下文,NoteDatabase db){
超级(上下文);
加载器_db=db;
loader\u noteems=loader\u db.get\u NoteListArray(loader\u noteems,loader\u db);
}
@凌驾
开始加载时受保护的void(){
if(loader_noteitems!=null){
deliverResult(loader_noteitems);//使用缓存
}
其他的
力载荷();
}
@凌驾
受保护的无效onStopLoading(){
取消加载();
}
@凌驾
公共ArrayList loadInBackground(){
loader_db.open();
ArrayList note_items=新建ArrayList();
loader\u db.get\u NoteListArray(注意\u项,loader\u db);
loader_db.close();
退货通知单及其他物品;
}
@凌驾
公共void deliverResult(ArrayList数据){
if(isReset()){
如果(数据!=null){
发布资源(数据);
}
}
ArrayList oldNotes=装入器\注释项;
加载器\注释项=数据;
如果(isStarted()){
super.deliverResult(数据);
}
if(oldNotes!=null){
onReleaseResources(旧票据);
}
}
@凌驾
受保护的void onReset(){
super.onReset();
onStopLoading();
loader_noteitems=null;
}
@凌驾
取消后的公共作废(ArrayList数据){
super.onCanceled(数据);
loader_noteitems=null;
}
受保护的无效onReleaseResources(ArrayList数据){}
}
my NoteDatabase.java

public class NoteDatabase {
private static final String DATABASE_NAME = "DB_NOTE";
private static final int DATABASE_VERSION = 1;
public static final String TABLE_NOTE = "NOTE";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_TOPIC = "Topic";
public static final String COLUMN_NOTECONTENT = "Content";
public static final String COLUMN_NAME = "Name";

public NoteListAdapter dbnoteListAdapter;

private static Context my_context;
static SQLiteDatabase note_sqldb;
private OpenHelper noteopenHelper;

public NoteDatabase(Context c){
    NoteDatabase.my_context = c;
}

public NoteDatabase open() throws SQLException{
    noteopenHelper = new OpenHelper(my_context);
    note_sqldb = noteopenHelper.getWritableDatabase();
    return this;
}

public void close(){
    noteopenHelper.close();
}

public long createData(String chude_note, String noidung_note) { 
    ContentValues cv = new ContentValues();
    cv.put(COLUMN_TOPIC, chude_note);
    cv.put(COLUMN_NOTECONTENT, noidung_note);
    cv.put(COLUMN_NAME, "by Black");
    return note_sqldb.insert(TABLE_NOTE, null, cv);
}

public String getData() {
    String[] columns = new String[] {COLUMN_ID,COLUMN_TOPIC,COLUMN_NOTECONTENT,COLUMN_NAME};
    Cursor c = note_sqldb.query(TABLE_NOTE, columns, null, null, null, null, null);
    /*if(c==null)
        Log.v("Cursor", "C is NULL");*/
    String result="";
    int iRow = c.getColumnIndex(COLUMN_ID);
    int iTopic = c.getColumnIndex(COLUMN_TOPIC);
    int iContent = c.getColumnIndex(COLUMN_NOTECONTENT);
    int iOwner = c.getColumnIndex(COLUMN_NAME);
    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){     
        result = result +" \n"+ c.getString(iRow)
                + "\n - Topic: " + c.getString(iTopic)
                + "\n - Content: " + c.getString(iContent)
                + "\n - Owner: " + c.getString(iOwner) + "\n";
    }
    c.close();
    //Log.v("Result", result);
    return result;
}

public Cursor selectQuery(String query) {
      Cursor c1 = null;
      try {

       if (note_sqldb.isOpen()) {
           note_sqldb.close();

       }
       note_sqldb = noteopenHelper.getWritableDatabase();
       c1 = note_sqldb.rawQuery(query, null);

      } catch (Exception e) {

       System.out.println("DATABASE ERROR " + e);

      }
      return c1;

     }

public void get_NoteListAdapter() {

  ArrayList<NoteItems> noteList = new ArrayList<NoteItems>();
  noteList.clear();


  open();
  String[] columns = new String[] {NoteDatabase.COLUMN_ID,NoteDatabase.COLUMN_TOPIC,NoteDatabase.COLUMN_NOTECONTENT,NoteDatabase.COLUMN_NAME};
  note_sqldb = noteopenHelper.getWritableDatabase();
    Cursor c1 = note_sqldb.query(NoteDatabase.TABLE_NOTE, columns, null, null, null, null, null);
    int iRow = c1.getColumnIndex(NoteDatabase.COLUMN_ID);
    int iTopic = c1.getColumnIndex(NoteDatabase.COLUMN_TOPIC);
    int iContent = c1.getColumnIndex(NoteDatabase.COLUMN_NOTECONTENT);
    int iOwner = c1.getColumnIndex(NoteDatabase.COLUMN_NAME);
    for(c1.moveToFirst(); !c1.isAfterLast(); c1.moveToNext()){     
      NoteItems one_rowItems = new  NoteItems();

      one_rowItems.set_rowTopic(c1.getString(iTopic));
      one_rowItems.set_rowContent(c1.getString(iContent));
      one_rowItems.set_rowOwner(c1.getString(iOwner));

      noteList.add(one_rowItems);
    }
    c1.close();

    close();

  dbnoteListAdapter = new NoteListAdapter(my_context, noteList);

 }

public ArrayList<NoteItems> get_NoteListArray(ArrayList<NoteItems> noteitems_list,NoteDatabase db) {

      noteitems_list.clear();

      db.open();
      String[] columns = new String[] {NoteDatabase.COLUMN_ID,NoteDatabase.COLUMN_TOPIC,NoteDatabase.COLUMN_NOTECONTENT,NoteDatabase.COLUMN_NAME};
      note_sqldb = noteopenHelper.getWritableDatabase();
      Cursor c1 = note_sqldb.query(NoteDatabase.TABLE_NOTE, columns, null, null, null, null, null);
      int iRow = c1.getColumnIndex(NoteDatabase.COLUMN_ID);
      int iTopic = c1.getColumnIndex(NoteDatabase.COLUMN_TOPIC);
      int iContent = c1.getColumnIndex(NoteDatabase.COLUMN_NOTECONTENT);
      int iOwner = c1.getColumnIndex(NoteDatabase.COLUMN_NAME);
      for(c1.moveToFirst(); !c1.isAfterLast(); c1.moveToNext()){     
          NoteItems one_rowItems = new  NoteItems();

          one_rowItems.set_rowTopic(c1.getString(iTopic));
          one_rowItems.set_rowContent(c1.getString(iContent));
          one_rowItems.set_rowOwner(c1.getString(iOwner));

          noteitems_list.add(one_rowItems);
      }
      c1.close();
      db.close();

      return noteitems_list;

 }

public int deleteNote(String topic) {
    return note_sqldb.delete(TABLE_NOTE, COLUMN_TOPIC + "='" + topic + "'", null);
}

public int deleteAllNote() {
    return note_sqldb.delete(TABLE_NOTE, null, null);
}
}
公共类数据库{
私有静态最终字符串数据库\u NAME=“DB\u NOTE”;
私有静态最终int数据库_VERSION=1;
公共静态最终字符串表\u NOTE=“NOTE”;
公共静态最终字符串列_ID=“_ID”;
公共静态最终字符串列\u TOPIC=“TOPIC”;
公共静态最终字符串列\u NOTECONTENT=“Content”;
公共静态最终字符串列\u NAME=“NAME”;
公共NoteListAdapter dbnoteListAdapter;
私有静态上下文my_上下文;
静态SQLITE数据库注释\u sqldb;
私有OpenHelper noteopenHelper;
公共记事本数据库(上下文c){
NoteDatabase.my_context=c;
}
public NoteDatabase open()引发SQLException{
noteopenHelper=新的OpenHelper(我的上下文);
注意_sqldb=noteopenHelper.getWritableDatabase();
归还这个;
}
公众假期结束(){
noteopenHelper.close();
}
公共长createData(字符串chude_注释,字符串noidung_注释){
ContentValues cv=新的ContentValues();
简历(主题栏,注释);
cv.put(列注释内容,noidung注释);
简历栏(栏名),由Blac填写