我在listview中的點擊長按事件中刪除了一條數據,就是在sqlite裡刪掉了,怎麼能立即刷新listview呢?
listview我用cursorAdapter
求大神解答,謝謝了,沒有c幣了,實在不好意思~
package com.chase.cn.demon;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
/**
* Created by Chase on 2016/11/10.
*/
public class BillActivity extends Activity {
private ListView billListView;
private MySQLiteOpenHelper sqLiteOpenHelper;
private SQLiteDatabase mDataBase;
private SimpleCursorAdapter billAdapter;
private boolean flag = true;
// 存儲數據的數組列表
// ArrayList<HashMap<String, Object>> listData;
// 適配器
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bill);
sqLiteOpenHelper = new MySQLiteOpenHelper(BillActivity.this, "finance.db", null, 1);
mDataBase = sqLiteOpenHelper.getReadableDatabase();
final Cursor c = mDataBase.rawQuery("select * from finance", null);
Log.i("database", "data" + mDataBase.isOpen());
billListView = (ListView) findViewById(R.id.bill_listView);
billAdapter = new SimpleCursorAdapter(BillActivity.this,
R.layout.bill_item,
c,
// ListItem的XML實現
// 動態數組與Item對應的子項
new String[]{"Time", "Fee", "Budget", "Type"},
// ImageItem的XML文件裡面的一個ImageView,兩個TextView ID
new int[]{R.id.billItem_time, R.id.billItem_money, R.id.billItem_budget, R.id.billItem_type});
billListView.setAdapter(billAdapter);
//item監聽
billListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
String cc = c.getString(c.getColumnIndex("_id")); //用cursor得到對應表中的id
Log.i("info", "cc= " + cc);
Toast.makeText(BillActivity.this, "刪除了", Toast.LENGTH_SHORT).show();
//刪除表中的對應id的行
mDataBase.execSQL("delete from finance where _id="
+ cc);
自己琢磨了,不單單要notifydadasetChanged 最重要的是要重新查詢一下
(billAdapter.getCursor()).requery();
billAdapter.notifyDataSetChanged();