在listactivity中使用contextmenu删除子字符串

时间:2012-11-01 05:51:30

标签: android substring listactivity android-contextmenu

我有一个上下文菜单来删除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();
    }

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

1 个答案:

答案 0 :(得分:0)

删除后就行了         setListAdapter(适配器);