2 u013451449 u013451449 于 2014.09.12 21:31 提问

为什么ListView刷新不成功

自己写了一个SQLite和ListView关联的代码,想实现每次点击一个item就删除这个item对应的数据库数据的功能,现在其他功能都实现了,就是点击之后ListView不会自动刷新,重新打开程序的话,可以看到数据确实被删除了,就是ListView不会自动刷新。很苦恼,这到底是为啥呢?

附代码:

public class ShowDataBaseActivity extends Activity{

private SQLiteDatabase db;
private Cursor cursor;
private EditText et01;
private EditText et02;
SimpleCursorAdapter sca;


@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.showdatabaseactivity);

    RuleDataBaseHelper ruleDataBaseHelper = new RuleDataBaseHelper(ShowDataBaseActivity.this, "text_RULE");
    TextView tv = (TextView) findViewById(R.id.TextView01);
    ListView lv = (ListView) findViewById(R.id.ListView01);
    db = ruleDataBaseHelper.getReadableDatabase();
    cursor = db.query("RULE", new String[]{"_id", "_check", "_reply"}, null, null, null, null, null);
    if(cursor.getCount()>0){
        tv.setVisibility(View.GONE);
    }
    sca=new SimpleCursorAdapter(ShowDataBaseActivity.this, R.layout.item, cursor, new String[]{"_check", "_reply"}, new int[]{R.id.TextView02,R.id.TextView03},1);
    lv.setAdapter(sca);
    db.close();

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1, int which, long arg3) {

            RuleDataBaseHelper ruleDataBaseHelper = new RuleDataBaseHelper(ShowDataBaseActivity.this, "text_RULE");
            db = ruleDataBaseHelper.getWritableDatabase();
            db.delete("RULE", "_id=" + cursor.getInt(cursor.getColumnIndex("_id")), null);

            //sca=new SimpleCursorAdapter(ShowDataBaseActivity.this, R.layout.item, cursor, new String[]{"_check", "_reply"}, new int[]{R.id.TextView02,R.id.TextView03},1);
            sca.notifyDataSetChanged();
            db.close();
        }
    });


}

}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!