2014-02-08 45 views
4

對不起,我的英語不好,一個愚蠢的noob問題。 我有一個SimpleCursorAdapter和ListView與每個項目(數據庫中的行)上的按鈕。
我意識到刪除行但我不知道如何刷新ListView。
我希望有人能幫助我簡單明瞭的例子刪除數據庫行後刷新自定義光標適配器

我的適配器

public class MySimpleCursorAdapter extends SimpleCursorAdapter { 

Context context; 

public MySimpleCursorAdapter(Context contxt, int layout, Cursor c, String[] from, int[] to, int flags) { 
    super(contxt, layout, c, from, to, flags); 
    context=contxt; 

} 

public View newView(Context _context, Cursor _cursor, ViewGroup parent){ 
    LayoutInflater inflater = (LayoutInflater) _context.getSystemService(_context.LAYOUT_INFLATER_SERVICE); 
    View view = inflater.inflate(R.layout.listform_item, parent, false); 
    return view; 
} 

@Override 
public void bindView(View view, Context Context, Cursor cursor) { 
    String name = cursor.getString(cursor.getColumnIndex(DBHelper.FORM_NAME)); 
    String title = cursor.getString(cursor.getColumnIndex(DBHelper.FORM_TITLE)); 
    TextView formname = (TextView)view.findViewById(R.id.tvFormName); 
    formname.setText(name); 
    TextView formtitle=(TextView)view.findViewById(R.id.tvFormTitle); 
    formtitle.setText(title); 
    ImageButton yourButton = (ImageButton)view.findViewById(R.id.ibtnDelete); 
    yourButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      if(view != null) { 
       Object obj = view.getTag(); 
       //if(obj != null && obj instanceof Integer) { 
       dbForm form=new dbForm(context); 
       form.open(); 
       String st=obj.toString(); 
       form.deleteForm(Long.valueOf(st).longValue()); 
        Toast.makeText(context, "Delete row with id = " + st, Toast.LENGTH_LONG).show(); 


      } 
     } 
    }); 
    Object obj = cursor.getString(cursor.getColumnIndex(DBHelper.FORM_ID)); 
    yourButton.setTag(obj); 


} 
} 

我也使用CursorLoader主進入電影數據庫

UPD:我使用使用遊標加載程序和一個不知道如何呼叫他在我的自定義適配器重置,希望有所幫助。

​​

}

+0

看看這個線程 http://stackoverflow.com/questions/2250770/how-to-refresh-android-listview –

回答

4

對於SimpleCursorAdapter最好使用這樣的CursorLoader:

public Loader<Cursor> onCreateLoader(int id,Bundle arg) { 
    return new SimpleCursorLoader(this) { 
     public Cursor loadInBackground() { 
      // This field reserved to what you want to load in your cursor adater and you return it (for example database query result) 
      // return Cursor ; 
     } 
    }; 
} 

public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { 
    adapter.swapCursor(cursor);  
} 

public void onLoaderReset(Loader<Cursor> loader){ 
    adapter.swapCursor(null); 
} 

而在你的MainActivity或在那裏你會實例化的適配器,就在補充一點:

getLoaderManager().initLoader(0x01, null, this); 
// declare your new Custom Simple Cursor Adapter here 
scAdapter = new MySimpleCursorAdapter .... 

並添加:

public void onResume() { 
    super.onResume(); 
    // Restart loader so that it refreshes displayed items according to database 
    getLoaderManager().restartLoader(0x01, null, this); 
} 

我用裝載機這樣的工作,我希望它幫助你。

+0

謝謝,我想到它,但我不能理解如何與我的自定義適配器。我用主代碼更新我的文章,我希望你給我的代碼建議。 – mechanikos

+0

我更新了我的答案,檢查並告訴我它是否有效。 – Sacredila

+0

是的,它工作,如果我退出我的應用程序(現在不是必須刷新進程),但不實時刷新。我只有一個活動,我不知道刪除後如何強制刷新活動。 – mechanikos

1

您應該重新載入你的光標,並更新你的光標適配器:

// Reload your cursor here 
Cursor newCursor = ….; 

if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD){ 
    adapter.swapCursor(newCurosr); 
} else { 
    adapter.changeCursor(newCursor); 
} 

然後,通知有關數據的更改您的列表視圖:

adapter.notifyDataSetChanged(); 
相關問題