0

我有一个上下文菜单来删除 listactivity 上的数据,但是当我在我的应用程序上尝试它时,数据并没有被删除。以前我在我的 AlmagHelper 类上使用子字符串查询输入数据。如果我创建的编码有问题?请帮忙 ..

我创建的这个活动课..

public class Pendapatan extends ListActivity {
    Cursor model=null;
    AlmagAdapter adapter=null;
    AlmagtHelper helper=null;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview_nasabah);

        helper=new AlmagtHelper(this);

        model=helper.getAllPendapatan();
        startManagingCursor(model);
        adapter=new AlmagAdapter(model);
        setListAdapter(adapter);

        registerForContextMenu(getListView());
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        getMenuInflater().inflate(R.menu.action_pendapatan, menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item){
        AdapterView.AdapterContextMenuInfo info=
            (AdapterContextMenuInfo)item.getMenuInfo();

        switch (item.getItemId()) {
        case R.id.hapus_pendapatan:
            delete(info.id);
            return(true);
        }
        return false;
    }

    private void delete(final long rowId) {
        if (rowId>0) {
            new AlertDialog.Builder(this)
            .setTitle("Hapus")
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    prosesDelete(rowId);
                }
            }) .setNegativeButton("Batal", new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog, int whichButton) {
                }
            }).show();
                }
    }

    private void prosesDelete(long rowId) {
        String[] args={String.valueOf(rowId)};

        //is there something wrong with this code?

        helper.getWritableDatabase().delete("pendapatan", "_id =?", args);
        model.requery();
    }

这是输入包含在类 AlmagHelper 中的数据的代码..

public Cursor getAllPendapatan() {
    return(getReadableDatabase()
                    .rawQuery("SELECT substr(_id, 1, 10) as _id, sum(value) as total FROM pendapatan GROUP BY _id ",
                                        null));
}

我试过使用这段代码,但它不起作用

private void prosesDelete(long rowId) {
        String[] args={String.valueOf(rowId)};

        helper.getWritableDatabase().delete("pendapatan", "(substr(_id, 1, 10)) = _id =?", args);
        model.requery();
    }

我有什么解决办法吗?任何解决方案对我来说都非常有用。谢谢 :-)

4

1 回答 1

0

删除后只需执行 setListAdapter(adapter);

于 2012-11-01T05:54:37.237 回答