Chase_Java
2016-11-14 08:47
采纳率: 30%
浏览 1.2k

求大神,安卓中,如何动态更新ListView?

我在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);

9条回答 默认 最新

相关推荐 更多相似问题